bash 脚本将带有密码的 sftp 文件从远程目录传输到本地文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11149296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:33:16  来源:igfitidea点击:

bash script to sftp files with a password from remote directories to local folders

bashsftp

提问by Kuo-Hsien Chang

How to write a bashscript using sftpcommand to download files? I can use the following command to login to the remote machine, however I need to type in the password manually.

如何bash使用sftp命令编写脚本来下载文件?我可以使用以下命令登录远程机器,但是我需要手动输入密码。

bash-3.2$ sftp -o "Port 22022" [email protected]
[email protected]'s password: 
Connected to s-edm-ssh.local.files.stack.com.
sftp>

How to do the sftpwithout the password prompt?

如何做sftp没有密码提示?

If I like to download aaa.txtfile at /remote/mike/filesto my local directory /local/mike/downloaded, how to build a script to do all of these work?

如果我想将aaa.txt文件下载/remote/mike/files到我的本地目录/local/mike/downloaded,如何构建一个脚本来完成所有这些工作?

回答by chrisaycock

Since sftpruns over SSH, you can place your public key on the remote server.

由于sftp通过SSH运行,您可以将公钥放在远程服务器上。

If for some reason you can't place your key on the server, then you can write an Expectscript to sendyour password when the prompt appears. See this example.

如果由于某种原因您不能将您的密钥放在服务器上,那么您可以在出现提示时为您的密码编写一个Expect脚本send。请参阅此示例