Guest User

Untitled

a guest
Dec 4th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.45 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. # router user name
  10. set name "admin"
  11.  
  12. # router password
  13. set pass "1234"
  14.  
  15. # Read command as arg to this script
  16. set routercmd [lindex $argv 0]
  17.  
  18.  
  19. foreach ip $ip_list {
  20.         if {$ip != ""} {
  21.                 send_user "telnet to this host: $ip\n"
  22.  
  23.                 # Connect
  24.                 spawn telnet $ip
  25.  
  26.                 # router user name
  27.                 set name "admin"
  28.  
  29.                 # router password
  30.                 set pass "1234"
  31.  
  32.                 # Read command as arg to this script
  33.                 set routercmd [lindex $argv 0]
  34.  
  35.                 expect_before -re {Bad username|Login incorrect} {
  36.                         send_user -- "$ip: auth problem. last output:\n $expect_out(buffer)\n=========\n"
  37.                         catch {exp_close}
  38.                         continue
  39.                     } -re {Unable to connect to remote host|Connection closed by foreign host} {
  40.                         send_user -- "$ip: connection problem. last output:\n $expect_out(buffer)\n=========\n"
  41.                         catch {exp_close}
  42.                         continue
  43.                     } timeout {
  44.                         send_user "$ip: timeout"
  45.                         catch {exp_close}
  46.                         continue
  47.                     } eof {
  48.                         send_user "$ip: close connection. last output:\n $expect_out(buffer)\n=======\n"
  49.                         continue
  50.                     }
  51.  
  52.                 # send username & password
  53.                 expect -re {ogin|sername} {
  54.                         send -- "$name\r"
  55.                         exp_continue
  56.                     } {assword} {
  57.                         send -- "$pass\r"
  58.                     }
  59.  
  60.                 # expect one of these strings and take an action depending which one comes
  61.                 # replace "Login incorrect" with whatever message your router sends
  62.                 expect {cli>} {
  63.                         send "shell\n"
  64.  
  65.                         # execute command , supposed we r using ash at aztech router
  66.                         expect -re {#} {
  67.                             send -- "$routercmd\r"
  68.                         }
  69.                     }
  70.                 send_user "end processing host: $ip\n\n"
  71.                 # close the child process
  72.                 catch {exp_close}
  73.         }
  74. }
Add Comment
Please, Sign In to add comment