【问题标题】:How to get response from ssh command using expect如何使用期望从 ssh 命令获得响应
【发布时间】:2013-12-12 11:00:22
【问题描述】:

目前我正在使用以下代码,

#!/usr/bin/expect --

set timeout 60

set username [lindex $argv 0]
set password [lindex $argv 1]
set ip [lindex $argv 2]


spawn ssh $username@$ip

expect {
        "*yes/no*" { send "yes\r" }
        "?assword:" { send "$password\r" }
}
expect ">"
send "en\r"

expect "#"
send "con t\r"

expect "(config) #"
send "show interf\r"
interact

我在终端有以下结果,

Interface abcdefgh status:
   Comment:            
   Admin up:           XXX
   Link up:            XXX
   DHCP running:       XX
   IP address:         00.00.00.000 /00
   Netmask:            255.255.255.0
   IPv6 enabled:       no
   Speed:              1000Mb/s (auto)
   Duplex:             full (auto)
   Interface type:     abcdefgh
   MTU:                0000
   HW address:         XX:XX:XX:XX:XX:XX

现在我想读取硬件地址,因为我正在使用期望进行自动化,有什么方法可以做到这一点。谢谢

【问题讨论】:

    标签: ssh terminal


    【解决方案1】:

    我刚刚找到了解决办法,分享给所有和我一样受苦的人。
    send "show interf\r"
    expect -re {HW address:\s+(\S+)}
    set hw_addr $expect_out(1,string)
    puts "the HW address is $hw_addr"

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多