&attach_input |
sftp1 |
put foo |
sftp -b sftp2_input [email protected] |
sftp -b sftp2_input [email protected] |
Unfortunately, sftp will just execute all the requests in the input file one after another, there is no mechanism for testing if the transfer worked or not.
Since command macros cannot be used are there any alternatives? The gnu_tools product ships with a program called expect. It can be used to send commands, wait for any number of different responds and do something based on what it sees in the output stream. I am no expect expert but the script in figure 6 will help get you started. If you search the web for “expect scripts” you will find many references that will help you customize my simple example.
# If we get an end-of-file (eof) it means that the sftp process # This procedure does regular expression matches looking for key strings# in the output collected from the command that was execured. In this case # I am just reporting the type of error but other things can be done # as well. I also only check for 2 errors. There are others. You will have # to add them as you find them. # proc checkforerrors {buf cmd} { if [regexp {.*not found} $buf] { puts “$cmd FAILED : not foundnnn” return 1 } if [regexp {.*Permission denied} $buf] { puts “$cmd FAILED : access problemsnnn” return 1 } return 0 }# set the timeout to -1 so there is no timeout. The default is 10 seconds # and most file transfers take longer than that. I decided to set no # timeout, you can change that. set timeout -1 # start sftp # wait for the sftp prompt but if we get an authentication prompt, ending in # change to a convient directory for testing # call check for errors passing it all collected characters up the send “put foor” send “get barr” send “quitr” |
expect sftp3.exp We have connected to the wrong server or the server has been reloaded.The server key must be validated before this script can be run again.ready 10:41:10 |
expect sftp3.exp sftp> get barCouldn’t stat remote file: No such file or directory File “/SysAdmin/Noah_Davids/sftp_test/bar” not found. sftp> get bar FAILED : not foundready 10:42:11 |
Finally, in my FTP blog I mentioned that FTP was able to read files that were still open and this on occasion has resulted in incomplete files being transferred, I suggested that if the FTP macro waits for a file to appear at a location and then transfers it, the macro should check to be sure that it is no longer locked. The same problem can occur with SFTP and the solution is the same. You can place the file lock check in a command macro (figure 9) and then when the file is no longer locked call expect with the appropriate script.
&label AGAIN |