通过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
"}
  expect "Select page" { send "$ip
" }
  expect {
    "Select account" { 
      send "2
"
      expect "Last login" { send "su - admin
"}
    }
    "Select page" {
    #找一台有权限的机器*.*.*.*
      send "*.*.*.*
"
      expect "Select account" { send "2
"}
      expect "Last login" { send "su - admin
"}
      expect "]" { send "ssh admin@$ip
"}
      expect {
        "yes/no" {
          send "yes
"
          expect "password" { send "$adminpwd
" }
        } 
        "password" { send "$adminpwd
" }
      }
    }
  }
}
interact

 

原文地址:https://www.cnblogs.com/ZhengQiZHou/p/12670418.html