2.1)ssh实现自动登录,并停在登录服务器上
yuminstall expect -y
[root@node3 ~]# cat ssh.sh
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set password [lindex $argv 1 ]
set timeout 20
spawnssh -p21386 root@$ip
expect {
"*yes/no" { send"yes\r"; exp_continue }
"*password:" { send"$password\r" }
}
interact
[root@node3 ~]# ./ssh.sh 192.168.1.221 renzhiyuan
spawnssh -p21386 root@192.168.1.221
Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.221's password:
Last login: Wed Dec 7 16:43:27 2016 from 192.168.1.217
[root@node3 ~]#
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]//接收第一个参数,并设置IP
set password [lindex $argv 1 ]//接收第二个参数,并设置密码
set timeout 10//设置超时时间
spawnssh root@$ip//发送ssh请滶
expect {//返回信息匹配
"*yes/no" { send"yes\r"; exp_continue}//第一次ssh连接会提示yes/no,继续
"*password:" { send"$password\r" }//出现密码提示,发送密码
}
interact//交互模式,用户会停留在远程服务器上面.
2、2)根据IP和密码连接到不同的机器.
[root@node3 ~]# ./ssh.sh
spawnssh -p21386 root@192.168.1.221
Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.221's password:
Last login: Wed Dec 7 16:43:56 2016 from 192.168.1.217
[root@node3 ~]#
2.3)远程登录到服务器,并且执行命令,执行完后并退出
[root@node3 ~]# ./ssh.sh
spawnssh -p21386 root@192.168.1.221
Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.221's password:
Last login: Wed Dec 7 16:45:33 2016 from 192.168.1.217
[root@HYXD ~]# pwd
/root
[root@HYXD ~]# exit
logout
Connection to 192.168.1.221 closed.
[root@node3 ~]#