Posted to tcl by urthmover at Mon Aug 08 21:01:25 GMT 2011view raw
- #!/usr/bin/expect
- # ./sshtest6.sh 'oldpassword' 'newpassword' using IPs or hostnames from hosts.lst in same folder
-
- set f [open hosts.lst r]; # open the file
- set data [read $f]; # read the file content
- close $f; # close the file
- foreach line [split $data \n] { ; # starts a loop over the lines
- if {$line eq {}} continue; # ignore blank lines
- set user root # sets user
- set oldpassword [lindex $argv 0] # sets oldpassword to argv1
- set newpassword [lindex $argv 1] # sets newpassword to argv2
- set cmd "passwd root" # sets cmd to the command to run
- spawn ssh $user@$line # spawns ssh connection
-
- expect continue {send -- yes\r;exp_continue} # if not in known_hosts answer 'yes'
-
- expect ssword # expects ssh password request
- send -- "$oldpassword\r" # sends oldpassword
- send -- "$cmd\r" # sends command to run
- expect "*?ssword:*" # expects New Password:
- send -- "$newpassword\r" # sends newpassword
- expect "*?ssword:*" # expects Retype Password
- send -- "$newpassword\r" # sends newpassword
- send -- "exit\r" # closes ssh connection
- send -- "\r" # additional carriage return for expect
-
- expect eof; # end expect session
- }