简体   繁体   English

使用PowerShell脚本中的WinSCP .NET程序集在Linux服务器上运行bash脚本

[英]Running bash script on Linux server using WinSCP .NET assembly from PowerShell script

I am currently implementing a PowerShell script to utilize WinSCP .NET assembly in order to access a set of Linux servers, execute a bash script on each server, and copy files. 我目前正在实现PowerShell脚本,以利用WinSCP .NET程序集来访问一组Linux服务器,在每个服务器上执行bash脚本,并复制文件。 I have successfully implemented the connection and copy functionality for my script (verified by pulling files from each machine), however I am unable to execute the bash script correctly. 我已经为脚本成功实现了连接和复制功能(已通过从每台计算机提取文件进行验证),但是我无法正确执行bash脚本。

I have looked into both ExecuteCommand and call commands, but receive a similar error message for all of the variations that I have attempted. 我已经研究了ExecuteCommandcall命令,但是对于我尝试的所有变体都收到了类似的错误消息。

Error: 错误:

WinSCP.SessionRemoteException: Connection has been unexpectedly closed. WinSCP.SessionRemoteException:连接已意外关闭。 Server sent command exit status 255. 服务器已发送命令退出状态255。
Error skipping startup message. 跳过启动消息时出错。 Your shell is probably incompatible with the application (BASH is recommended). 您的外壳可能与应用程序不兼容(建议使用BASH)。

Would someone be able to provide an explanation as to why the script connects and copies correctly, but is unable to execute the bash script remotely? 有人能够提供有关脚本为什么正确连接和复制但无法远程执行bash脚本的解释吗?

