如何编写将文件从 Unix 复制到 Windows 的脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/569789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:01:02  来源:igfitidea点击:

How can I script copying files from Unix to Windows?

windowsunixvbscriptcopysamba

提问by CheeseConQueso

Is there any way a script can be made that copies files from a Unix drive onto a Windows drive?

有没有什么办法可以制作一个脚本来将文件从 Unix 驱动器复制到 Windows 驱动器上?

回答by Kurt W. Leucht

I do it manually all the time using secure copy (SCP). SCP comes already installed on most Unix machines. On my PC, I installed PuTTYwhich is a secure terminal app for Windows and also contains a secure copy utility.

我一直使用安全副本 ( SCP)手动执行此操作。SCP 已经安装在大多数 Unix 机器上。在我的 PC 上,我安装了PuTTY,它是 Windows 的安全终端应用程序,还包含一个安全复制实用程序。

When you do the secure copy manually from the Windows command line, you put your Unix username into the command line command, but then you have to type in your password at the prompt, so it's interactive. But I've also thought about doing it from a script or a batch file. In that case, you will need to create and install private and public keyson the appropriate machines. I created the appropriate keys and installed them on both the Windows and Unix machines and then I manually performed the Windows command line secure copy again and it did notask me for my password that time. So this is one way you can accomplish your task. I'm sure there are many other ways, though.

当您从 Windows 命令行手动执行安全复制时,您将 Unix 用户名放入命令行命令,但随后您必须在提示符下输入密码,因此它是交互式的。但我也考虑过从脚本或批处理文件中执行此操作。在这种情况下,您需要在适当的机器上创建和安装私钥和公钥。我创建了相应的键和安装他们在Windows和Unix机器都然后我再次手动执行的Windows命令行的安全副本,它并没有问我要我的密码的时间。因此,这是您完成任务的一种方式。不过,我相信还有很多其他方法。

回答by crashmstr

Since you have a Samba tag, are you talking about mounting a network share from a Unix machine onto your Windows machine?

既然你有一个 Samba 标签,你是在谈论从 Unix 机器上安装网络共享到你的 Windows 机器上吗?

If so, it is just treated like a normal network drive and you would be able to copy anything that you had permissions via the share.

如果是这样,它就像一个普通的网络驱动器一样,您将能够通过共享复制您拥有权限的任何内容。

回答by Stuart Childs

Yes, as long as the operating system the script is executing on can read from the Unix drive and write to the Windows drive. Since this is tagged vbscript, I'll assume you're running on Windows so you might want to look into something like thisto get access to Unix filesystems for Windows (although that's a Linux ext driver). Then it's just running a standard copy function to complete your task.

是的,只要执行脚本的操作系统可以从 Unix 驱动器读取并写入 Windows 驱动器。由于这是标记VBScript中,我会假设你在Windows上运行,所以你可能想看看像这样以访问Unix文件系统为Windows(虽然这是一个Linux的分机司机)。然后它只是运行一个标准的复制功能来完成你的任务。

回答by Boris Brodski

I would use expecttool.

我会使用expect工具。

Here is an example, how to upload

这是一个例子,如何上传

  • /local/path/to/file
  • /local/path/to/file

to

  • \\HOST\SHARE\remote\path\to\file
  • \\HOST\SHARE\remote\path\to\file

authenticating with domain\userusing password password:

domain\user使用密码进行身份验证password

expect <<<EOF
set timeout 10
spawn smbclient //HOST/SHARE "-Udomain\user%password"
expect {
  "smb: \\\\>" {
    send "cd /remote/path/to\r"
    expect {
      "NT_STATUS_OBJECT_NAME_NOT_FOUND" {exit 1}
      "smb: *>"                         {
        set timeout -1
        send "put /local/path/to/file file\r"
        expect {
          "putting file " {
            expect "smb: *>" {exit 0}
          }
          "smb: *>" {exit 1}
        }
      }
    }
  }
}
# Timeout
exit 1
EOF