Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

just10minutes

Re: Using WinScp to Run Remote Python Script - But Output is not as expected

martin wrote:

WinSCP considers exit code 1 as a warning only and won't propagate it to WinSCP exit code.
You have to use a higher exit code number.

Note that, if you only execute a remote script, you better use a tool designed for such task, such as PuTTY Plink.


Thank you for the input on Exit code 1 = Just a Warning,
let me try to return higher code if possible on my code.



Coming to usage of putty, I just don't run script, usually I will run a script and produce the output (some Text file), and that output i will get back to my local machine.
martin

Re: Using WinScp to Run Remote Python Script - But Output is not as expected

WinSCP considers exit code 1 as a warning only and won't propagate it to WinSCP exit code.
You have to use a higher exit code number.

Note that, if you only execute a remote script, you better use a tool designed for such task, such as PuTTY Plink.
just10minutes

Using WinScp to Run Remote Python Script - But Output is not as expected

Hi,

I am struggling to understand the concept or what am I doing wrong.

On my Local Machine I run this command using python

command = 'WinSCP.exe /console /log=WinScpLogs.txt /script=scriptFile.txt
runStatus = subprocess.check_output(command, shell=True) #I am expecting exit code != 0 because my remote python command outputs exit code != 0

Content of scriptFile.txt:
option batch abort 

option confirm off
open sftp://user:pass@server/ -hostkey="myHostKey"
option transfer binary
cd "/mypath/"
call  /mypath/myRemotePythonCommand.py
close
exit



Assume my myRemotePythonCommand.py is outputting a exit code != 0 due to some error

But on my "runStatus" I get output as 0 which is not what I am expecting.




To cross verify my script I followed same approach on windows Batch file like below.

WinSCP.exe /console /log=WinScpLogs.txt /script=scriptFile.txt

if %ERRORLEVEL% neq 0 goto error
echo Success
exit /b 0

:error
echo Error!
exit /b 1



This printed me Success Message

So
1. Does this mean if a remote script fails during execution or it exits with a code != 0 winScp won't let me know that my remote script did not run successfully?
2. or Here the %ERRORLEVEL% only relates to how winScp has exited from the process for eg: "call AfileWhichDoesNotExist.py" in this case I saw it produces the exit code != 0


This is the LogMessage: you can see there is ExitCode 1 in the log

. 2017-03-10 12:01:25.125 --------------------------------------------------------------------------

