Question

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

Was it helpful?

Solution 7

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.

OTHER TIPS

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.

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

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

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.

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();

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 

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

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



Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top