在expect脚本中显示cdp邻居

时间:2012-11-14 17:18:00

标签: regex expect cisco

我对脚本编写很新。我正在编写一个期望脚本,它将ssh'es转换为Cisco交换机,并运行“show cdp neighbors”命令以获取连接到交换机的所有设备的列表。然后我将输出保存到变量并退出ssh会话。 我在包含的文件中设置了用户名和密码。

#!/usr/bin/expect -f
#exp_internal 1

source accountfile
set timeout 10
spawn $env(SHELL)
expect "#"
send "ssh $USERNAME@<hostname>\r"
expect {
  "continue connecting" {
    send_user "Adding host to ssh known hosts list...\n"
    send "yes\n"
    exp_continue
  }
  "Do you want to change the host key on disk" {
    send_user "Changing host key on disk...\n"
    send "yes\n"
    exp_continue
  }
  "assword:" {
    send "$PASSWORD\r"
  }
}
expect "#"
send "term len 0\r"
expect "#"
send "show cdp neighbors\r"
expect "#"
set result $expect_out(buffer)
send "exit\r"
expect "#"

那么我想取$ result并查找包含'R'的行,并将这些行保存到文件中(两边都有空格的R表示路由器,这是我感兴趣的)

问题是如果连接设备的名称很长,它会将设备名称放在一行,然后将下一行设备的其余数据放在一行。因此,如果我匹配'R'字符串,我将无法获得设备的名称,因为该名称位于上一行。

Device ID        Local Intrfce     Holdtme     Capability Platform  Port ID
...
<device_name_really_long>
                 Gig 2/0/52        171             R S I  WS-C6509  Gig 3/14
<device_name2>   Gig 2/0/1         131             H P M  IP Phone  Port 1
...

有什么想法吗?可能有一个正则表达式可以做到,但我不知道蹲下正则表达式。

解决:感谢Glenn Jackman

我最终不得不添加一个期望条件来检查我是否有一个完整的缓冲区,所以我的最终代码如下所示:

#!/usr/bin/expect
#exp_internal 1
match_max 10000
set expect_out(buffer) {}
set timeout 30

source accountfile
spawn $env(SHELL)
expect "#"
send "ssh $USERNAME@ol2110-3750stack.sw.network.local\r"
expect {
    "continue connecting" {
        send_user "Adding host to ssh known hosts list...\n"
        send "yes\n"
        exp_continue
    }
    "Do you want to change the host key on disk" {
        send_user "Changing host key on disk...\n"
        send "yes\n"
        exp_continue
    }
    "assword:" {
        send "$PASSWORD\r"
    }
}
expect "#"
send "term len 0\r"
expect "#"
send "show cdp neighbors\r"
set result ""
expect {
    {full_buffer} {
        puts "====== FULL BUFFER ======"
        append result $expect_out(buffer)
        exp_continue
    }
    "#" {
        append result $expect_out(buffer)
    }
}
send "exit\r"
expect "#"
set devices [list]
set current_device ""
set lines [split $result "\n"]
foreach line $lines {
    set line [string trim $line]
    if {[llength $line] == 1} {
        set current_device $line
        continue
    }
    set line "$current_device$line\n"
    if {[string match {* R *} $line]} {
        lappend devices $line
    }
    set current_device ""
}

puts $devices

1 个答案:

答案 0 :(得分:0)

set devices [list]
set current_device ""
foreach line [split $result \n] {
    if {[llength [split [string trim $line]]] == 1} {
        set current_device $line
        continue
    }
    set line "$current_device$line"
    if {[string match {* R *} $line]} {
        lappend devices $line
    }
    set current_device ""
}

# devices list should now contain "joined" routers.