Posted to tcl by schelte at Thu Oct 27 14:28:01 GMT 2011view raw
- #!/usr/bin/expect -f
- set timeout 5
- set load_fh [open "targets.txt" r]
- set ip_list [split [read $load_fh] "\n"]
- close $load_fh
- set load_fh [open "usersandpass.txt" r]
- set name_pass [split [read $load_fh] "\n"]
- close $load_fh
- # Read command as arg to this script
- set routercmd [lindex $argv 0]
- proc operate {ip routercmd name_pass} {
- send_user "telnet to this host: $ip\n"
- # Connect
- spawn telnet $ip
- expect_before -re {Bad username|Login incorrect} {
- send_user -- "$ip: auth problem. last output:\n $expect_out(buffer)\n=========\n"
- catch {exp_close}
- return
- } -re {Unable to connect to remote host|Connection closed by foreign host} {
- send_user -- "$ip: connection problem. last output:\n $expect_out(buffer)\n=========\n"
- catch {exp_close}
- return
- } timeout {
- send_user "$ip: timeout"
- catch {exp_close}
- return
- } eof {
- send_user "$ip: close connection. last output:\n $expect_out(buffer)\n=======\n"
- return
- }
- foreach {name pass} $name_pass {
- if {$name eq {} || $name eq {}} {
- continue
- }
- set done 0
- # send username & password
- expect -re {ogin|sername} {
- send -- "$name\r"
- exp_continue
- } {assword} {
- send -- "$pass\r"
- }
- # expect one of these strings and take an action depending which one comes
- # replace "Login incorrect" with whatever message your router sends
- expect {cli>} {
- send "shell\n"
- # execute command , supposed we r using ash at aztech router
- expect -re {#} {
- send -- "$routercmd\r"
- }
- } {Do you want to spawn a shell instead} {
- send "y\n"
- # execute command , supposed we r using ash at aztech router
- expect -re {#} {
- send -- "$routercmd\r"
- }
- } {#} {
- send -- "$routercmd\r"
- }
- expect {#} {
- set done 1
- }
- if {$done} {
- break
- }
- }
- send_user "end processing host: $ip\n\n"
- # close the child process
- catch {exp_close}
- }
- foreach ip $ip_list {
- if {$ip == ""} {
- continue
- }
- operate $ip $routercmd $name_pass
- }