Guest User

Untitled

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