. 2017-03-10 12:01:25.125 WinSCP Version 4.2.7 (Build 758) (OS 6.1.7601 Service Pack 1)
. 2017-03-10 12:01:25.125 Login time: 10 March 2017 12:01:25 PM
. 2017-03-10 12:01:25.125 --------------------------------------------------------------------------
. 2017-03-10 12:01:25.125 Session name: user@server
. 2017-03-10 12:01:25.125 Host name: server (Port: 22)
. 2017-03-10 12:01:25.126 User name: user (Password: Yes, Key file: No)
. 2017-03-10 12:01:25.126 Tunnel: No
. 2017-03-10 12:01:25.126 Transfer Protocol: SFTP
. 2017-03-10 12:01:25.126 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2017-03-10 12:01:25.126 Proxy: none
. 2017-03-10 12:01:25.126 SSH protocol version: 2; Compression: No
. 2017-03-10 12:01:25.126 Bypass authentication: No
. 2017-03-10 12:01:25.126 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2017-03-10 12:01:25.126 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2017-03-10 12:01:25.126 SSH Bugs: -,-,-,-,-,-,-,-,-
. 2017-03-10 12:01:25.126 SFTP Bugs: -,-
. 2017-03-10 12:01:25.126 Return code variable: Autodetect; Lookup user groups: Yes
. 2017-03-10 12:01:25.126 Shell: default, EOL: 0
. 2017-03-10 12:01:25.126 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2017-03-10 12:01:25.126 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2017-03-10 12:01:25.126 Local directory: default, Remote directory: home, Update: No, Cache: Yes
. 2017-03-10 12:01:25.126 Cache directory changes: Yes, Permanent: Yes
. 2017-03-10 12:01:25.126 DST mode: 1
. 2017-03-10 12:01:25.126 --------------------------------------------------------------------------
. 2017-03-10 12:01:25.126 Looking up host "server"
. 2017-03-10 12:01:25.126 Connecting to server port 22
. 2017-03-10 12:01:25.203 Server version: SSH-2.0-OpenSSH_6.6.1
. 2017-03-10 12:01:25.203 Using SSH protocol version 2
. 2017-03-10 12:01:25.203 We claim version: SSH-2.0-WinSCP_release_4.2.7
. 2017-03-10 12:01:25.213 Doing Diffie-Hellman group exchange
. 2017-03-10 12:01:25.267 Doing Diffie-Hellman key exchange with hash SHA-1
. 2017-03-10 12:01:25.347 Host key fingerprint is:
. 2017-03-10 12:01:25.347 ssh-rsa 2048 3e:e8:9e:5e:cc:a8:fe:de:2f:29:42:2f:80:99:fe:9f
. 2017-03-10 12:01:25.347 Initialised AES-256 SDCTR client->server encryption
. 2017-03-10 12:01:25.347 Initialised HMAC-SHA1 client->server MAC algorithm
. 2017-03-10 12:01:25.347 Initialised AES-256 SDCTR server->client encryption
. 2017-03-10 12:01:25.347 Initialised HMAC-SHA1 server->client MAC algorithm
! 2017-03-10 12:01:25.535 Using username "user".
. 2017-03-10 12:01:35.623 Prompt (5, SSH server authentication, Using keyboard-interactive authentication., Password: )
. 2017-03-10 12:01:35.623 Using stored password.
. 2017-03-10 12:01:35.701 Prompt (5, SSH server authentication, , <no prompt>)
. 2017-03-10 12:01:35.701 Ignoring empty SSH server authentication request
. 2017-03-10 12:01:35.717 Access granted
. 2017-03-10 12:01:35.733 Opened channel for session
. 2017-03-10 12:01:35.811 Started a shell/command
. 2017-03-10 12:01:35.811 --------------------------------------------------------------------------
. 2017-03-10 12:01:35.811 Using SFTP protocol.
. 2017-03-10 12:01:35.811 Doing startup conversation with host.
> 2017-03-10 12:01:35.811 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2017-03-10 12:01:35.842 Type: SSH_FXP_VERSION, Size: 150, Number: -1
. 2017-03-10 12:01:35.842 SFTP version 3 negotiated.
. 2017-03-10 12:01:35.842 Unknown server extension posix-rename@openssh.com="1"
. 2017-03-10 12:01:35.842 Unknown server extension statvfs@openssh.com="2"
. 2017-03-10 12:01:35.842 Unknown server extension fstatvfs@openssh.com="2"
. 2017-03-10 12:01:35.842 Unknown server extension hardlink@openssh.com="1"
. 2017-03-10 12:01:35.842 Unknown server extension fsync@openssh.com="1"
. 2017-03-10 12:01:35.842 We believe the server has signed timestamps bug
. 2017-03-10 12:01:35.842 We will use UTF-8 strings for status messages only
. 2017-03-10 12:01:35.842 Limiting packet size to OpenSSH sftp-server limit of 262148 bytes
. 2017-03-10 12:01:35.842 Getting current directory name.
. 2017-03-10 12:01:35.842 Getting real path for '.'
> 2017-03-10 12:01:35.842 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2017-03-10 12:01:35.857 Type: SSH_FXP_NAME, Size: 51, Number: 16
. 2017-03-10 12:01:35.857 Real path is '/home/user'
. 2017-03-10 12:01:35.857 Startup conversation with host finished.
. 2017-03-10 12:01:35.857 Cached directory change via "/mypath//" to "/mypath/".
. 2017-03-10 12:01:35.857 Getting current directory name.
. 2017-03-10 12:01:35.857 [Shell] Looking up host "server"
. 2017-03-10 12:01:35.857 [Shell] Connecting to server port 22
. 2017-03-10 12:01:35.889 [Shell] Server version: SSH-2.0-OpenSSH_6.6.1
. 2017-03-10 12:01:35.889 [Shell] Using SSH protocol version 2
. 2017-03-10 12:01:35.889 [Shell] We claim version: SSH-2.0-WinSCP_release_4.2.7
. 2017-03-10 12:01:35.889 [Shell] Doing Diffie-Hellman group exchange
. 2017-03-10 12:01:35.998 [Shell] Doing Diffie-Hellman key exchange with hash SHA-1
. 2017-03-10 12:01:36.107 [Shell] Host key fingerprint is:
. 2017-03-10 12:01:36.107 [Shell] ssh-rsa 2048 3e:e8:9e:5e:cc:a8:fe:de:2f:29:42:2f:80:99:fe:9f
. 2017-03-10 12:01:36.107 [Shell] Initialised AES-256 SDCTR client->server encryption
. 2017-03-10 12:01:36.107 [Shell] Initialised HMAC-SHA1 client->server MAC algorithm
. 2017-03-10 12:01:36.107 [Shell] Initialised AES-256 SDCTR server->client encryption
. 2017-03-10 12:01:36.107 [Shell] Initialised HMAC-SHA1 server->client MAC algorithm
! 2017-03-10 12:01:36.279 [Shell] Using username "user".
. 2017-03-10 12:01:41.611 [Shell] Prompt (5, SSH server authentication, Using keyboard-interactive authentication., Password: )
. 2017-03-10 12:01:41.611 [Shell] Using stored password.
. 2017-03-10 12:01:41.674 [Shell] Prompt (5, SSH server authentication, , <no prompt>)
. 2017-03-10 12:01:41.674 [Shell] Ignoring empty SSH server authentication request
. 2017-03-10 12:01:41.689 [Shell] Access granted
. 2017-03-10 12:01:41.720 [Shell] Opened channel for session
. 2017-03-10 12:01:41.798 [Shell] Started a shell/command
. 2017-03-10 12:01:41.798 [Shell] --------------------------------------------------------------------------
. 2017-03-10 12:01:41.798 [Shell] Using SCP protocol.
. 2017-03-10 12:01:41.798 [Shell] Doing startup conversation with host.
. 2017-03-10 12:01:41.798 [Shell] Skipping host startup message (if any).
> 2017-03-10 12:01:41.798 [Shell] echo "WinSCP: this is end-of-file:0"
< 2017-03-10 12:01:41.861 [Shell] WinSCP: this is end-of-file:0
. 2017-03-10 12:01:41.861 [Shell] Detecting variable containing return code of last command.
. 2017-03-10 12:01:41.861 [Shell] Trying "$status".
> 2017-03-10 12:01:41.861 [Shell] echo "$status" ; echo "WinSCP: this is end-of-file:0"
< 2017-03-10 12:01:41.954 [Shell] WinSCP: this is end-of-file:0
. 2017-03-10 12:01:41.954 [Shell] The response is not numerical exit code
. 2017-03-10 12:01:41.954 [Shell] Trying "$?".
> 2017-03-10 12:01:41.954 [Shell] echo "$?" ; echo "WinSCP: this is end-of-file:0"
< 2017-03-10 12:01:42.079 [Shell] 0
< 2017-03-10 12:01:42.079 [Shell] WinSCP: this is end-of-file:0
. 2017-03-10 12:01:42.079 [Shell] Return code variable "$?" selected.
. 2017-03-10 12:01:42.079 [Shell] Changing directory to "/mypath/".
> 2017-03-10 12:01:42.079 [Shell] cd "/mypath/" ; echo "WinSCP: this is end-of-file:$?"
< 2017-03-10 12:01:42.173 [Shell] WinSCP: this is end-of-file:0
. 2017-03-10 12:01:42.173 [Shell] Getting current directory name.
> 2017-03-10 12:01:42.173 [Shell] pwd ; echo "WinSCP: this is end-of-file:$?"
< 2017-03-10 12:01:42.235 [Shell] /mypath/
< 2017-03-10 12:01:42.235 [Shell] WinSCP: this is end-of-file:0
. 2017-03-10 12:01:42.235 [Shell] Startup conversation with host finished.
. 2017-03-10 12:01:42.235 Executing user defined command on command session.
> 2017-03-10 12:01:42.235 [Shell]  /mypath//myRemotePythonCommand.py  ; echo "WinSCP: this is end-of-file:$?"
! 2017-03-10 12:01:43.436 [Shell] mkdir: `SomeNewPathDirectory': File exists
! 2017-03-10 12:01:43.826 [Shell] Traceback (most recent call last):
! 2017-03-10 12:01:43.826 [Shell]   File "/mypath//myRemotePythonCommand.py", line 25, in <module>
! 2017-03-10 12:01:43.826 [Shell]     subprocess.check_output(['hdfs', 'dfs', '-mkdir', newfolder])
< 2017-03-10 12:01:43.826 [Shell] SomeNewPathDirectory
! 2017-03-10 12:01:43.826 [Shell]   File "lib/python2.7/subprocess.py", line 574, in check_output
! 2017-03-10 12:01:43.826 [Shell]     raise CalledProcessError(retcode, cmd, output=output)
! 2017-03-10 12:01:43.826 [Shell] subprocess.CalledProcessError: Command '['hdfs', 'dfs', '-mkdir', 'SomeNewPathDirectory']' returned non-zero exit status 1
< 2017-03-10 12:01:43.842 [Shell] WinSCP: this is end-of-file:1
> 2017-03-10 12:01:43.842 [Shell] pwd ; echo "WinSCP: this is end-of-file:$?"
< 2017-03-10 12:01:43.889 [Shell] /mypath/
< 2017-03-10 12:01:43.889 [Shell] WinSCP: this is end-of-file:0
. 2017-03-10 12:01:43.889 Cached directory change via "/mypath/" to "/mypath/".
. 2017-03-10 12:01:43.889 Getting current directory name.
. 2017-03-10 12:01:43.889 Getting current directory name.
. 2017-03-10 12:01:43.889 Closing connection.
. 2017-03-10 12:01:43.889 Sending special code: 12
. 2017-03-10 12:01:43.889 Sent EOF message
. 2017-03-10 12:01:43.889 [Shell] Closing connection.
. 2017-03-10 12:01:43.889 [Shell] Sending special code: 12
. 2017-03-10 12:01:43.889 [Shell] Sent EOF message

[/code]