Posted to tcl by schelte at Thu Oct 27 14:28:01 GMT 2011view raw

  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. proc operate {ip routercmd name_pass} {
  17. send_user "telnet to this host: $ip\n"
  18.  
  19. # Connect
  20. spawn telnet $ip
  21.  
  22. expect_before -re {Bad username|Login incorrect} {
  23. send_user -- "$ip: auth problem. last output:\n $expect_out(buffer)\n=========\n"
  24. catch {exp_close}
  25. return
  26. } -re {Unable to connect to remote host|Connection closed by foreign host} {
  27. send_user -- "$ip: connection problem. last output:\n $expect_out(buffer)\n=========\n"
  28. catch {exp_close}
  29. return
  30. } timeout {
  31. send_user "$ip: timeout"
  32. catch {exp_close}
  33. return
  34. } eof {
  35. send_user "$ip: close connection. last output:\n $expect_out(buffer)\n=======\n"
  36. return
  37. }
  38.  
  39. foreach {name pass} $name_pass {
  40. if {$name eq {} || $name eq {}} {
  41. continue
  42. }
  43.  
  44. set done 0
  45.  
  46. # send username & password
  47. expect -re {ogin|sername} {
  48. send -- "$name\r"
  49. exp_continue
  50. } {assword} {
  51. send -- "$pass\r"
  52. }
  53.  
  54. # expect one of these strings and take an action depending which one comes
  55. # replace "Login incorrect" with whatever message your router sends
  56. expect {cli>} {
  57. send "shell\n"
  58.  
  59. # execute command , supposed we r using ash at aztech router
  60. expect -re {#} {
  61. send -- "$routercmd\r"
  62. }
  63. } {Do you want to spawn a shell instead} {
  64. send "y\n"
  65.  
  66. # execute command , supposed we r using ash at aztech router
  67. expect -re {#} {
  68. send -- "$routercmd\r"
  69. }
  70. } {#} {
  71. send -- "$routercmd\r"
  72. }
  73.  
  74. expect {#} {
  75. set done 1
  76. }
  77.  
  78. if {$done} {
  79. break
  80. }
  81. }
  82.  
  83. send_user "end processing host: $ip\n\n"
  84. # close the child process
  85. catch {exp_close}
  86. }
  87.  
  88. foreach ip $ip_list {
  89. if {$ip == ""} {
  90. continue
  91. }
  92. operate $ip $routercmd $name_pass
  93. }
  94.  
  95.