#!/usr/bin/expect -- set usage "Usage: $argv0 -u user -p password -c command switch\n" set user "manager" set password "procurve" set commands [ list ] log_user 0 set timeout 10 # Process command line for {set i 0} {$i < $argc} {incr i} { set arg [lindex $argv $i] switch -glob -- $arg { -u* { incr i; set user [ lindex $argv $i ] } -p* { incr i; set password [ lindex $argv $i ] } -c* { incr i; lappend commands [ lindex $argv $i ] } -* { puts "\nError: Unknown argument $arg\n" puts $usage exit 1 } default { break } } } if { $i == $argc } { puts "\nError: no switches listed\n" puts $usage exit 1 } proc sendcommand { switch user password commands } { # Connect to switch using ssh spawn ssh $switch -l $user expect_after { timeout { catch { close }; wait; return 1 } eof { catch { close }; wait; return 1 } } # Login and enter configuration mode expect { "(yes/no) " { send "yes\r" expect { "Press any key to continue" { send "\r" expect { "password: " { send "$password\r" expect { "# " { send "config\r" } "password: " { catch { close }; wait; return 1 } } } "# " { send "config\r" } } } "password: " { send "$password\r" expect { "# " { send "config\r" } "password: " { catch { close }; wait; return 1 } } } "# " { send "config\r" } } } "Press any key to continue" { send "\r" expect { "password: " { send "$password\r" expect { "Press any key to continue" { send "\r" expect { "# " { send "config\r" } } } "# " { send "config\r" } "password: " { catch { close }; wait; return 1 } } } "# " { send "config\r" } } } "password: " { send "$password\r" expect { "Press any key to continue" { send "\r" expect { "# " { send "config\r" } } } "# " { send "config\r" } "password: " { catch { close }; wait; return 1 } } } "# " { send "config\r" } } expect "(config)# " # Send the commands foreach command $commands { send "$command\r" expect "(config)# " } # Logout send "logout\r" expect { "\[y/n\]\? " { send "y\r" expect { "\[y/n\]\? " { } "closed." { } } } "closed." { } } } # Iterate over switches foreach switch [lrange $argv $i end] { set switch [string tolower $switch] sendcommand $switch $user $password $commands }