0

I've searched a lot on the internet but haven't been able to find any useful info this yet. Does PFTP not allow you to run loops like 'IF' and 'WHILE' at all?

If it does, please let me know the syntax, I'm tried of banging my head against it. Annoyingly, PuTTY allows these commands but psftp doesn't seem to even though both are from the same family. I really hope there is a solution to this!

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Chipmunk_da
  • 467
  • 2
  • 9
  • 27
  • no. psftp is not " programming language". if you want loops, you use something ELSE and use that something else to send commands to psftp. – Marc B Oct 28 '16 at 16:39
  • Thanks @MarcB That makes sense. But I'm not sure how to achieve this then. I wanted to use the while loop to check if a particular file has been deleted on the remote server or not, and if it has been then download the log file from the server eg `while [ ! -f /tmp/foo.txt ]; sleep 2; done get FileWriter1.log` – Chipmunk_da Oct 28 '16 at 16:46
  • then don't use psftp. use some other language which supports sftp connections, and do the looping in there. – Marc B Oct 28 '16 at 16:48
  • I don't think the tag [tag:batch-file] applies here as it is Windows-related... – aschipfl Oct 28 '16 at 17:08
  • Thanks @MarcB! This is quite helpful, now I actually understand what PSFTP is lol – Chipmunk_da Oct 29 '16 at 20:08

1 Answers1

0

PSFTP isn't a language. It's just an SFTP client. SFTP itself is just a protocol for moving files between computers. If you have SFTP set up on the remote computer then it suggests that you have SSH running (since SFTP generally comes bundled with the SSH server install).

You can do a test in a bash shell script, for instance, to see if the file exists on the remote server, then execute your psftp command based on the result. Something like:

#!/bin/bash

# test if file exists on remote system
fileExists=$(ssh user@yourothercomputer "test -f /tmp/foo && echo 'true' || echo 'false'")

if $fileExists; then
    psftp <whatever>
fi

You can stick that whole mess in a loop or whatevs. What's happening here is that we are sending a command test -f /tmp/foo && echo 'true' || echo 'false' to the remote computer to execute. The stdout of the command is returned and stored in the variable fileExists. Then we just test it.

If you are in windows you could convert this to a batch script and use plink.exe to send the command kind of like they do here. Or maybe just plop cygwin on your computer with an SSH and SFTP client and use what's above.

The big take-away here is that you will need a separate scripting environment to do the loop and run psftp based on a test.

Community
  • 1
  • 1
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Thanks @JNevill ! This is really helpful, I'll try this out and let you know how it went or bother you for more help if I face any issues :) – Chipmunk_da Oct 29 '16 at 20:07
  • This worked perfectly, thanks 'JNevill! Just 1 issue I faced with this - the username password cannot be supplied within the batch file and has to be done separately on the command line when calling the batch file. I used the following code: `plink username@host -pw password -m "C:\PATH\PlinkTest.bat" -v` – Chipmunk_da Oct 31 '16 at 13:12
  • Also if file exists I want to retrieve a log file from the remote server. I used the following code: `if $fileExists; then "Q:\PuTTY\psftp cd/home/LOGS/" "Q:\PuTTY\psftp get FileLog.log"` but this doesn't work. I'm obviously not passing the commands correctly here but I don't know what the correct syntax is. Can you please help? Also, can you please advice how I can call this recursively, so that if the file doesn't exist, it keep checking until the file appears and then download the log file as above? – Chipmunk_da Oct 31 '16 at 13:26
  • These all sound like separate questions to be asked here on SO. While I'm pretty good with command line scripting in Linux, I'm pretty much useless on windows outside of vbscript and vba. My guess for that psftp command is that you need to do both commands in the same session, though. Maybe something like [in this question](http://stackoverflow.com/questions/8556241/call-multiple-commands-from-powershell-e-g-psftp) – JNevill Oct 31 '16 at 13:30
  • Thanks @JNevill, I checked out that article but seems to be in a different context. I'll post these as separate questions on SO. – Chipmunk_da Oct 31 '16 at 16:29