【问题标题】:Multiples Spawn Session with expect具有期望的倍数生成会话
【发布时间】:2013-10-11 12:17:58
【问题描述】:

我只想做一个脚本,通过 ssh 连接到多个设备(有时是三个设备,有时是十个,...)并在其中执行相同的命令。

例如:

如果我执行./script.sh ipaddress1 ipaddress2 ipaddress5 我想对所有设备(IP 地址 1、2 和 5)执行一个 ssh,以执行一个命令并关闭会话。但我的问题是只有我可以连接到第一台设备。在连接到最后一个设备之前,我该如何做一个循环?

#!/usr/bin/expect -f

set list_ip [lindex $argv 0]

foreach ip $list_ip {
spawn ssh -l NA $ip
  expect "password:"
  send "pepe\n"
  expect "#"
  exp_send "sh linecard all\n\r"
  send "\n"
  send "exit\n"
  send "\n"

expect eof
}
expect "%"

【问题讨论】:

    标签: tcl expect


    【解决方案1】:

    嗯,很简单。遍历argv 列表(所有参数),而不是第一个参数:

    #!/usr/bin/expect -f
    foreach ip $argv {
        spawn ssh -l NA $ip
        expect "password:"
        send "pepe\n"
        expect "#"
        exp_send "sh linecard all\n\r"
        send "\n"
        send "exit\n"
        send "\n"
    
        expect eof
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2023-03-30
      • 2014-05-13
      • 2015-02-03
      • 2011-05-04
      相关资源
      最近更新 更多