Expect 安装 on centos7

使用场景

在主机A上编写并且执行Shell脚本,Shell脚本中需要ssh到主机B上执行交互命令。

安装

在主机A上安装expect:

yum install expect

Shell脚本示例

#!/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

猜你喜欢

转载自www.cnblogs.com/yorkwu/p/9260322.html