【问题标题】:expect regexp example cisco期待正则表达式示例 cisco
【发布时间】:2021-03-16 12:00:16
【问题描述】:

您好,我正在尝试制作以下脚本。

  1. 我们通过 ssh 访问设备 - 它可以工作
  2. 我们执行命令并从该命令中获取必要的数据 通过正则表达式
  3. 我们将接收到的数据写入一个变量
  4. 使用变量执行命令
#!/usr/bin/expect -f

set host [lindex $argv 0]
set user test
set pass 123456

set timeout 5

spawn ssh -o StrictHostKeyChecking=no $user@$host
expect "*assword:" {send "$pass\r"}

expect "*#" {send "sh ip int br | i Vlan9\n"}
regexp {(\d+\.\d+\.\d+\.\d+)} $expect_out(buffer) matched ip
puts "ADDRESS: $ip"

expect eof

但它不起作用..

expect -d ssh 192.168.18.200
expect -d ssh 192.168.18.200
expect version 5.45.4
argv[0] = expect  argv[1] = -d  argv[2] = ssh  argv[3] = 192.168.18.200
set argc 1
set argv0 "ssh"
set argv "192.168.18.200"
executing commands from command file ssh
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 {22907}

expect: does "" (spawn_id exp5) match glob pattern "*assword:"? no
Password:
expect: does "\rPassword: " (spawn_id exp5) match glob pattern "*assword:"? yes
expect: set expect_out(0,string) "\rPassword:"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "\rPassword:"
send: sending "123456\r" to { exp5 }

expect: does " " (spawn_id exp5) match glob pattern "*#"? no


expect: does " \r\n" (spawn_id exp5) match glob pattern "*#"? no
test-gw#
expect: does " \r\ntest-gw#" (spawn_id exp5) match glob pattern "*#"? yes
expect: set expect_out(0,string) " \r\ntest-gw#"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) " \r\ntest-gw#"
send: sending "sh ip int br | i Vlan9\n" to { exp5 }
can't read "ip": no such variable
    while executing
"puts "ADDRESS: $ip""
    (file "ssh" line 17)

在路由器上是这样的

sh ip int br | i Vlan9
Vlan9                  192.168.18.200  YES NVRAM  up                    up

通过send我们执行这个命令,通过正则表达式我们需要把地址192.168.18.200拉出来写入一个变量中以备使用

【问题讨论】:

  • 你能给出合法行和非法行的输入示例吗?这个期望看起来像 TCL,所以你在你的正则表达式中缺少捕获分配
  • expect -d myprog.exp arg ... 运行你的程序——非常详细的调试输出会告诉你为什么你的模式不匹配。
  • 不,你读错了我的回答。把我的正则表达式放在你期望的 -re 正则表达式的地方。 IP 将出现在 expect_out(1,string) 变量中。
  • 是的,我不明白:它工作 - 期望 -exact "#" 发送 -- "sh run | i hostname\r" set name $expect_out(buffer) regexp {([_A-Za-z0-9]+)} $name match hostname puts "$hostname"
  • 它不起作用 - 期望 -exact "#" 发送 -- "sh ip int br | i Vlan9\r" set src $expect_out(buffer) regexp { (\d+\.\d+\.\d+\.\d+)} $src match ipsrc puts "ADDRESS: $ipsrc"

标签: regex expect cisco


【解决方案1】:

我看到 expect 是基于 TCL 的,所以:
首先 - 您的正则表达式有误:大括号内的引号会使正则表达式在输入本身中查找引号。
其次,如果你想对捕获的字符串做一些事情,你需要分配它。 如果不这样做,为什么将大括号放在正则表达式中?
请参阅下面的正则表达式分配示例。

> set line "the ip is 192.35.32.0 bla bla bka"
the ip is 192.35.32.0 bla bla bka
> regexp {(\d+\.\d+\.\d+\.\d+)} $line -> ip
1
> echo $ip
192.35.32.0

所以,当转换为expect 时,expect_out(1,string) 与$ip 相同

EDIT
expect -re {(\d+\.\d+\.\d+\.\d+)} ... 结果将出现在 expect_out(1,string) var

【讨论】:

  • 您能做的最好的事情是编辑您的问题,使其包含几行您希望被接受的行和几行不接受的行。目前,要使代码正常工作,您需要做的就是从正则表达式中删除引号 (")
  • hmm,但是如何让注释中的代码可读???
  • 在 TCL 中我检查过,它可以工作,但在 except 中没有
【解决方案2】:

这就是它的工作原理

expect -exact "#"
send -- "sh ip int br | i Vlan9 \r"
expect -exact "#"
set src $expect_out(buffer)
regexp {(\d+.\d+.\d+.\d+)} $src match ipsrc
expect -exact "#"
send -- "ping $ipsrc\r"

问题来了,如果我们可以在一个脚本中做这样的检查,ping / tracert,端口状态,如何用括号将它们分开,以及如何更正确地使用超时?

expect -exact "#"
send -- "sh int des | i main | Main\r"
expect -exact "#"
set description $expect_out(buffer)
regexp {(Gi0\/1\/0)|(Fa0\/0\/0)|(Fa0)|(Gi0)} $description match interface
expect -exact "#"
send -- "sh int $interface\r"

#expect -exact "#"
#send -- "sh ip int br | i Vlan9 \r"
#expect -exact "#"
#set src $expect_out(buffer)
#regexp {(\d+.\d+.\d+.\d+)} $src match ipsrc
#expect -exact "#"
#send -- "ping $ipsrc\r"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多