Home > front end >  How to script SFTP commands with PSFTP?
How to script SFTP commands with PSFTP?

Time:11-22

I have a script that should list all files in an SFTP server and output that in a file (to be used by another script). The cmd that produces the needed output is:

echo ls | psftp -l myusername -pw mycomplexpwd FTPServerHostname > C:/Users/myuser/Desktop/ls.txt

and this would be invoked by Windows Task Scheduler couple of times a day. For some reason, when scheduling this command (in a .bat file) and running via user SYSTEM, the output file would only contain this:

Remote working directory is /
psftp> quit

While when using another user, the output is as expected (listing of all files) -see https://serverfault.com/questions/1084015/why-psftp-script-is-failing-when-ran-as-system. I need a way to script that and be able to run it as SYSTEM like the rest of my scripts in that system. I've also tried the below:

psftp -l myusername -pw mycomplexpwd FTPServerHostname < C:/Users/myuser/Desktop/lscmd.txt > C:/Users/myuser/Desktop/ls.txt

and:

psftp -l myusername -pw mycomplexpwd FTPServerHostname -b C:/Users/myuser/Desktop/lscmd.txt > C:/Users/myuser/Desktop/ls.txt

where lscmd.txt contains the below:

ls

And the behavior is the same. Anything I can do so I can do the needed behavior?

OS is Windows Server 2012 R2.

CodePudding user response:

So turns out there is an added step of trusting the sFTP server in the first connection (or adding the key to HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys). Check the related serverfault question for (slightly) more details of how I did it. Both the echo ls | psftp -l myusername -pw mycomplexpwd FTPServerHostname > C:/Users/myuser/Desktop/ls.txt and the other variants (e.g. -b) worked afterwards.

  • Related