Expect 安装 on centos7

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

使用场景

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

安装

在主机A上安装expect:spa

yum install expectcode

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
相关文章
相关标签/搜索