【问题标题】:Is there a SCP alternative for PowerShell?PowerShell 是否有 SCP 替代品?
【发布时间】:2013-08-23 00:47:08
【问题描述】:

我需要编写一个脚本,将文件从文件夹传输到另一台服务器 (Linux),但传输文件的脚本在 Windows 上,我想知道是否有替代 scp 用于 PowerShell(或者还有另一种方法)

【问题讨论】:

  • 对于使用 PowerShell 版本 3 或更高版本的任何人,github.com/darkoperator/Posh-SSH 是一个选项。
  • 老实说,这项任务在 MS 中仍然非常困难,这太荒谬了。这是服务器管理的最基本任务..

标签: powershell scp


【解决方案1】:

Putty 附带一个方便的小工具,名为 pscp.exe,它可以执行此操作,并且可以在 powershell 中轻松调用。

下面的示例从 Windows 复制到 CentOS 机器(以用户代码“bill”登录),然后您使用 pscp 中的 -pw 开关来传递密码(否则生成的命令窗口将提示输入Linux 密码):

Start-Process 'C:\Program Files (x86)\PuTTY\pscp.exe' -ArgumentList ("-scp -pw password C:\Document.rtf bill@192.168.0.28:/home/bill/")  

PuTTY Secure Copy client
Release 0.62
Usage: pscp [options] [user@]host:source target
       pscp [options] source [source...] [user@]host:target
       pscp [options] -ls [user@]host:filespec
Options:
  -V        print version information and exit
  -pgpfp    print PGP key fingerprints and exit
  -p        preserve file attributes
  -q        quiet, don't show statistics
  -r        copy directories recursively
  -v        show verbose messages
  -load sessname  Load settings from saved session
  -P port   connect to specified port
  -l user   connect with specified username
  -pw passw login with specified password
  -1 -2     force use of particular SSH protocol version
  -4 -6     force use of IPv4 or IPv6
  -C        enable compression
  -i key    private key file for authentication
  -noagent  disable use of Pageant
  -agent    enable use of Pageant
  -batch    disable all interactive prompts
  -unsafe   allow server-side wildcards (DANGEROUS)
  -sftp     force use of SFTP protocol
  -scp      force use of SCP protocol

【讨论】:

  • 使用基于密钥的身份验证,而不是将密码放入脚本中。
  • 公平点,这只是一个简单的例子,表明 PSCP 可以完成所要求的工作:-)
  • 这里是 Windows 菜鸟。当我这样做时,我只是得到一个空白的控制台窗口,它在大约 10 秒后消失,并且似乎没有发生 SCP 传输。关于可能发生的事情有什么想法吗?
  • 我能够通过从命令提示符而不是 Powershell 运行命令来确定存在防火墙问题,但是如果有某种方法可以通过在 Powershell 中运行命令来获取输出,那就太好了:)
  • @jayhendren 来自the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter5.html 的文档 - pscp 返回错误代码。因此,在启动过程完成后,立即检查 $lastexitcode 的值或将 start-process 语句的结果放入变量中,然后检查 - 例如 $result = Start-Process
【解决方案2】:

pscp.exe 是一个可行的选择,但几年来我一直在使用来自Rebex 的库在 C# 应用程序和 PowerShell 脚本中进行 SFTP 和 FTPS 传输,并取得了巨大成功。他们的包还包括an SCP object,但我没有亲自使用过。

pscp 相比,它确实需要花钱。在选择 Rebex 包之前,我曾考虑采用 PuTTY 路线,但我的团队认为,从长远来看,拥有一个可以轻松融入任何应用程序/脚本的库是值得的。

【讨论】:

【解决方案3】:

您可以使用 PowerShell 中的 WinSCP .NET assembly 进行 SCP 传输。

例如见http://winscp.net/eng/docs/library_powershell#example

该示例使用 SFTP 协议。要使用 SCP,只需将其修改为:

$sessionOptions.Protocol = [WinSCP.Protocol]::Scp

虽然您的服务器支持 SCP 协议,但它很可能也支持 SFTP。如果您有选择,SFTP 是更好的选择。

【讨论】:

    【解决方案4】:

    还有一种“.NET 友好”的方式:

    您可以使用SharpSSH dll 来执行 ssh 命令,并进行 scp/sftp 传输。

    例如:

    [Reflection.Assembly]::LoadFrom((Resolve-Path .\Tamir.SharpSSH.dll))
    
    $ssh = New-Object Tamir.SharpSsh.Sftp("server","user","password")
    
    $ssh.Connect()
    
    $ssh.Put("C:\localfile","distantfile")
    
    $ssh.Close()
    

    还有SSH.Net 库,它的功能大致相同。

    【讨论】:

      【解决方案5】:

      如今,Windows 已将 OpenSSH(包括 SCP)作为可选组件,因此您可以直接使用它。有关如何设置的说明,请参阅 OpenSSH in Windows

      或者,如果您在两台机器上都有 PowerShell,则应该可以使用 Copy-Item 并通过 SSH 连接会话传递 -ToSession 来执行此操作,但我从未真正尝试过。它需要最新版本的 PowerShell 和更多设置,请参阅 PowerShell remoting over SSH。像这样的:

      Copy-Item C:\localPath\*.* ~\remotePath\ -ToSession (New-PSSession -HostName UserA@LinuxServer01:22 -KeyFilePath c:\\userAKey_rsa)
      

      如果两台机器都是 Windows 机器,您可以使用相同的 -ToSession 参数通过 WinRM 复制文件。但是两台机器都必须加入域,否则可能存在安全问题。

      【讨论】:

        猜你喜欢
        • 2017-03-25
        • 2010-09-13
        • 2013-01-29
        • 2011-01-19
        • 2020-06-11
        • 1970-01-01
        • 1970-01-01
        • 2021-01-01
        • 2015-11-13
        相关资源
        最近更新 更多