pass password to sftp in a bash script

不打扰是莪最后的温柔 提交于 2019-12-08 05:11:33

问题


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

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 post which had a solution with expect, which I tried and I got the following error:

Script:

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

Error:

Permission denied (publickey,keyboard-interactive).

cmdfile.txt

cd $dir
get $file
quit

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


回答1:


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.




回答2:


Please try the below steps

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



回答3:


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



来源:https://stackoverflow.com/questions/12508206/pass-password-to-sftp-in-a-bash-script

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