本文演示如何在CentOS7上安装和使用Expect。

使用场景

在主机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 "yesr"; exp_continue}
        "password:" {send "$passr"}
}
expect "root@*"  {send "df -hr"}
expect "root@*"  {send "exitr"}
expect eof
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!