2016-08-08 33 views
1

현재 Linux 서버 세트에 액세스하고 각 서버에서 bash 스크립트를 실행하고 복사하기 위해 WinSCP .NET 어셈블리를 활용하는 PowerShell 스크립트를 구현 중입니다. 파일. 성공적으로 스크립트의 연결 및 복사 기능을 구현했지만 (각 시스템에서 파일을 가져 와서 확인) bash 스크립트를 올바르게 실행할 수는 없습니다.PowerShell 스크립트에서 WinSCP .NET 어셈블리를 사용하여 Linux 서버에서 bash 스크립트 실행

나는 ExecuteCommandcall 명령을 모두 조사했지만 시도한 모든 유사 콘텐츠에 대해 유사한 오류 메시지가 표시됩니다.

오류 :

WinSCP.SessionRemoteException: Connection has been unexpectedly closed. Server sent command exit status 255.
Error skipping startup message. Your shell is probably incompatible with the application (BASH is recommended).

누군가가 스크립트가 연결되어 제대로 복사,하지만 원격으로 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 
} 

로그 출력 : 당신은 bash와 스크립트를 호출해야합니다

. 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 [email protected]="1" 
. 2016-08-09 11:43:25.772 Supports [email protected] extension version "2" 
. 2016-08-09 11:43:25.772 Unknown server extension [email protected]="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] [email protected] 
> 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 -------------------------------------------------------------------------- 
+1

내가 잘못하지 않았다면,'call'은 배치 스크립트 호출에 사용됩니다. 'bash'를 실행하고 스크립트를 처리하도록해야합니다. – alvits

답변

2

이 그 권한을 부여합니다

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

:

    당신은 수 있습니다
  • , 효과적으로, bash을 사용하여 bash 스크립트를 호출하십시오 (예 : shebang이 누락 된 경우).
  • 권한 문제 (실행 가능 비트 설정 해제)가 있어도 스크립트는 계속 실행됩니다.
2

Session.ExecuteCommand method의 인수는 서버에서 실행할 명령입니다.

call은 서버에서 명령을 실행하는 로컬 WinSCP scripting 명령입니다.

ExecuteCommand(command)call command을 내부적으로 호출합니다. 따라서 효과적으로 call call command으로 전화하고 있습니다.

ExecuteCommand에서 call을 제거하면됩니다.

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

당신은 떠들썩한 파티를 시작할 때, 당신처럼 재 입력에서 암호를 공급해야하는 "유형"암호가 필요하지만 "

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

그러나 을보고 정의되지 않은 값 ""에서 메서드 "readline"을 호출 할 수 없다는 것은 실제로 암호를 읽는 스크립트에 문제가있는 것처럼 보입니다. 비대화 형 WinSCP 셸 세션 때문일 수 있습니다. 그러나 그것은 단지 야생의 추측입니다. 스크립트에 대한 자세한 내용을 알지 못하면서 도울 수 없습니다.

+0

아마도 스크립트 파일이 서버의 사용자 홈 디렉토리에 존재할 것으로 예상됩니다. 그렇지 않은 경우 원격 경로를 조정해야합니다. – tripleee

+0

아마도 bash를 실행할 때 암호를 묻는 메시지가 나왔을 것입니다. 나는 당신과 @pah 솔루션을 시도했지만 유사한 오류와 로그 출력을 얻습니다. 로그를 포함하도록 내 질문을 편집했습니다. –

+0

이것을 달성하기 위해 어떤 기술을 사용해야하는지에 대한 권장 사항이 있습니까? 이 스크립트를 작성할 수 있습니까? bash에 대해 동일한 사용자 이름/암호 조합이 필요한 약 21 대의 컴퓨터에서 bash 스크립트를 실행하려고합니다. –