19

I am creating a script file programmatically and call psftp.exe as follows:

psftp user@hostname.com -pw password -b psftpscript.txt

but it prompts for user input

The server's host key is not cached in the registry. You have no guarantee that the server is the computer you think it is. The server's rsa2 key fingerprint is: [ssh-rsa 1024 somekey] 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)

I need it to be completely prompt free, automatic. I tried -batch parameter but it just abandons the connection

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
coder
  • 4,121
  • 14
  • 53
  • 88

10 Answers10

27

I had the same problem with running a unattended script in Windows Server 2008's 'sandbox' like environment. I ended up running the following which enters the y at the prompt for you:

echo y | psftp user@hostname.com -l username -pw password -b psftpscript.txt

Hope this helps!

Note: I only had to run the echo y once and removing it for the 2nd run didn't ask for the key to be cached anymore.

vmitchell85
  • 478
  • 1
  • 5
  • 12
  • 14
    Note: if you echo `n` instead you will get more consistent behavior. You will still continue with the connection, but the key won't be cached. It will ask you for it every time. – Michael Haren Jun 20 '13 at 14:57
  • 2
    Works as expected an uninterrupted file upload without user interaction. I even tested manually once accepted a key. Script file "echo n | psftp.exe ..." still works. – Whome Mar 24 '14 at 13:36
  • Didn't work for me if I have that echo command in a batch (.bat) file preceding the call to psftp. I run the .bat from the command prompt. – VISQL Jun 08 '15 at 23:42
  • 3
    This solution just skips the security afforded by the key in the first place. See the answer from @ReeveStrife for the best solution. – bob Aug 24 '15 at 14:53
  • Note that context user should have permission to write into registry – Брайков Oct 05 '16 at 06:27
  • 1
    Do not suggest anyone to blindly accept a host key of an SSH server without explaining security consequences! You lose a protection against [MITM attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) by doing so. – Martin Prikryl Mar 09 '18 at 19:27
17

When you run it the first time, it will show you your key for the server. Copy the key and then on your command line, specify your host key like this:

psftp example.com -hostkey 06:15:d4:3b:e4:e8:23:c0:d6:6d:45:47:7e:bd:8d:74 -l yourusername -pw yourpassword -batch
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Gerrie Pretorius
  • 3,381
  • 2
  • 31
  • 34
2

You can create a file as input containing just a y and carriage return then run

psftp user@hostname.com -pw password -b psftpscript.txt < filename.txt

Thanks to James from http://www.sqlservercentral.com/Forums/Topic281954-9-1.aspx for such a simple solution

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
bergermeister
  • 134
  • 2
  • 10
  • 1
    Do not suggest anyone to blindly accept a host key of an SSH server without explaining security consequences! You lose a protection against [MITM attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) by doing so. – Martin Prikryl Mar 09 '18 at 19:29
2

This doesn't answer your question directly, but provides a possible workaround:

Launch a command prompt as the user who will be running your script and manually accept the certificate. Then upon future connections, you won't have the issue.

Given your need, beyond what has been stated, this may or may not work. I came to this question with the same problem and ended up resolving it using the approach I've just described.

matt.bungard
  • 109
  • 1
  • 4
1

In .Net, I found that the above method didn't work quite as expected. The trick was to use .Net's built-in input redirection - and not the < operator. Here's what the code looks like now:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "c:\\psftp.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;            
proc.StartInfo.Arguments = strIP + " -v -l " + strUsername + " -pw " + strPassword + " -b " + strBatchFilename;            
proc.Start();
StreamWriter myStreamWriter = proc.StandardInput;
myStreamWriter.WriteLine("Y\n"); //override the public key question <---
myStreamWriter.Close();
proc.WaitForExit();
proc.Close();
FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
  • 1
    Do not suggest anyone to blindly accept a host key of an SSH server without explaining security consequences! You lose a protection against [MITM attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) by doing so. – Martin Prikryl Mar 09 '18 at 19:29
  • @MartinPrikryl - while you're completely right about this, shouldn't your comment go in the question? isnt this exactly what the asker asked for? – FuzzyAmi Mar 10 '18 at 22:35
  • 1
    OP asked for *"prompt free"* solution. As [@Gerrie's answer](https://stackoverflow.com/a/30378685/850848) shows, there's a prompt free solution, that still secure. – Martin Prikryl Mar 11 '18 at 08:28
1

I had the problem where I didn't have the permission to delete \ rename file over the remote server and the files contains time stamp. So I needed to download files by name. The psftp can't accept params (or any way I was aware of) and I couldn't dynamically change the file names according to the current date.

So, from the batch file which I'm calling the psftp commands I created the commands dynamically and with the file with the relevant time stamp. I could copy only the today files which is better the then copy everything each time.

cd "C:\Files"  
echo cd outbound > C:\SFTP\temp.txt
echo mget file_%date:~10,4%%date:~4,2%%date:~7,2%*.csv >> C:\SFTP\temp.txt
echo quit >> C:\SFTP\temp.txt
echo close >> C:\SFTP\temp.txt
C:\SFTP\psftp user@ftp.address.com -b C:\SFTP\temp.txt
close
exit

The "echo cd outbound > C:\SFTP\temp.txt" cleaned the old file and start writing the content of the new file. The "echo mget file_%date:~10,4%%date:~4,2%%date:~7,2%.csv >> C:\SFTP\temp.txt" resulted in creating the the command: "mget file_20151008.csv " which downloads all files which starts with "file_20151008..." the next 2 rows just ended the action and the line "C:\SFTP\psftp user@ftp.address.com -b C:\SFTP\temp.txt" execute it.

as results temp.txt looks like this:

cd outbound 
mget file_20151008*.csv 
quit 
close 
Gil Allen
  • 1,169
  • 14
  • 24
0

I think it should also be noted that if you are intending to use psftp in an unattended manner that you MUST include the -batch option. This will ensure that psftp does not provide any interactive prompts.

In the event user input would be required, psftp would abort. (e.g. ask user to cache host key, ask for password, etc.)

This will ensure that your process doesn't 'hang' and allow your automation to alert someone that there is an issue.

0

I ended up adding the key to cache by entering 'y' to the prompt. I had to do it only once, and after that no more prompts, it works good.

coder
  • 4,121
  • 14
  • 53
  • 88
  • 1
    -1: this is not fully automatic: prompt is shown first time the connection is open. If somebody happens to clear cache, your program will break. Relying on this not happening is bad! – ya23 Aug 27 '13 at 15:35
  • 1
    @ya23 Verification of the host key must be a manual process. Trying to automate that is bad! Using `-hostkey` switch as seen in the answer by @Gerrie is definitely better than caching the key. But this answer is still better that most the others that blindly accept any host key. – Martin Prikryl Oct 08 '20 at 06:38
-1

I think there is a problem with your command line.

Usage: psftp [options] [user@]host

Try:

psftp -pw password -b psftpscript.txt user@hostname.com
sorcereral
  • 47
  • 1
-1

None of the above suggestions worked for me, but I did figure out something that DID work for me.

I made 2 entries in my batch file for it to work.
The first entry causes a connection to cache in the registry.
The second entry will connect automatically because now the hostkey is cached in the registry.

echo y | psftp.exe username@hostname.com -pw password
psftp.exe username@hostname.com -pw password -v -be -batch -b psftp_upload_command.bat



Zor
  • 11
  • 1