How to pass value to psftp prompt

微笑、不失礼 提交于 2019-11-30 06:45:52

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 yourhostAddress -hostkey 06:15:d4:3b:e4:e8:23:c0:d6:6d:45:47:7e:bd:8d:74 -l yourusername -pw yourpassword -batch

bergermeister

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

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:\CX\FTP\IG\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 cellxpert-prod@ftp.nadex.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 cellxpert-prod@ftp.nadex.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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!