本文演示如何在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 "yes\r"; exp_continue}
        "password:" {send "$pass\r"}
}
expect "root@*"  {send "df -h\r"}
expect "root@*"  {send "exit\r"}
expect eof

相关文章:

  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2022-12-23
  • 2021-08-22
  • 2021-06-29
  • 2021-05-27
相关资源
相似解决方案