2013-12-12 24 views
0

目前我使用下面的代碼從ssh命令響應,如何使用指望

#!/usr/bin/expect -- 

set timeout 60 

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


spawn ssh [email protected]$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 

現在我想讀的硬件地址,如我我正在使用期望的自動化,是否有任何方法。謝謝

回答

0

我剛剛找到解決方案,並分享給每一個像我一樣受苦的人。
send "show interf\r"
expect -re {HW address:\s+(\S+)}
set hw_addr $expect_out(1,string)
puts "the HW address is $hw_addr"