Guest User

Untitled

a guest
Dec 3rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.05 KB | None | 0 0
  1. #!/usr/bin/expect -f
  2.  
  3. set timeout 5
  4.  
  5. set load_fh [open "iplist.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 "PASSWORD"
  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 "PASSWORD"
  30.  
  31.                 # Read command as arg to this script
  32.                 set routercmd [lindex $argv 0]
  33.  
  34.                 # send username & password
  35.                 expect {Login:} {
  36.                 send -- "$name\r"
  37.                 } {Unable to connect to remote host} {
  38.                 send_user -- $expect_out(buffer)
  39.                 exp_close
  40.                 continue
  41.                 } timeout {
  42.                 send_user "$ip: timeout"
  43.                 exp_close
  44.                 continue
  45.                 } eof {
  46.                 send_user "$ip: close connection"
  47.                 continue
  48.               }
  49.              
  50.                 expect "Password:"
  51.                 send -- "$pass\r"
  52.  
  53.                 # expect one of these strings and take an action depending which one comes
  54.                 # replace "Login incorrect" with whatever message your router sends
  55.                 expect {
  56.                     "Do you want to spawn a shell instead?" {
  57.                         send "y\n"
  58.  
  59.                         # execute command , supposed we r using ash at aztech router
  60.                         expect "# "
  61.                         send -- "$routercmd\r"
  62.                     }
  63.                     "Login incorrect" {
  64.                         puts "Login to $ip failed!"
  65.                     }
  66.                 }
  67.                 send_user "end processing host: $ip\n\n"
  68.                 # close the child process
  69.                 exp_close
  70.         }
  71. }
Add Comment
Please, Sign In to add comment