tcl - 我是否错过了 exp_continue 的目标?

标签 tcl expect cisco-ios

我正在尝试编写一个expect脚本,该脚本允许我通过ssh连接到交换机,在闪存中查找一组特定的软件,然后将其删除。我想说,如果您看到结果“A”,请执行操作“B”,如果您看到结果“C”,请执行操作“B”。当我运行一个命令时,所有这些结果都会显示。

这是我期望的结果的示例。

B3898_RM23_SW1#dir ?
  /all             List all files
  /recursive       List files recursively
  all-filesystems  List files on all filesystems
  bs:              Directory or file name
  cns:             Directory or file name
  flash1:          Directory or file name
  flash2:          Directory or file name
  flash:           Directory or file name
  null:            Directory or file name
  nvram:           Directory or file name
  system:          Directory or file name
  tar:             Directory or file name
  tmpsys:          Directory or file name
  vb:              Directory or file name
  xmodem:          Directory or file name
  ymodem:          Directory or file name
  <cr>

  B3898_RM23_SW1#

我正在寻找的是“flash1:”目录。现在它们有可能总共有九个闪存文件系统。你将永远拥有“flash1:”;我想要做的是进入每个可能的目录并删除一个文件或整个目录。如果没有其他目录,则可以继续前进。

我想知道的是如何正确使用 exp_continue,或者在这种情况下我是否需要使用它。这是我写的,它会将软件复制到flash1,但如果flas2等存在,它不会将软件复制或删除到其他flash。

