-2

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. EDIT: as indicated by Martin below, the below might've not generated the same output but rather not touched the file. Didn't generate the expected results however.

Anything I can do so I can do the needed behavior?

OS is Windows Server 2012 R2.

amyassin
  • 2,714
  • 3
  • 25
  • 32
  • 1
    Did you perhaps miss the `-b` option when you've read through the [help pages](https://the.earth.li/~sgtatham/putty/0.76/htmldoc/Chapter6.html#psftp-option-b) of the `psftp` command? `psftp -b "C:\Users\myuser\Desktop\lscmd.txt"`? – Gerhard Nov 20 '21 at 06:09
  • @MartinPrikryl if I do `type C:/Users/myuser/Desktop/lscmd.txt > C:/Users/myuser/Desktop/ls2.txt psftp -l myusername -pw mycomplexpwd FTPServerHostname` the ls2.txt file contains only `ls`.. File size is about 150 KB.. – amyassin Nov 20 '21 at 08:52
  • @Gerhard did you miss my remark that I tried the `-b` option but didn't work? :) – amyassin Nov 20 '21 at 09:04
  • You should read the content again, you will see more detail on how to create the answer file. – Gerhard Nov 20 '21 at 09:17
  • @MartinPrikryl `lscmd.txt` is 2 bytes, and `ls2.txt` is 4 bytes. – amyassin Nov 20 '21 at 10:31
  • @Gerhard I am getting the needed file when running the command directly on cmd or batch file so I am using the `-b` correctly I believe. The only problem is when using Task Scheduler with user `SYSTEM` -all other users work fine.. – amyassin Nov 20 '21 at 10:39

1 Answers1

-1

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.

EDIT: I believe my biggest problem was getting cmd as SYSTEM so I can debug what is happening, once I could (see the sister serverfault question for how I did it) it became clear to me. For reference and better clarity (since I got downvoted without explanation!), below is the output of the command before adding the key, just masked the sensitive parts, showing that I did get Remote working directory is / message that was in the output file:

C:\Users\myuser>echo ls | psftp -l myftpuser -pw mycomplexpswd sftp_server
The server's host key is not cached. You have no guarantee
that the server is the computer you think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 SHA256: thecomplexfancyhostkey
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n, Return cancels connection, i for more info) 
Using username "myftpuser".
Pre-authentication banner message from server:
| Company FTP Login - Please enter valid credentials to continue
End of banner message from server
Keyboard-interactive authentication prompts from server:
End of keyboard-interactive prompts from server
Remote working directory is /
psftp> quit
amyassin
  • 2,714
  • 3
  • 25
  • 32
  • 1
    But you cannot get *"Remote working directory is /"* if this was the problem. So you didn't give us the correct information. – Martin Prikryl Nov 20 '21 at 19:42
  • @MartinPrikryl I understand why you assume that but that is what I got. I tried it in another server as well and got the same results! Not what I expected as well.. The error was not piped into the file only those lines.. – amyassin Nov 21 '21 at 05:53
  • 1
    You won't get the *"The server's host key is not cached"* to the `ls.txt`, as you are redirecting stdout only, not stderr. That's expected. – But you cannot get *"Remote working directory is /"* either, as you can get that only after you actually connect, what you did not. – Martin Prikryl Nov 21 '21 at 09:20
  • @MartinPrikryl Yes I understand, and I agree with you totally, but that what happened. Anything that might went wrong or different you think? I have not given wrong information, just what I got.. – amyassin Nov 21 '21 at 21:06
  • Now that your SF question was, closed, I'm reposting my comments from there: With input redirection (`<`), you did connect, because the `ls` was taken as a confirmation of the the hostkey prompt (as `n`). And as there were no further commands in the input, nothing else was done. On the contrary with `-b` you won't connect, as there's no response to the prompt. So that means that with `-b` you won't get the *"Remote working directory is /"*, is that correct? Contrary to what your question says. – Martin Prikryl Dec 01 '21 at 06:51
  • @MartinPrikryl I lost access to the test machine I was on so I cannot verify, but logically you are correct. Might be the old file that is result of the `ls` command that wasn't touched and I mistakenly thought it was a new file.. Might be other thing as well.. Will test and confirm asap.. – amyassin Dec 14 '21 at 08:11