Posted to tcl by urthmover at Mon Aug 08 21:01:25 GMT 2011view raw

  1. #!/usr/bin/expect
  2. # ./sshtest6.sh 'oldpassword' 'newpassword' using IPs or hostnames from hosts.lst in same folder
  3.  
  4. set f [open hosts.lst r]; # open the file
  5. set data [read $f]; # read the file content
  6. close $f; # close the file
  7. foreach line [split $data \n] { ; # starts a loop over the lines
  8. if {$line eq {}} continue; # ignore blank lines
  9. set user root # sets user
  10. set oldpassword [lindex $argv 0] # sets oldpassword to argv1
  11. set newpassword [lindex $argv 1] # sets newpassword to argv2
  12. set cmd "passwd root" # sets cmd to the command to run
  13. spawn ssh $user@$line # spawns ssh connection
  14.  
  15. expect continue {send -- yes\r;exp_continue} # if not in known_hosts answer 'yes'
  16.  
  17. expect ssword # expects ssh password request
  18. send -- "$oldpassword\r" # sends oldpassword
  19. send -- "$cmd\r" # sends command to run
  20. expect "*?ssword:*" # expects New Password:
  21. send -- "$newpassword\r" # sends newpassword
  22. expect "*?ssword:*" # expects Retype Password
  23. send -- "$newpassword\r" # sends newpassword
  24. send -- "exit\r" # closes ssh connection
  25. send -- "\r" # additional carriage return for expect
  26.  
  27. expect eof; # end expect session
  28. }