Running bash script on Linux server using WinSCP .NET assembly from PowerShell script -


i implementing powershell script utilize winscp .net assembly in order access set of linux servers, execute bash script on each server, , copy files. have implemented connection , copy functionality script (verified pulling files each machine), unable execute bash script correctly.

i have looked both executecommand , call commands, receive similar error message of variations have attempted.

error:

winscp.sessionremoteexception: connection has been unexpectedly closed. server sent command exit status 255.
error skipping startup message. shell incompatible application (bash recommended).

would able provide explanation why script connects , copies correctly, unable execute bash script remotely?

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 file , throw on error         $session.getfiles(             ($remotepath + $filename),             ($localpath + $stamp + "\" + $hostdescription + "." + $stamp + "." + $filename)).check()     }         {         # disconnect, clean         $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 . 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 host "x" ssh connection . 2016-08-09 11:43:24.886 connecting x port 22 . 2016-08-09 11:43:24.924 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 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 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 hash sha-256 . 2016-08-09 11:43:25.540 server has ssh-dss host key, don't know . 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 main channel . 2016-08-09 11:43:25.675 opened main channel . 2016-08-09 11:43:25.751 started 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 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 believe server has signed timestamps bug . 2016-08-09 11:43:25.772 use utf-8 strings until server sends invalid utf-8 string sftp version 3 , older utf-8 strings not mandatory . 2016-08-09 11:43:25.772 limiting packet size 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 '.' > 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 '/home/x' . 2016-08-09 11:43:25.792 startup conversation 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 host... . 2016-08-09 11:43:26.547 [shell] looking host "x" ssh connection . 2016-08-09 11:43:26.547 [shell] connecting x port 22 . 2016-08-09 11:43:26.572 [shell] claim version: ssh-2.0-winscp_release_5.9 < 2016-08-09 11:43:26.573 script: connecting 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] 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 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 hash sha-256 . 2016-08-09 11:43:27.222 [shell] server has ssh-dss host key, don't know . 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  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 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 main channel . 2016-08-09 11:43:27.388 [shell] opened main channel . 2016-08-09 11:43:27.481 [shell] started 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 host. < 2016-08-09 11:43:27.481 script: starting 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: end-of-file:0" ! 2016-08-09 11:43:27.661 [shell] can't call method "readline" on undefined value @ /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: 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. shell incompatible application (bash 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 script bash:

$session.executecommand("bash /path/to/encaricva_redhatlinux_collectdata.sh") 

this grant that:

  • you'll be, effectively, using bash call bash script (e.g, if shebang missing).
  • even if there permission issues (executable bit unset), script still executed.

Comments