2

スイッチに SSH で接続し、フラッシュ内の特定のソフトウェア セットを探して削除できるようにするスクリプトを作成しようとしています。結果「A」が表示された場合はアクション「B」を実行し、結果「C」が表示された場合はアクション「B」を実行します。1 つのコマンドを実行すると、これらすべての結果が表示されます。

これは、私が期待している結果の例です。

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:」ディレクトリです。現在、合計 9 つのフラッシュ ファイル システムである可能性があります。常に「flash1:」があります。私がやりたいことは、考えられる各ディレクトリに移動し、ファイルまたはディレクトリ全体を削除することです。そして、それらが他のディレクトリでない場合は、先に進みます。

私が知りたいのは、exp_continue を適切に使用する方法、またはこのインスタンスで使用する必要があるかどうかです。これは私が書いたもので、ソフトウェアをフラッシュ1にコピーしますが、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 "*#"
}
4

1 に答える 1

1

I would say:

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 "*#"
}

Then you copy stuff into flash1. After that, I assume the dir list has not changed, so you already know which flash drives are mounted:

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 "*#"
}

Much DRYer. Untested of course.

于 2013-06-14T15:33:07.203 に答える