-2

我需要使用正则表达式从下面的输出中提取“管理 VLAN”的值,并将该值存储在一个变量中,以便稍后在我的脚本中使用。

set switch 100.10.100.100
set Username "test"
set Password "test"

spawn ssh -o StrictHostKeyChecking=no $Username@$switch
expect "*assword: "
send "$Password\r"
expect *>
send "enable\r"
expect "*#"
send "config\r"
expect "(config)#"
send "display something"

输出将是:

status             : Connected 
IP Address         : 2.2.2.2 
Mask               : 255.255.255.255 
Gateway            : 2.2.2.1 
Manage VLAN        : 456 
Manage priority    : 0 
Option60           : No 
Switch             : Enable

我怎样才能做到这一点?

4

1 回答 1

1

参见示例:

expect1.1> spawn bash -c "echo vlan : 1234"
spawn bash -c echo vlan : 1234
22902
expect1.2> expect -re {vlan *: *([0-9]+)}
vlan : 1234
expect1.4> set vlanid $expect_out(1,string)
1234
expect1.5> puts $vlanid
1234
expect1.6>
于 2017-10-13T03:15:51.990 回答