安装expect
[root@localhost ~]# yum -y install expect
查看expect位置
[root@localhost ~]# whereis expect
编辑脚本
[root@localhost ~]# vim /shell/ssh.exp
#!/usr/bin/expect
spawn ssh root@192.168.33.137
expect {
"yes/no" { send "yes\r";exp_continue}
"password" {send "123456\r"}
}
expect "]#"
send "ifconfig ens33"
expect eof
添加执行条件
[root@localhost shell]# chmod +x /shell/ssh.exp
执行测试
[root@localhost shell]# cd /shell/
[root@localhost shell]# ./ssh.exp
如果之前连接过导致连接失败,删除之后再测试
[root@localhost shell]# rm -rf /root/.ssh/known_hosts