none
How to get server response and return status codes in output file using PSFTP.EXE? RRS feed

  • Question

  • I need your help. I've designed a vbscript using PSFTP.EXE which connects to sftp server and uploads a file. Script creates a command input file and executes every command successfully and generates a log. problem here is that I'm unable to capture sftp server response and status with code. Also I want file transfer status and progress status. Below is my script:


    strHostname = "transfer.com"
    strUsername = "User1"
    strPassword = "password"
    strLocalDir = "C:\data"
    strRemoteDir = "Sales"
    PSFTP_DIR = "c:\windows\"
    PSFTP_EXE = PUTTY_DIR & "psftp.exe"
    strFile1="open.txt"
    strFtpScriptFile = "c:\myscript.txt"
    strOutputFile = "C:\output.txt"
    Set WshShell=CreateObject("Wscript.Shell")
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set oFile=fso.OpenTextFile(strFtpScriptFile,2,true)
    oFile.WriteLine "lcd " & chr(34) & strLocalDir & chr(34)
    oFile.WriteLine "cd " & chr(34) & strRemoteDir & chr(34)
    oFile.WriteLine "ls" & chr(34)
    oFile.WriteLine "put " & chr(34) & strFile1 & chr(34)
    oFile.WriteLine "bye" & chr(34) & vbCrLf 
    PSFTP = "cmd /c c:\windows\psftp.exe -v -be -bc " & " -l " & strUsername & " -pw " & strPassword & " " & _
    strHostname & " -b " & strFtpScriptFile & " >> " & strOutputFile & " 2>&1"
    Set oScriptExec = WshShell.Exec(PSFTP)
    oScriptExec.StdOut.ReadAll
    WScript.Echo oScriptExec.StdErr.AtEndOfStream


    • Edited by Puneet Tiwari Sunday, November 9, 2014 9:01 AM spelling mistake
    Sunday, November 9, 2014 9:00 AM

Answers

  • You are going too have to learn how to troubleshoot a script.  I cannot run your environment and I cannot guess at what you are doing.  I have shown you what can be done but it may not work with all utilities.  You will have to try various things to discover where you mistake is.


    ¯\_(ツ)_/¯

    Monday, November 10, 2014 5:59 PM

