我正在尝试编写一个expect脚本,允许我ssh到一个开关,在flash中寻找一组软件,然后删除它。我想说,如果你看到结果“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:”目录。现在他们有可能共有九个flash文件系统。你将永远拥有“flash1:”;我想要做的是进入每个可能的目录并删除文件或整个目录。如果他们不是其他目录就可以继续前进。
我想知道的是如何正确使用exp_continue,或者如果我需要在这个实例中使用它。这就是我编写的内容,它会将软件复制到flash1,但如果存在flas2等,则不会将软件复制或删除到其他闪存中。
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 "*#"
}
答案 0 :(得分:1)
我会说:
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呃。当然未经测试。