我需要创建一个自动向OpenSSH ssh客户端输入密码的脚本。

假设我需要使用密码a1234b SSH到myname@somehost。

我已经试过了……

#~/bin/myssh.sh
ssh myname@somehost
a1234b

...但这并不奏效。

如何将此功能放入脚本中?


当前回答

我有一个更好的解决方案,包括登录您的帐户,而不是更改为根用户。 它是一个bash脚本

http://felipeferreira.net/index.php/2011/09/ssh-automatic-login/

其他回答

我设法让它工作了:

SSH_ASKPASS="echo \"my-pass-here\""
ssh -tt remotehost -l myusername

您可以使用expect脚本。我没有写一个在相当长的一段时间,但它应该如下所示。您需要在脚本前面加上#!/usr/bin/expect

#!/usr/bin/expect -f
spawn ssh HOSTNAME
expect "login:" 
send "username\r"
expect "Password:"
send "password\r"
interact
# create a file that echo's out your password .. you may need to get crazy with escape chars or for extra credit put ASCII in your password...
echo "echo YerPasswordhere" > /tmp/1
chmod 777 /tmp/1

# sets some vars for ssh to play nice with something to do with GUI but here we are using it to pass creds.
export SSH_ASKPASS="/tmp/1"
export DISPLAY=YOURDOINGITWRONG
setsid ssh root@owned.com -p 22

参考:https://www.linkedin.com/pulse/youre-doing-wrong-ssh-plain-text-credentials-robert-mccurdy?trk=mp-reader-card

在ubuntu linux -

ssh username@server_ip_address -p port_number

按enter键,然后输入服务器密码

如果您不是root用户,请在命令的开头添加sudo

在下面的例子中,我将写下我使用的解决方案:

场景:我想用sh脚本从服务器复制文件:

#!/usr/bin/expect
$PASSWORD=password
my_script=$(expect -c "spawn scp userName@server-name:path/file.txt /home/Amine/Bureau/trash/test/
expect \"password:\"
send \"$PASSWORD\r\"
expect \"#\"
send \"exit \r\"
")

echo "$my_script"