本文演示如何在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