本文演示如何在CentOS7上安装和使用Expect。shell
在主机A上编写而且执行Shell脚本,Shell脚本中须要ssh到主机B上执行交互命令。ssh
在主机A上安装expect:spa
yum install expect
code
#!/usr/bin/expect set ip 192.168.1.102 set pass yourpassword set timeout 30 spawn ssh root@$ip expect { "(yes/no)" {send "yes\r"; exp_continue} "password:" {send "$pass\r"} } expect "root@*" {send "df -h\r"} expect "root@*" {send "exit\r"} expect eof