通过正则表达式排除除 ip 地址以外的所有内容
exclude everything except ip address via regexp
sh ip cef | i 0.0.0.0.*Vlan9
0.0.0.0/0 192.168.18.200 Vlan9
我只想从中取出ip地址,即排除0.0.0.0/0和Vlan9
我通过expect试了一下,但是不知为何不行
(?!Vlan9|0)\d+.\d+.\d+.\d+
expect -exact "#"
send -- "sh ip cef | i 0.0.0.0.*Vlan9 \r"
expect -exact "#"
set dst $expect_out(buffer)
regexp {(?!Vlan9|0)\d+.\d+.\d+.\d+} $dst match ipdst
#expect -exact "#"
#send -- "clear ip arp $ipdst\r"
#expect -exact "#"
puts "router is dst ip $ipdst"
顺便说一句,expect -exact "#" - 根据调试判断,它在单独的行上输出每个字符并进行粘合?
日志
./ssh 192.168.18.200
/*** DATE is Wed Mar 17 12:42:57 MSK 2021 ***/
spawn ssh -o StrictHostKeyChecking=no test@192.168.18.200
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {28141}
expect: does "" (spawn_id exp6) match glob pattern "*assword:"? no
Password:
expect: does "\rPassword: " (spawn_id exp6) match glob pattern "*assword:"? yes
expect: set expect_out(0,string) "\rPassword:"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\rPassword:"
send: sending "123456\r" to { exp6 }
send: sending "sh ip cef | i 0.0.0.0.*Vlan9 \r" to { exp6 }
expect: does " " (spawn_id exp6) match glob pattern "#"? no
expect: does " \r\n" (spawn_id exp6) match glob pattern "#"? no
请尝试:
regexp {(?!Vlan9|0)(\d+\.\d+\.\d+\.\d+)} $dst match ipdst
或:
regexp {(?!Vlan9|0)\d+\.\d+\.\d+\.\d+} $dst ipdst
- 您需要用括号将正则表达式括起来以捕获子匹配项。在上面的第二个语句中,
ipdst
赋值给了整个匹配的子串,这也是有道理的。
- 你需要转义文字点,否则它会匹配任何字符。
[编辑]
如果您的 expect
版本不支持 lookaround assertions
,请试试:
regexp {(\d+\.\d+\.\d+\.\d+)\s+Vlan9} $dst match ipdst
或万不得已:
regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[^0-9]+Vlan9} $dst match ipdst
这将适用于非常旧的 expect
或 tcl
。
[编辑]
这是 ssh
登录交易的示例:
#!/usr/bin/expect
set host "localhost" ;# change to your server name
set user "guest" ;# user name
set pass "secret123" ;# password
spawn ssh $host -l $user
expect "assword:"
send "$pass\n"
expect "Last login:"
expect "$ " ;# command prompt (may differ depending on the system)
send "hostname\n" ;# just an example; replace with your preferred command
expect "\n" ;# newline of the echo back of the sent command
expect "*\n" ;# wait for the server's response
set response $expect_out(0,string)
puts "server's response is $response"
sh ip cef | i 0.0.0.0.*Vlan9
0.0.0.0/0 192.168.18.200 Vlan9
我只想从中取出ip地址,即排除0.0.0.0/0和Vlan9
我通过expect试了一下,但是不知为何不行
(?!Vlan9|0)\d+.\d+.\d+.\d+
expect -exact "#"
send -- "sh ip cef | i 0.0.0.0.*Vlan9 \r"
expect -exact "#"
set dst $expect_out(buffer)
regexp {(?!Vlan9|0)\d+.\d+.\d+.\d+} $dst match ipdst
#expect -exact "#"
#send -- "clear ip arp $ipdst\r"
#expect -exact "#"
puts "router is dst ip $ipdst"
顺便说一句,expect -exact "#" - 根据调试判断,它在单独的行上输出每个字符并进行粘合?
日志
./ssh 192.168.18.200
/*** DATE is Wed Mar 17 12:42:57 MSK 2021 ***/
spawn ssh -o StrictHostKeyChecking=no test@192.168.18.200
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {28141}
expect: does "" (spawn_id exp6) match glob pattern "*assword:"? no
Password:
expect: does "\rPassword: " (spawn_id exp6) match glob pattern "*assword:"? yes
expect: set expect_out(0,string) "\rPassword:"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\rPassword:"
send: sending "123456\r" to { exp6 }
send: sending "sh ip cef | i 0.0.0.0.*Vlan9 \r" to { exp6 }
expect: does " " (spawn_id exp6) match glob pattern "#"? no
expect: does " \r\n" (spawn_id exp6) match glob pattern "#"? no
请尝试:
regexp {(?!Vlan9|0)(\d+\.\d+\.\d+\.\d+)} $dst match ipdst
或:
regexp {(?!Vlan9|0)\d+\.\d+\.\d+\.\d+} $dst ipdst
- 您需要用括号将正则表达式括起来以捕获子匹配项。在上面的第二个语句中,
ipdst
赋值给了整个匹配的子串,这也是有道理的。 - 你需要转义文字点,否则它会匹配任何字符。
[编辑]
如果您的 expect
版本不支持 lookaround assertions
,请试试:
regexp {(\d+\.\d+\.\d+\.\d+)\s+Vlan9} $dst match ipdst
或万不得已:
regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[^0-9]+Vlan9} $dst match ipdst
这将适用于非常旧的 expect
或 tcl
。
[编辑]
这是 ssh
登录交易的示例:
#!/usr/bin/expect
set host "localhost" ;# change to your server name
set user "guest" ;# user name
set pass "secret123" ;# password
spawn ssh $host -l $user
expect "assword:"
send "$pass\n"
expect "Last login:"
expect "$ " ;# command prompt (may differ depending on the system)
send "hostname\n" ;# just an example; replace with your preferred command
expect "\n" ;# newline of the echo back of the sent command
expect "*\n" ;# wait for the server's response
set response $expect_out(0,string)
puts "server's response is $response"