send "dir ?\r"
expect {
    -re {\mflash1\M} {
        send "delete /force /recursive flash1:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash1:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {\mflash2\M} {
        send "delete /force /recursive flash2:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash2:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#" 
        }
    -re {\mflash3\M} {
        send "delete /force /recursive flash3:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash3:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {\mflash4\M} {
        send "delete /force /recursive flash4:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash4:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {\mflash5\M} {
        send "delete /force /recursive flash5:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash5:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {mflash6\M} {
        send "delete /force /recursive flash6:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash6:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {\mflash7\M} {
        send "delete /force /recursive flash7:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash7:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {\mflash8\M} {
        send "delete /force /recursive flash8:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash8:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
    -re {\mflash9\M} {
        send "delete /force /recursive flash9:c3750-ipservicesk9-mz.122-55.SE5\r"
        expect "*#"
        send "delete /force /recursive flash9:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
        expect "*#"
        }
}

#This deletes the IOS from a single device, then tftps the IOS to flash. If the TFTP fails it tries one more time.

send "copy tftp: flash1:\r"
expect "Address or name of remote host []?"
send "204.208.204.209\r"
expect "Source filename []?"
send "c3750-ipservicesk9-mz.122-55.SE7.bin\r"
expect "Destination filename"
send "\r"
expect {
    -re {\mtimed out\M} {
        send "copy tftp: flash:\r"
        expect "Address or name of remote host []?"
        send "XXX.XXX.XXX.XXX\r"
        expect "Source filename []?"
        send "c3750-ipservicesk9-mz.122-55.SE7.bin\r"
        expect "Destination filename [c3750-ipservicesk9-mz.122-55.SE7.bin]?"
        send "\r"
        expect "*#"
        }
    -re {\mOK - 13010154 bytes\M}
}


# checks to see if there are other members in the stack, and if there are it will copy the ios from flash1 to the other devices.

send "dir ?\r"
expect {
    -re {\mflash\M} {
        send "\r"
        expect "*#"
        }
    -re {\mflash1\M} {
        send "\r"
        expect "*#"
        }
    -re {\mflash2\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash2:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash3\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash3:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash4\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash4:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash5\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash5:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash6\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash6:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash7\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash7:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash8\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash8:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
    -re {\mflash9\M} {
        send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash9:\r"
        expect "Destination filename"
        send "\r"
        expect "*#"
        }
}

这是我将脚本通过管道传输到文件时的输出...我只捕获第一个循环。

send: sending "dir ?\r" to { exp7 }
Gate keeper glob pattern for '\m(flash[1-9])\M' is 'flash?'. Activating booster.

expect: does "\r\n    6  -rwx       28612  Apr 23 2012 02:35:18 +02:00  config.text.backup\r\n    5  -rwx        1276   Mar 1 1993 01:04:41 +01:00  vlan.dat\r\n    4  -rwx        2404  Jun 17 2013 14:01:30 +02:00  private-config.text\r\n   88  -rwx        2404  Apr 23 2012 02:35:18 +02:00  private-config.text.backup\r\n    8  -rwx       43535  Jun 17 2013 14:01:29 +02:00  config.text\r\n\r\n32514048 bytes total (19417088 bytes free)\r\nB3762_6D205C_SW1&2#" (spawn_id exp7) match regular expression "\m(flash[1-9])\M"? Gate "flash?"? gate=no
"*#"? yes
expect: set expect_out(0,string) "\r\n    6  -rwx       28612  Apr 23 2012 02:35:18 +02:00  config.text.backup\r\n    5  -rwx        1276   Mar 1 1993 01:04:41 +01:00  vlan.dat\r\n    4  -rwx        2404  Jun 17 2013 14:01:30 +02:00  private-config.text\r\n   88  -rwx        2404  Apr 23 2012 02:35:18 +02:00  private-config.text.backup\r\n    8  -rwx       43535  Jun 17 2013 14:01:29 +02:00  config.text\r\n\r\n32514048 bytes total (19417088 bytes free)\r\nB3762_6D205C_SW1&2#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "\r\n    6  -rwx       28612  Apr 23 2012 02:35:18 +02:00  config.text.backup\r\n    5  -rwx        1276   Mar 1 1993 01:04:41 +01:00  vlan.dat\r\n    4  -rwx        2404  Jun 17 2013 14:01:30 +02:00  private-config.text\r\n   88  -rwx        2404  Apr 23 2012 02:35:18 +02:00  private-config.text.backup\r\n    8  -rwx       43535  Jun 17 2013 14:01:29 +02:00  config.text\r\n\r\n32514048 bytes total (19417088 bytes free)\r\nB3762_6D205C_SW1&2#"
send: sending "copy tftp: flash:\r" to { exp7 }

expect: does "" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no
d
expect: does "d" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no
i
expect: does "di" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no
r
expect: does "dir" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no

expect: does "dir " (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no
?
expect: does "dir ?" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no

  /all             List all files
  /recursive       List files recursively
  all-filesystems  List files on all filesystems
  bs:              Directory or file name
  cns:             Directory or file name
  flash1:          Directory or file name
  flash2:          Directory or file name
  flash:           Directory or file name
  null:            Directory or file name
  nvram:           Directory or file name
  system:          Directory or file name
  tar:             Directory or file name
  tmpsys:          Directory or file name
  vb:              Directory or file name
  xmodem:          Directory or file name
  ymodem:          Directory or file name
  <cr>

B3762_6D205C_SW1&2#dir 
expect: does "dir ?\r\n  /all             List all files\r\n  /recursive       List files recursively\r\n  all-filesystems  List files on all filesystems\r\n  bs:              Directory or file name\r\n  cns:             Directory or file name\r\n  flash1:          Directory or file name\r\n  flash2:          Directory or file name\r\n  flash:           Directory or file name\r\n  null:            Directory or file name\r\n  nvram:           Directory or file name\r\n  system:          Directory or file name\r\n  tar:             Directory or file name\r\n  tmpsys:          Directory or file name\r\n  vb:              Directory or file name\r\n  xmodem:          Directory or file name\r\n  ymodem:          Directory or file name\r\n  <cr>\r\n\r\nB3762_6D205C_SW1&2#dir " (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no


expect: does "dir ?\r\n  /all             List all files\r\n  /recursive       List files recursively\r\n  all-filesystems  List files on all filesystems\r\n  bs:              Directory or file name\r\n  cns:             Directory or file name\r\n  flash1:          Directory or file name\r\n  flash2:          Directory or file name\r\n  flash:           Directory or file name\r\n  null:            Directory or file name\r\n  nvram:           Directory or file name\r\n  system:          Directory or file name\r\n  tar:             Directory or file name\r\n  tmpsys:          Directory or file name\r\n  vb:              Directory or file name\r\n  xmodem:          Directory or file name\r\n  ymodem:          Directory or file name\r\n  <cr>\r\n\r\nB3762_6D205C_SW1&2#dir \r\n" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no
Directory of flash:/

    2  -rwx        9240  Jun 17 2013 14:01:30 +02:00  multiple-fs
    3  -rwx    13006601  Apr 24 2012 19:24:08 +02:00  c3750-ipservicesk9-mz.122-55.SE5.bin
    6  -rwx       28612  Apr 23 2012 02:35:18 +02:00  config.text.backup
    5  -rwx        1276   Mar 1 1993 01:04:41 +01:00  vlan.dat
    4  -rwx        2404  Jun 17 2013 14:01:30 +02:00  private-config.text
   88  -rwx        2404  Apr 23 2012 02:35:18 +02:00  private-config.text.backup
    8  -rwx       43535  Jun 17 2013 14:01:29 +02:00  config.text

32514048 bytes total (19417088 bytes free)
B3762_6D205C_SW1&2#
expect: does "dir ?\r\n  /all             List all files\r\n  /recursive       List files recursively\r\n  all-filesystems  List files on all filesystems\r\n  bs:              Directory or file name\r\n  cns:             Directory or file name\r\n  flash1:          Directory or file name\r\n  flash2:          Directory or file name\r\n  flash:           Directory or file name\r\n  null:            Directory or file name\r\n  nvram:           Directory or file name\r\n  system:          Directory or file name\r\n  tar:             Directory or file name\r\n  tmpsys:          Directory or file name\r\n  vb:              Directory or file name\r\n  xmodem:          Directory or file name\r\n  ymodem:          Directory or file name\r\n  <cr>\r\n\r\nB3762_6D205C_SW1&2#dir \r\nDirectory of flash:/\r\n\r\n    2  -rwx        9240  Jun 17 2013 14:01:30 +02:00  multiple-fs\r\n    3  -rwx    13006601  Apr 24 2012 19:24:08 +02:00  c3750-ipservicesk9-mz.122-55.SE5.bin\r\n    6  -rwx       28612  Apr 23 2012 02:35:18 +02:00  config.text.backup\r\n    5  -rwx        1276   Mar 1 1993 01:04:41 +01:00  vlan.dat\r\n    4  -rwx        2404  Jun 17 2013 14:01:30 +02:00  private-config.text\r\n   88  -rwx        2404  Apr 23 2012 02:35:18 +02:00  private-config.text.backup\r\n    8  -rwx       43535  Jun 17 2013 14:01:29 +02:00  config.text\r\n\r\n32514048 bytes total (19417088 bytes free)\r\nB3762_6D205C_SW1&2#" (spawn_id exp7) match glob pattern "Address or name of remote host ?"? no
c

下面是我手动运行所有这些命令的示例。

B3762_6D205C_SW1&2#term length 0
B3762_6D205C_SW1&2#wr
Building configuration...
[OK]
B3762_6D205C_SW1&2#dir flash:
# This first DIR tells me whether or not I have .bin version 5 or version 7. If I have version 7 I exit out of the script and I move on to upgrading another device. If it has version 5 continue on through the script.
Directory of flash:/

    3  -rwx    13006601  Apr 24 2012 19:24:08 +02:00  c3750-ipservicesk9-mz.122-55.SE5.bin
    6  -rwx       28612  Apr 23 2012 02:35:18 +02:00  config.text.backup
    5  -rwx        1276   Mar 1 1993 01:04:41 +01:00  vlan.dat
    4  -rwx        9240  Jun 18 2013 07:44:01 +02:00  multiple-fs
    7  -rwx       43535  Jun 18 2013 07:44:01 +02:00  config.text
   88  -rwx        2404  Apr 23 2012 02:35:18 +02:00  private-config.text.backup
    8  -rwx        2404  Jun 18 2013 07:44:01 +02:00  private-config.text

32514048 bytes total (19417088 bytes free)
# This dir tells me how many flash file systems there are. I want to use this output to determine how many flash file systems I have to delete the old version 5 .bin file from.
B3762_6D205C_SW1&2#dir ?
  /all             List all files
  /recursive       List files recursively
  all-filesystems  List files on all filesystems
  bs:              Directory or file name
  cns:             Directory or file name
  flash1:          Directory or file name
  flash2:          Directory or file name
  flash:           Directory or file name
  null:            Directory or file name
  nvram:           Directory or file name
  system:          Directory or file name
  tar:             Directory or file name
  tmpsys:          Directory or file name
  vb:              Directory or file name
  xmodem:          Directory or file name
  ymodem:          Directory or file name
  <cr>

B3762_6D205C_SW1&2#delete /force /recursive flash1:c3750-ipservicesk9-mz.122-55.SE5.bin
B3762_6D205C_SW1&2#delete /force /recursive flash2:c3750-ipservicesk9-mz.122-55.SE5.bin
B3762_6D205C_SW1&2#copy tftp: flash:
Address or name of remote host []? 204.208.204.209
Source filename []? c3750-ipservicesk9-mz.122-55.SE7.bin
Destination filename [c3750-ipservicesk9-mz.122-55.SE7.bin]?
Accessing tftp://204.208.204.209/c3750-ipservicesk9-mz.122-55.SE7.bin...
Loading c3750-ipservicesk9-mz.122-55.SE7.bin from 204.208.204.209 (via Vlan402): !OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!OO!O!OO!OO!OO!OOO!OO!OO!OO!OO!OO!OO!O!OO!OO!OO!OO!OO!OO!OO!OO
[OK - 13010154 bytes]

13010154 bytes copied in 281.312 secs (46248 bytes/sec)
B3762_6D205C_SW1&2#dir ?
# I want to use this "dir ?" to tell me I how many flash file systems I have to copy the new version 7 flash file system to. 
  /all             List all files
  /recursive       List files recursively
  all-filesystems  List files on all filesystems
  bs:              Directory or file name
  cns:             Directory or file name
  flash1:          Directory or file name
  flash2:          Directory or file name
  flash:           Directory or file name
  null:            Directory or file name
  nvram:           Directory or file name
  system:          Directory or file name
  tar:             Directory or file name
  tmpsys:          Directory or file name
  vb:              Directory or file name
  xmodem:          Directory or file name
  ymodem:          Directory or file name
  <cr>
B3762_6D205C_SW1&2#copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin flash2:
Destination filename [c3750-ipservicesk9-mz.122-55.SE7.bin]?
Copy in progress...CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
13010154 bytes copied in 171.731 secs (75759 bytes/sec)
B3762_6D205C_SW1&2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
B3762_6D205C_SW1&2(config)#boot system switch all flash:c3750-ipservicesk9-mz.122-55.SE7.bin
B3762_6D205C_SW1&2(config)#exit
B3762_6D205C_SW1&2#reload at 02:00 22 June

System configuration has been modified. Save? [yes/no]: yes
Building configuration...
[OK]
Reload scheduled for 02:00:00 CEST Sat Jun 22 2013 (in 90 hours and 3 minutes) by super.poop on vty0 (204.208.XXX.XXX)
Proceed with reload? [confirm]
B3762_6D205C_SW1&2#term length 50
B3762_6D205C_SW1&2#wr mem
Building configuration...
[OK]
B3762_6D205C_SW1&2#exit

这是目前唯一失败的部分!

set DIR {flash2 flash3 flash4 flash5 flash6 flash7 flash8 flash9}
send "dir ?\r"
expect {
    -re {\m(flash[2-9])\M} {
        lappend DIR $expect_out(1,string)
        exp_continue
    }
    "*#"
}
foreach X $DIR {
    send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin $X:\r"
    expect "Destination filename"
    send "\r"
    expect "*#"
}

最佳答案

我会说:

set flash_dirs {}
send "dir ?\r"
expect {
    -re {\m(flash[1-9])\M} {
        lappend flash_dirs $expect_out(1,string)
        exp_continue
    }
    "*#"
}
foreach dir $flash_dirs {
    send "delete /force /recursive $dir:c3750-ipservicesk9-mz.122-55.SE5\r"
    expect "*#"
    send "delete /force /recursive $dir:c3750-ipservicesk9-mz.122-55.SE5.bin\r"
    expect "*#"
}

然后将内容复制到 flash1 中。之后,我假设目录列表没有改变,所以你已经知道安装了哪些闪存驱动器:

foreach dir $flash_dirs {
    if {$dir eq "flash1"} continue
    send "copy flash1:c3750-ipservicesk9-mz.122-55.SE7.bin $dir:\r"
    expect "Destination filename"
    send "\r"
    expect "*#"
}

很多DRY呃。当然未经测试。

关于tcl - 我是否错过了 exp_continue 的目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108715/

相关文章:

lisp - 在 Tcl 中模拟 lisp cons 单元

linux - Multixterm - "can' t 找到包 Expect”

javascript - 利用正则表达式分析Cisco交换机端口信息

Expect Script - 从某一点开始记录,并在某一点停止

xcode - 为未知文件扩展名(例如 ".tcl")向 Xcode 添加语法着色

tcl - 出现错误时从命令行退出 Modelsim

bash - 期待脚本问题

没有交互指令的 Linux 期望命令不起作用

python - Netmiko send_config_set - 解析响应 - 检测错误?

python - 其他优于基于 TCL 的 Expect 的解决方案/语言?