通过expect脚本在机器间跳转

 

expect脚本的用法

首先可以通过 set命令设置一些常量或者从参数中获取一些变量

然后通过spawn执行一个想要的操作,在通过expect命令,当检测到紧跟着的参数字样的输出时,通过send命令交互式的发送想要执行的操作。最后通过interact结束expect脚本对屏幕输出的监控

#!/usr/bin/expect -f
set ip [lindex $argv 0]
set cmd [lindex $argv 1]
set adminpwd ******
set rootpwd ******
spawn sshpass -p 密码 ssh 跳板机ip
​
if { $ip != "" } {
  expect "Select group" { send "0\n"}
  expect "Select page" { send "$ip\n" }
  expect {
    "Select account" { 
      send "2\n"
      expect "Last login" { send "su - admin\n"}
    }
    "Select page" {
    #找一台有权限的机器*.*.*.*
      send "*.*.*.*\n"
      expect "Select account" { send "2\n"}
      expect "Last login" { send "su - admin\n"}
      expect "]" { send "ssh admin@$ip\n"}
      expect {
        "yes/no" {
          send "yes\n"
          expect "password" { send "$adminpwd\n" }
        } 
        "password" { send "$adminpwd\n" }
      }
    }
  }
}
interact

 


 

posted @ 2020-04-09 23:11  一碗正气粥  阅读(237)  评论(0编辑  收藏  举报