linux下实现ssh的无交互登陆

1.软件安装

yum -y install expect

2.编写脚本

cat >arg_ssh_login.exp <<EOF
#!/usr/bin/expect

set ipaddr [ lindex $argv 0 ]
set user [ lindex $argv 1 ]
set pwd [ lindex $argv 2 ]
set timeout 30
spawn ssh $user@$ipaddr
expect {
"yes/no:" { send "yes\r";exp_continue }
"password:" { send "$pwd\r" }
}
interact
EOF

chmod +x arg_ssh_login.exp

3.运行脚本

./arg_ssh_login.exp 192.168.8.62 root 123456