编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
安装expect
[14:48:40 root@centos8 ~]#yum install expect -y
[14:49:20 root@centos8 ~]#rpm -qa expect expect-5.45.4-5.el8.x86_64
expect脚本
[15:10:58 root@centos8 ~]#vim remotelogin_expect.sh
#!/usr/bin/expect
spawn ssh root@192.168.31.158
expect {
"*(yes/no*" { send "yes\r";exp_continue }
"*password:*" { send "root123\r";exp_continue }
"*]#" { send "hostname -I\r" }
}
expect eof
shell脚本
[14:50:56 root@centos8 ~]#vim remotelogin_expect.sh
#!/bin/bash
Address=192.168.31.158
User=root
Passwd=root123
expect <<EOF
spawn ssh $User@$Address
expect {
"*(yes/no*" { send "yes\r";exp_continue }
"*password:*" { send "root123\r";exp_continue }
"*]#" { send "hostname -I\r" }
}
expect eof;