Foreach ($asset in $idAssetArray)
{
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = $asset[1]
        UserName = $credential.UserName
        Password = $credential.GetNetworkCredential().Password
        SshHostKeyFingerprint = $asset[2]
    }

    #
    $HostDescription = $asset[0]

    $session = New-Object WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

        # Format timestamp
        $stamp = $(Get-Date -f "MMddyyyy")

        # Create timestamp directory if DNE
        $newDirectory = $stamp
        md -Force $newDirectory

        # ATTEMPT SCRIPT EXECUTION HERE

        # Execute CVA Collection Script on asset
        #$runCommand = "./EncariCVA_RedHatLinux_CollectData.sh"
        #$session.ExecuteCommand($runCommand)
        #$session.call ../tmp/cva/EncariCVA_RedHatLinux_CollectData.sh
        $session.ExecuteCommand("call ./EncariCVA_RedHatLinux_CollectData.sh")

        # Download the file and throw on any error
        $session.GetFiles(
            ($remotePath + $fileName),
            ($localPath + $stamp + "\" + $HostDescription + "." + $stamp + "." + $fileName)).Check()
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
}
exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

Log Output: 日志输出:

. 2016-08-09 11:43:24.885 --------------------------------------------------------------------------
. 2016-08-09 11:43:24.885 WinSCP Version 5.9 (Build 6786) (OS 6.1.7601 Service Pack 1 - Windows 7 Enterprise)
. 2016-08-09 11:43:24.885 Configuration: nul
. 2016-08-09 11:43:24.885 Log level: Normal
. 2016-08-09 11:43:24.885 Local account: 
. 2016-08-09 11:43:24.885 Working directory: C:\Scripts\CVACollection
. 2016-08-09 11:43:24.885 Process ID: 7860
. 2016-08-09 11:43:24.885 Command-line: "" /xmllog="" /xmlgroups /nointeractiveinput /dotnet=590  /ini=nul /log="c:\Scripts\CVACollection\Log.txt"  /console /consoleinstance=
. 2016-08-09 11:43:24.886 Time zone: Current: GMT-5, Standard: GMT-6 (Central Standard Time), DST: GMT-5 (Central Daylight Time), DST Start: 3/13/2016, DST End: 11/6/2016
. 2016-08-09 11:43:24.886 Login time: Tuesday, August 09, 2016 11:43:24 AM
. 2016-08-09 11:43:24.886 --------------------------------------------------------------------------
. 2016-08-09 11:43:24.886 Script: Retrospectively logging previous script records:
> 2016-08-09 11:43:24.886 Script: option batch on
< 2016-08-09 11:43:24.886 Script: batch           on        
< 2016-08-09 11:43:24.886 Script: reconnecttime   120       
> 2016-08-09 11:43:24.886 Script: option confirm off
< 2016-08-09 11:43:24.886 Script: confirm         off       
> 2016-08-09 11:43:24.886 Script: option reconnecttime 120
< 2016-08-09 11:43:24.886 Script: reconnecttime   120       
> 2016-08-09 11:43:24.886 Script: open sftp://X:***@ -hostkey="" -timeout=15
. 2016-08-09 11:43:24.886 --------------------------------------------------------------------------
. 2016-08-09 11:43:24.886 Session name:  (Ad-Hoc site)
. 2016-08-09 11:43:24.886 Host name:  (Port: 22)
. 2016-08-09 11:43:24.886 User name: X (Password: Yes, Key file: No)
. 2016-08-09 11:43:24.886 Tunnel: No
. 2016-08-09 11:43:24.886 Transfer Protocol: SFTP
. 2016-08-09 11:43:24.886 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2016-08-09 11:43:24.886 Disable Nagle: No
. 2016-08-09 11:43:24.886 Proxy: None
. 2016-08-09 11:43:24.886 Send buffer: 262144
. 2016-08-09 11:43:24.886 SSH protocol version: 2; Compression: No
. 2016-08-09 11:43:24.886 Bypass authentication: No
. 2016-08-09 11:43:24.886 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2016-08-09 11:43:24.886 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2016-08-09 11:43:24.886 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2016-08-09 11:43:24.886 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2016-08-09 11:43:24.886 Simple channel: Yes
. 2016-08-09 11:43:24.886 Return code variable: Autodetect; Lookup user groups: Auto
. 2016-08-09 11:43:24.886 Shell: default
. 2016-08-09 11:43:24.886 EOL: LF, UTF: Auto
. 2016-08-09 11:43:24.886 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2016-08-09 11:43:24.886 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2016-08-09 11:43:24.886 SFTP Bugs: Auto,Auto
. 2016-08-09 11:43:24.886 SFTP Server: default
. 2016-08-09 11:43:24.886 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2016-08-09 11:43:24.886 Cache directory changes: Yes, Permanent: Yes
. 2016-08-09 11:43:24.886 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2016-08-09 11:43:24.886 DST mode: Unix
. 2016-08-09 11:43:24.886 --------------------------------------------------------------------------
. 2016-08-09 11:43:24.886 Looking up host "X" for SSH connection
. 2016-08-09 11:43:24.886 Connecting to X port 22
. 2016-08-09 11:43:24.924 We claim version: SSH-2.0-WinSCP_release_5.9
. 2016-08-09 11:43:24.944 Server version: SSH-2.0-OpenSSH_5.3
. 2016-08-09 11:43:24.944 We believe remote version has SSH-2 channel request bug
. 2016-08-09 11:43:24.944 Using SSH protocol version 2
. 2016-08-09 11:43:24.945 Have a known host key of type rsa2
. 2016-08-09 11:43:24.965 Doing Diffie-Hellman group exchange
. 2016-08-09 11:43:24.984 Doing Diffie-Hellman key exchange with hash SHA-256
. 2016-08-09 11:43:25.540 Server also has ssh-dss host key, but we don't know it
. 2016-08-09 11:43:25.540 Host key fingerprint is:
. 2016-08-09 11:43:25.540 ssh-rsa 2048 X
. 2016-08-09 11:43:25.540 Verifying host key rsa2 X
. 2016-08-09 11:43:25.541 Host key matches configured key
. 2016-08-09 11:43:25.542 Initialised AES-256 SDCTR client->server encryption
. 2016-08-09 11:43:25.542 Initialised HMAC-SHA-256 client->server MAC algorithm
. 2016-08-09 11:43:25.542 Initialised AES-256 SDCTR server->client encryption
. 2016-08-09 11:43:25.542 Initialised HMAC-SHA-256 server->client MAC algorithm
! 2016-08-09 11:43:25.616 Using username "X".
. 2016-08-09 11:43:25.635 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2016-08-09 11:43:25.635 Using stored password.
. 2016-08-09 11:43:25.636 Sent password
. 2016-08-09 11:43:25.654 Access granted
. 2016-08-09 11:43:25.654 Opening session as main channel
. 2016-08-09 11:43:25.675 Opened main channel
. 2016-08-09 11:43:25.751 Started a shell/command
. 2016-08-09 11:43:25.752 --------------------------------------------------------------------------
. 2016-08-09 11:43:25.752 Using SFTP protocol.
. 2016-08-09 11:43:25.752 Doing startup conversation with host.
> 2016-08-09 11:43:25.752 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2016-08-09 11:43:25.772 Type: SSH_FXP_VERSION, Size: 95, Number: -1
. 2016-08-09 11:43:25.772 SFTP version 3 negotiated.
. 2016-08-09 11:43:25.772 Unknown server extension posix-rename@openssh.com="1"
. 2016-08-09 11:43:25.772 Supports statvfs@openssh.com extension version "2"
. 2016-08-09 11:43:25.772 Unknown server extension fstatvfs@openssh.com="2"
. 2016-08-09 11:43:25.772 We believe the server has signed timestamps bug
. 2016-08-09 11:43:25.772 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
. 2016-08-09 11:43:25.772 Limiting packet size to OpenSSH sftp-server limit of 262148 bytes
. 2016-08-09 11:43:25.772 Getting current directory name.
. 2016-08-09 11:43:25.773 Getting real path for '.'
> 2016-08-09 11:43:25.773 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2016-08-09 11:43:25.792 Type: SSH_FXP_NAME, Size: 43, Number: 16
. 2016-08-09 11:43:25.792 Real path is '/home/X'
. 2016-08-09 11:43:25.792 Startup conversation with host finished.
< 2016-08-09 11:43:25.792 Script: Active session: [1] X@X
> 2016-08-09 11:43:26.465 Script: pwd
< 2016-08-09 11:43:26.465 Script: /home/X
> 2016-08-09 11:43:26.545 Script: call bash /tmp/cva/EncariCVA_RedHatLinux_CollectData.sh
< 2016-08-09 11:43:26.546 Script: Searching for host...
. 2016-08-09 11:43:26.547 [Shell] Looking up host "X" for SSH connection
. 2016-08-09 11:43:26.547 [Shell] Connecting to X port 22
. 2016-08-09 11:43:26.572 [Shell] We claim version: SSH-2.0-WinSCP_release_5.9
< 2016-08-09 11:43:26.573 Script: Connecting to host...
. 2016-08-09 11:43:26.595 [Shell] Server version: SSH-2.0-OpenSSH_5.3
. 2016-08-09 11:43:26.595 [Shell] We believe remote version has SSH-2 channel request bug
. 2016-08-09 11:43:26.595 [Shell] Using SSH protocol version 2
. 2016-08-09 11:43:26.595 [Shell] Have a known host key of type rsa2
. 2016-08-09 11:43:26.618 [Shell] Doing Diffie-Hellman group exchange
. 2016-08-09 11:43:26.637 [Shell] Doing Diffie-Hellman key exchange with hash SHA-256
. 2016-08-09 11:43:27.222 [Shell] Server also has ssh-dss host key, but we don't know it
. 2016-08-09 11:43:27.222 [Shell] Host key fingerprint is:
. 2016-08-09 11:43:27.222 [Shell] ssh-rsa 2048 X
. 2016-08-09 11:43:27.222 [Shell] Verifying host key rsa2 X  with fingerprint ssh-rsa 2048 X
< 2016-08-09 11:43:27.222 Script: Authenticating...
. 2016-08-09 11:43:27.222 [Shell] Host key matches configured key
. 2016-08-09 11:43:27.222 [Shell] Initialised AES-256 SDCTR client->server encryption
. 2016-08-09 11:43:27.222 [Shell] Initialised HMAC-SHA-256 client->server MAC algorithm
. 2016-08-09 11:43:27.222 [Shell] Initialised AES-256 SDCTR server->client encryption
. 2016-08-09 11:43:27.223 [Shell] Initialised HMAC-SHA-256 server->client MAC algorithm
! 2016-08-09 11:43:27.317 [Shell] Using username "X".
< 2016-08-09 11:43:27.317 Script: Using username "X".
. 2016-08-09 11:43:27.345 [Shell] Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2016-08-09 11:43:27.346 [Shell] Using stored password.
< 2016-08-09 11:43:27.346 Script: Authenticating with pre-entered password.
. 2016-08-09 11:43:27.347 [Shell] Sent password
. 2016-08-09 11:43:27.366 [Shell] Access granted
. 2016-08-09 11:43:27.366 [Shell] Opening session as main channel
. 2016-08-09 11:43:27.388 [Shell] Opened main channel
. 2016-08-09 11:43:27.481 [Shell] Started a shell/command
< 2016-08-09 11:43:27.481 Script: Authenticated.
. 2016-08-09 11:43:27.481 [Shell] --------------------------------------------------------------------------
. 2016-08-09 11:43:27.481 [Shell] Using SCP protocol.
. 2016-08-09 11:43:27.481 [Shell] Doing startup conversation with host.
< 2016-08-09 11:43:27.481 Script: Starting the session...
. 2016-08-09 11:43:27.482 [Shell] Skipping host startup message (if any).
> 2016-08-09 11:43:27.482 [Shell] echo "WinSCP: this is end-of-file:0"
! 2016-08-09 11:43:27.661 [Shell] Can't call method "readline" on an undefined value at /usr/local/share/perl5/Term/Shell.pm line 107.
. 2016-08-09 11:43:27.664 [Shell] Server sent command exit status 255
. 2016-08-09 11:43:27.666 Closing connection.
. 2016-08-09 11:43:27.666 Sending special code: 12
. 2016-08-09 11:43:27.667 Sent EOF message
. 2016-08-09 11:43:27.667 [Shell] Disconnected: All channels closed
< 2016-08-09 11:43:27.669 Script: Connection has been unexpectedly closed. Server sent command exit status 255.
< 2016-08-09 11:43:27.669 Error skipping startup message. Your shell is probably incompatible with the application (BASH is recommended).
. 2016-08-09 11:43:27.670 Script: Failed
> 2016-08-09 11:43:28.213 Script: exit
. 2016-08-09 11:43:28.213 Script: Exit code: 1
. 2016-08-09 11:43:29.249 --------------------------------------------------------------------------

You should call the script with bash : 您应该使用bash调用脚本:

$session.ExecuteCommand("bash /path/to/EncariCVA_RedHatLinux_CollectData.sh")

This will grant that: 这将授予:

  • You'll be, effectively, using bash to call your bash script (eg, if the shebang is missing). 您将有效地使用bash来调用bash脚本(例如,如果缺少shebang)。
  • Even if there are permission issues (executable bit unset), the script will still be executed. 即使存在权限问题(未设置可执行位),该脚本仍将执行。

The argument to the Session.ExecuteCommand method is the command to execute on the server. Session.ExecuteCommand方法的参数是要在服务器上执行的命令。

While the call is a local WinSCP scripting command to execute a command on the server. call是本地WinSCP脚本命令时,将在服务器上执行命令。

The ExecuteCommand(command) calls call command internally. ExecuteCommand(command)内部调用call command So you are effectively calling call call command . 因此,您实际上是在调用call call command

Just remove the call from the ExecuteCommand . 只需从ExecuteCommand删除该call

$session.ExecuteCommand("./EncariCVA_RedHatLinux_CollectData.sh")

Though you need to "type" a password when starting the bash, you would have to feed the password from a redirected input, like: 尽管启动bash时需要“输入”密码,但是您必须从重定向的输入中输入密码,例如:

$session.ExecuteCommand("echo password| bash ./EncariCVA_RedHatLinux_CollectData.sh")

But seeing the "Can't call method "readline" on an undefined value" , it actually looks like the script that reads the password has some problems. 但是,看到“无法在未定义的值上调用方法“ readline”时 ,实际上看起来像读取密码的脚本存在一些问题。 Possibly due to a non-interactive nature of the WinSCP shell session. 可能是由于WinSCP Shell会话的非交互性质。 But that's just a wild guess. 但这只是一个疯狂的猜测。 We cannot help you there without knowing more details about the script. 如果您不了解有关脚本的更多详细信息,我们将无法为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM