如何将值传递给psftp提示符?

时间:2021-08-10 23:41:18

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

我正在以编程方式创建一个脚本文件,并调用psftp。exe如下:

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)

服务器的主机密钥没有缓存在注册表中。您不能保证服务器是您认为的计算机。服务器的rsa2密钥指纹是:[ssh-rsa 1024 somekey]如果你信任这个主机,输入“y”,将密钥添加到PuTTY的缓存中并进行连接。如果您希望只进行一次连接,而不添加缓存的键,则输入“n”。如果您不信任此主机,则按Return键退出连接。在缓存中存储的关键吗?(y / n)

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

我需要它完全的提示*,自动。我尝试了-批处理参数,但它只是放弃了连接。

8 个解决方案

#1


21  

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:

在Windows Server 2008的“沙箱”环境中运行一个无人参与的脚本时,我遇到了同样的问题。最后,我在提示符下输入了以下内容:

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.

注意:我只需要运行echo y一次,然后在第2次运行中删除它,就不再需要缓存的密钥了。

#2


8  

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

你的地址是什么?你的地址是什么?你的地址是什么?

#3


3  

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

您可以创建一个文件作为输入,只包含一个y和回车。

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

感谢詹姆斯从http://www.sqlservercentral.com/Forums/Topic281954-9-1.aspx获得这样一个简单的解决方案。

#4


2  

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:

在。net中,我发现上面的方法并没有达到预期的效果。诀窍是使用。net内置的输入重定向——而不是 <操作符。下面是代码现在的样子:< p>

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

#5


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.

我有一个问题,我没有权限在远程服务器上删除\重命名文件,并且文件包含时间戳。所以我需要按名字下载文件。psftp不能接受params(或者我知道的任何方式),并且不能根据当前日期动态更改文件名。

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.

因此,从我调用psftp命令的批处理文件中,我可以动态地创建命令,并将文件与相关时间戳一起创建。我只能复制今天的文件,这是更好的,然后复制所有的每一次。

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.

“echo cd输出> C:\SFTP\temp”。txt“清理旧文件并开始写入新文件的内容。“回声mget file_ %日期:~ 10,4% %日期:~ 4 2% %日期:~ 7 2%。csv > > C:\ SFTP \ temp。txt创建了命令:mget file_20151008。下载所有以“file_20151008”开头的文件,接下来的两行就结束了动作和行“C:\SFTP\psftp \psftp \ -prod@ftp.nadex.com - bc:\SFTP\temp。txt”执行它。

as results temp.txt looks like this:

因此,txt是这样的:

cd outbound 
mget file_20151008*.csv 
quit 
close 

#6


0  

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

#7


0  

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.

考虑到你的需要,除了已经说明的以外,这可能也可能不起作用。我用同样的问题来解决这个问题,最后用我刚才描述的方法解决了这个问题。

#8


-2  

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.

最后,我通过输入“y”来增加缓存的键值。我只能做一次,在那之后不再有提示了,效果很好。

#1


21  

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:

在Windows Server 2008的“沙箱”环境中运行一个无人参与的脚本时,我遇到了同样的问题。最后,我在提示符下输入了以下内容:

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.

注意:我只需要运行echo y一次,然后在第2次运行中删除它,就不再需要缓存的密钥了。

#2


8  

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

你的地址是什么?你的地址是什么?你的地址是什么?

#3


3  

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

您可以创建一个文件作为输入,只包含一个y和回车。

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

感谢詹姆斯从http://www.sqlservercentral.com/Forums/Topic281954-9-1.aspx获得这样一个简单的解决方案。

#4


2  

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:

在。net中,我发现上面的方法并没有达到预期的效果。诀窍是使用。net内置的输入重定向——而不是 <操作符。下面是代码现在的样子:< p>

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

#5


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.

我有一个问题,我没有权限在远程服务器上删除\重命名文件,并且文件包含时间戳。所以我需要按名字下载文件。psftp不能接受params(或者我知道的任何方式),并且不能根据当前日期动态更改文件名。

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.

因此,从我调用psftp命令的批处理文件中,我可以动态地创建命令,并将文件与相关时间戳一起创建。我只能复制今天的文件,这是更好的,然后复制所有的每一次。

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.

“echo cd输出> C:\SFTP\temp”。txt“清理旧文件并开始写入新文件的内容。“回声mget file_ %日期:~ 10,4% %日期:~ 4 2% %日期:~ 7 2%。csv > > C:\ SFTP \ temp。txt创建了命令:mget file_20151008。下载所有以“file_20151008”开头的文件,接下来的两行就结束了动作和行“C:\SFTP\psftp \psftp \ -prod@ftp.nadex.com - bc:\SFTP\temp。txt”执行它。

as results temp.txt looks like this:

因此,txt是这样的:

cd outbound 
mget file_20151008*.csv 
quit 
close 

#6


0  

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

#7


0  

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.

考虑到你的需要,除了已经说明的以外,这可能也可能不起作用。我用同样的问题来解决这个问题,最后用我刚才描述的方法解决了这个问题。

#8


-2  

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.

最后,我通过输入“y”来增加缓存的键值。我只能做一次,在那之后不再有提示了,效果很好。