在 bash 脚本中将密码传递给 sftp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12508206/
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-09 22:44:08  来源:igfitidea点击:

pass password to sftp in a bash script

bashpasswordssftp

提问by mtk

I would like to automate a bash script, that connects to server using sftp and does a file transfer. I have the password for this, and initially I tried for this

我想自动化一个 bash 脚本,它使用 sftp 连接到服务器并进行文件传输。我有这个密码,最初我试过这个

sftp $acc@$host << EOF
<passwd_here>
cd $dir
get $file
quit
EOF

but it still prompted for password, and I had to enter it manually at the prompt.

但它仍然提示输入密码,我不得不在提示下手动输入。

After searching SO, I found this postwhich had a solution with expect, which I tried and I got the following error:

搜索 SO 后,我发现这篇文章有一个解决方案expect,我尝试过,但出现以下错误:

Script:

脚本:

sftp -b cmdfile.txt $acc@$host
expect "Password:"
send "<passwd>\n";
interact

Error:

错误:

Permission denied (publickey,keyboard-interactive).

cmdfile.txt

cmdfile.txt

cd $dir
get $file
quit

Please let me know, How to connect using the password in a bash script?

请让我知道,如何在 bash 脚本中使用密码进行连接?

采纳答案by Piotr Wadas

With scp/sftp you should use key-based authentication. Public key from the user you want to authenticate copy into ~/.ssh/authorized_keys file on the server, into home directory of user on which you want log on. Storing password in clear text on client side is not a good practice, you know :) That way you "workaround" problem of reading password from the prompt too.

对于 scp/sftp,您应该使用基于密钥的身份验证。来自您要验证的用户的公钥复制到服务器上的 ~/.ssh/authorized_keys 文件中,到您要登录的用户的主目录中。在客户端以明文形式存储密码不是一个好习惯,你知道:) 这样你也可以“解决”从提示中读取密码的问题。

回答by cmdilip

Please try the below steps

请尝试以下步骤

lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--

回答by opaque

Yes key-based auth is the way to go. Check herefor some direction.

是的,基于密钥的身份验证是要走的路。检查这里的一些方向。