pass password to sftp in a bash script

2019-06-06 15:59发布

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?

3条回答
趁早两清
2楼-- · 2019-06-06 16:27

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

查看更多
欢心
3楼-- · 2019-06-06 16:34

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.

查看更多
成全新的幸福
4楼-- · 2019-06-06 16:34

Please try the below steps

lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
查看更多
登录 后发表回答