All replies

  • Here is how to do that.  Do not use CMD.  It will cause the shell to hang.

    Set shell=CreateObject("Wscript.Shell")
    Set exec = shell.Exec("ping localhost -n 5 ")
    While exec.Status = 0
        WScript.Sleep 100
    Wend
    
    If Not exec.StdErr.AtEndOfStream Then
    	WScript.Echo exec.StdErr.ReadAll()
    End If
    
    If Not exec.StdOut.AtEndOfStream Then
    	WScript.Echo exec.Stdout.ReadAll()
    End If
    
    

    Use this command structure and be careful of the format.  Do not use redirectors in the command.

    PSFTP = "c:\windows\psftp.exe" _		
                                    & " -v -be -bc " _
                                  	& " -l " & strUsername _
                                  	& " -pw " & strPassword _
                                  	& " " & strHostname _
                                  	& " -b " & strFtpScriptFile
    
    


    ¯\_(ツ)_/¯

    Sunday, November 9, 2014 10:30 AM
  • REM INSTRUCTIONS: http://test.rebex.net/
    
    strUserName = "demo"
    strPassword = "password"
    strHostname = "test.rebex.net"
    strFtpScriptFile = "c:\scripts\sftp.txt"
    
    REM the following line can be run from console to test
    strtest="c:\test\psftp -v -be -bc -l demo -pw password  test.rebex.net -b c:\scripts\sftp.txt"
    
    
    strPSFTP="c:\test\psftp.exe" _
                                   & " -v" _
                                   & " -be" _
                                    & " -bc" _
                                  	& " -l " & strUsername _
                                  	& " -pw " & strPassword _
                                  	& " " & strHostname _
                                  	& " -b " & strFtpScriptFile
    
    
    Set shell=CreateObject("Wscript.Shell")
    Set exec = shell.Exec(strPSFTP)
    While exec.Status = 0
        WScript.Sleep 1000
        WScript.Echo "Waiting .... "
    Wend
    
    If Not exec.StdErr.AtEndOfStream Then
    	WScript.Echo exec.StdErr.ReadAll()
    End If
    
    If Not exec.StdOut.AtEndOfStream Then
    	WScript.Echo exec.Stdout.ReadAll()
    End If

    Here is a complete working example with a test site for sftp.


    ¯\_(ツ)_/¯




    • Edited by jrv Sunday, November 9, 2014 11:25 AM
    Sunday, November 9, 2014 11:24 AM
  • Code provided by you is generating same output as my code.
    -----------------------------------------------------------------------------------------------------------
    Without your code I am getting below output in log file.
    -----------------------------------------------------------------------------------------------------------
    Looking up host "transfer.com"
    Connecting to <Sample IP> port 22
    Server version: SSH-2.0-Internet Server SSHD
    Using SSH protocol version 2
    We claim version: SSH-2.0-PuTTY_Release_0.63
    Doing Diffie-Hellman group exchange
    Doing Diffie-Hellman key exchange with hash SHA-1
    Host key fingerprint is:
    ssh-rsa 1024 c3:e8:ce:84:c3:97:77:28:62:32:ae:e6:33:e6:47:b8
    Initialised AES-256 SDCTR client->server encryption
    Initialised HMAC-SHA1 client->server MAC algorithm
    Initialised AES-256 SDCTR server->client encryption
    Initialised HMAC-SHA1 server->client MAC algorithm
    Using username "User1".
    This is MFT KIILA QA env DMZ ServerAttempting keyboard-interactive authentication
    Access granted
    Opening session as main channel
    Opened main channel
    Started a shell/command
    Sent EOF message
    Server sent command exit status 0
    Disconnected: All channels closed
    Connected to transfer.com
    Remote working directory is /
    psftp> lcd "C:\"
    New local directory is C:\Sales
    psftp> cd "Sales"
    Remote directory is now /Sales
    psftp> ls"
    Listing directory /Sales/
    d-wx------   1 0        0            4096 Nov 09 14:40 .
    d-wx------   1 0        0            4096 Nov 09 14:40 ..
    psftp> put "ABC_01-11-2014.DAT"
    local:ABC_01-11-2014.DAT => remote:/Sales/ABC_01-11-2014.DAT
    psftp> bye"
    -----------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------
    I need a log as mentioned below:

    18:47:20 Command:open "User1@transfer.com" 22
    18:47:22 Command:Pass: **********
    18:47:22 Status:Connected to transfer.com
    18:47:23 Status:Starting upload of C:\Test.txt
    18:47:23 Command:cd "/Sales"
    18:47:24 Response:New directory is: "/Sales"
    18:47:24 Command:put "C:\Test.txt" "Test.txt"
    18:47:24 Status:local:C:\Test.txt => remote:/Sales/Test.txt
    18:47:24 Status:File transfer successful, transferred 0 bytes in 1 second
    18:47:24 Status:Retrieving directory listing...
    18:47:24 Command:ls
    18:47:24 Status:Listing directory /Sales/
    18:47:26 Status:Directory listing successful

    • Edited by Puneet Tiwari Sunday, November 9, 2014 2:36 PM Updated
    Sunday, November 9, 2014 1:27 PM
  • That is not what your question said.

    To get a log just write the output to a file.


    ¯\_(ツ)_/¯

    Sunday, November 9, 2014 2:42 PM
  • I need a log as mentioned below. Please help me


    18:47:20 Command:open "User1@transfer.com" 22
    18:47:22 Command:Pass: **********
    18:47:22 Status:Connected to transfer.com
    18:47:23 Status:Starting upload of C:\Test.txt
    18:47:23 Command:cd "/Sales"
    18:47:24 Response:New directory is: "/Sales"
    18:47:24 Command:put "C:\Test.txt" "Test.txt"
    18:47:24 Status:local:C:\Test.txt => remote:/Sales/Test.txt
    18:47:24 Status:File transfer successful, transferred 0 bytes in 1 second
    18:47:24 Status:Retrieving directory listing...
    18:47:24 Command:ls
    18:47:24 Status:Listing directory /Sales/
    18:47:26 Status:Directory listing successful

    • Edited by Puneet Tiwari Sunday, November 9, 2014 3:08 PM Extra text added
    Sunday, November 9, 2014 3:06 PM
  • Why?

    The output of PSFTP does not contain that information.


    ¯\_(ツ)_/¯

    Sunday, November 9, 2014 6:13 PM
  • You question is:

    How to get server response and return status codes in output file using PSFTP.EXE?

    The PSFTP output does not contain what you want.  You will have to write a custom logging script to get that.  I recommend using PowerShell as it works better for that kind of thing.


    ¯\_(ツ)_/¯

    Sunday, November 9, 2014 6:44 PM
  • Thank you so much for your response.
    Sunday, November 9, 2014 7:10 PM
  • The best you can do is this.  We cannot read two channels at the same time in script.  That can only be done with a multithreaded compiles program.

    There is some possibility that it can be done in PowerShell.

    strUserName = "demo"
    strPassword = "password"
    strHostname = "test.rebex.net"
    strFtpScriptFile = "c:\scripts\sftp.txt"
    
    '' the following line can be run from console to test
    strtest="c:\test\psftp -v -be -bc -l demo -pw password  test.rebex.net -b c:\scripts\sftp.txt"
    
    
    strPSFTP="c:\test\psftp.exe" _
                                   & " -v" _
                                   & " -be" _
                                    & " -bc" _
                                  	& " -l " & strUsername _
                                  	& " -pw " & strPassword _
                                  	& " " & strHostname _
                                  	& " -b " & strFtpScriptFile
    
    
    Set shell=CreateObject("Wscript.Shell")
    Set exec = shell.Exec(strPSFTP)
    While exec.Status <> 0
        WScript.Sleep 100
        WScript.Echo "Waiting .... "
    Wend
    
    While Not exec.StdErr.AtEndOfStream
    	WScript.Echo "[" & Now & "]" & exec.StdErr.ReadLine()
    Wend
    


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, November 9, 2014 7:34 PM
    Sunday, November 9, 2014 7:34 PM
  • My script (mentioned above) is able to download a file successfully from server, but not uploading file on server, also no error prompted or written in log. Exit code is coming as 0. Which states operation completed successfully, but when I checked path on server file was not there. this. output is listed below:

    I executed code mentioned by you:

    While Not exec.StdErr.AtEndOfStream
    	WScript.Echo "[" & Now & "]" & exec.StdErr.ReadLine()
    Wend

     and in log file I received below error. Whereas the file is no where in use. I tried all extensions like (.csv/.txt/.dat/.xls) but for every file receiving same error. 

    Error if any [10-Nov-14 11:16:51 PM]The process cannot access the file because it is being used by another process.

    Please help as I'm in urgent need of this.

    Monday, November 10, 2014 5:52 PM
  • You are going too have to learn how to troubleshoot a script.  I cannot run your environment and I cannot guess at what you are doing.  I have shown you what can be done but it may not work with all utilities.  You will have to try various things to discover where you mistake is.


    ¯\_(ツ)_/¯

    Monday, November 10, 2014 5:59 PM
  • Ok. 

    Is there any way to call external library in vbscript for secure file transfer? creating object for that library etc. please help

    Monday, November 10, 2014 6:03 PM
  • There are many - you just have to choose one and purchase or download it.

    Try WsFTP

    http://www.ipswitch.com/


    ¯\_(ツ)_/¯

    Monday, November 10, 2014 6:12 PM
  • for testing purpose any freely available library is there?
    Monday, November 10, 2014 6:19 PM
  • You will have to search on you own.


    ¯\_(ツ)_/¯

    Monday, November 10, 2014 6:39 PM
  • Free for non-commercial use: http://www.netcmdlets.com/order/

    $99 for single workstation.


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, November 10, 2014 6:46 PM
    Monday, November 10, 2014 6:45 PM
  • Thanks a ton..... 
    Monday, November 10, 2014 6:46 PM
  • Hi,

    I did lot of research and searched in google too, but no solution to my problem. I have no idea why I'm getting this error. I'm able to download file from ssh/sftp server but can't upload. Even the tool that you suggested "IPSWITCH" also throwing the same error: The process cannot access the file because it is being used by another process. 

    I need your help as sign off date is very near.. It's a humble request that please help me in resolving this issue. I'm using windows 7. I have pasted my script above. Please help.


    PT

    Tuesday, November 11, 2014 6:26 PM
  • I am not sure why you are writing a file when you can just use a redirected in out handle StdIn.

    In any case you cannot open a file then allow another program try to access.  VBScript does not share files.  You must close it before passing it along.

    Try closing your files.


    ¯\_(ツ)_/¯

    Tuesday, November 11, 2014 9:11 PM
  • I need your help as sign off date is very near.. It's a humble request that please help me in resolving this issue. I'm using windows 7. I have pasted my script above. Please help.

    jrv has provided guidance, but I think you need to have appropriate expectations for this forum. The purpose of the forum is to answer scripting questions. We are volunteers, and there is no service-level agreement that guarantees someone will be able to troubleshoot your script, reproduce your environment, and fix it for you.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, November 12, 2014 3:01 AM
    Moderator
  • so, does it mean that a file image is created on local machine when transferring  a file to sftp server?

    But I have already closed a file (oFile.close) before script attempts to upload it on target server.

    1. Is there any other way to close file before uploading?

    2. How to check for below error ?

    Also, I was able to upload same file on FTP server whereas when I tried to upload same file on sftp server it says The process cannot access the file because it is being used by another process. 


    PT

    Wednesday, November 12, 2014 7:54 AM
  • There is no line in your script that closes any files.


    ¯\_(ツ)_/¯

    Wednesday, November 12, 2014 8:06 AM
  • Below is a script where I closed file and same highlighted in Bold.

    strHostname = "transfer.com"
    strUsername = "User1"
    strPassword = "password"
    strLocalDir = "C:\data"
    strRemoteDir = "Sales"
    PSFTP_DIR = "c:\windows\"
    PSFTP_EXE = PUTTY_DIR & "psftp.exe"
    strFile1="open.txt"
    'strGetFile ="abc.txt"
    strFtpScriptFile = "c:\myscript.txt"
    strOutputFile = "C:\output.txt"

    Set WshShell=CreateObject("Wscript.Shell")
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set oFile=fso.OpenTextFile(strFtpScriptFile,2,true)

    oFile.WriteLine "lcd " & chr(34) & strLocalDir & chr(34)
    oFile.WriteLine "cd " & chr(34) & strRemoteDir & chr(34)
    oFile.WriteLine "ls" & chr(34)
    'oFile.WriteLine "get " & chr(34) & strGetFile & chr(34)
    oFile.WriteLine "put " & chr(34) & strFile1 & chr(34)
    oFile.WriteLine "bye" & chr(34) & vbCrLf
    oFile.Close
    Set fso = Nothing
    PSFTP = "C:\windows\psftp.exe -v -be -bc " & " -l " & strUsername & " -pw " & strPassword & " " & _
    strHostname & " -b " & strFtpScriptFile & " >> " & strOutputFile & " 2>&1"
    MsgBox PSFTP
    Set oScriptExec = WshShell.Exec(PSFTP)
    WScript.Echo PSFTP & " >> " & strOutputFile & " 2>&1"
    WScript.Echo Err.Number
    While oScriptExec.Status = 0
        WScript.Sleep 1000
        WScript.Echo "Waiting .... "
        WScript.Echo "[" & Now & "]" & oScriptExec.StdErr.ReadLine()     
    Wend
    WScript.Echo oScriptExec.ExitCode

    Set oFile = Nothing
    'WScript.Echo oScriptExec.StdErr.AtEndOfStream
    WScript.Quit 


    PT

    Wednesday, November 12, 2014 8:59 AM
  • This is getting very annoying.  You keep changing what you are doing then asking for help on an error message.  You also keep breaking everything we give you.

    You are now not redirecting with Exec.  Why use Exec?  You are just adding to you problems.

    Use Run.

    WshShell.Run PSFTP,1.True

    The problem is that you are trying to output to the same file that the PSFTP program is outputting to.  I told you that it wasn't shared.


    ¯\_(ツ)_/¯

    Wednesday, November 12, 2014 9:50 AM
  • Start with this:

    strHostname = "transfer.com"
    strUsername = "User1"
    strPassword = "password"
    strLocalDir = "C:\data"
    strRemoteDir = "Sales"
    PSFTP_DIR = "c:\windows\"
    PSFTP_EXE = PUTTY_DIR & "psftp.exe"
    strFile1="open.txt"
    strFtpScriptFile = "c:\myscript.txt"
    strOutputFile = "C:\output.txt"
    
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set oFile=fso.OpenTextFile(strFtpScriptFile,2,true)
    
    oFile.WriteLine "lcd " & chr(34) & strLocalDir & chr(34)
    oFile.WriteLine "cd " & chr(34) & strRemoteDir & chr(34)
    oFile.WriteLine "ls" & chr(34)
    'oFile.WriteLine "get " & chr(34) & strGetFile & chr(34)
    oFile.WriteLine "put " & chr(34) & strFile1 & chr(34)
    oFile.WriteLine "bye" & chr(34) & vbCrLf 
    oFile.Close
    
    PSFTP = "C:\windows\psftp.exe -v -be -bc " & " -l " & strUsername & " -pw " & strPassword & " " & _
    strHostname & " -b " & strFtpScriptFile & " >> " & strOutputFile
    
    Set WshShell=CreateObject("Wscript.Shell")
    WshShell.Run PSFTP,1,True
    


    ¯\_(ツ)_/¯

    Wednesday, November 12, 2014 9:54 AM
  • I did the same what you have mentioned in above post but same issue.

    PT

    Wednesday, November 12, 2014 11:50 AM
  • I did the same what you have mentioned in above post but same issue.

    PT

    Did you run the exact code I just posted  or did you change it then run it?  You have to run it exactly as it is posted. No changes.


    ¯\_(ツ)_/¯

    Wednesday, November 12, 2014 12:44 PM
  • Executed same code that u posted.. No changes were done. Received same error.

     The process cannot access the file because it is being used by another process. 


    PT

    Wednesday, November 12, 2014 1:06 PM
  • jrv has provided guidance, but I think you need to have appropriate expectations for this forum. The purpose of the forum is to answer scripting questions. We are volunteers, and there is no service-level agreement that guarantees someone will be able to troubleshoot your script, reproduce your environment, and fix it for you.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, November 12, 2014 3:16 PM
    Moderator
  • Executed same code that u posted.. No changes were done. Received same error.

     The process cannot access the file because it is being used by another process. 


    PT

    This can be caused by many things including bugs in PSFTP.  PSFTP was not designed to be used as you are using it which may cause issues.

    You also cannot use this on your command. "" >> " & strOutputFile & " 2>&1"

    I recommend d using PowerShell with the FTP client.  It is easier to manage.

    We cannot troubleshoot your system or process.  If thisis important I recommend hiring a consultant to work with you to resolve this.  Purchasing a scriptable client like WSFtp would be easiest.


    ¯\_(ツ)_/¯

    Wednesday, November 12, 2014 4:19 PM
  • Thanks for I got your point. Thanks to everyone who helped me for this post. Guys, Have a great time..

    PT

    Wednesday, November 12, 2014 4:30 PM
  • At last my script worked as I wanted...

    Thanks  a lot guys....


    PT

    One quick question..

    if i need one more functionality then do I have to submit new question?

    • Edited by Puneet Tiwari Thursday, November 13, 2014 12:49 PM additional query
    Thursday, November 13, 2014 12:48 PM
  • This isn't a script design forum where we add features to your script for you; sorry.

    However, if you have a script question (where you're not asking others to write the code for you), then you can ask, but please be specific about your exact question.

    For help in asking a good question and to avoid asking a bad question, please read the following:


    -- Bill Stewart [Bill_Stewart]

    Thursday, November 13, 2014 3:25 PM
    Moderator