#!/opt/sfw/bin/expect # # Script to change passes # Hosts file must be listed in user@host format, one per line. # # Example Usage: # ./passchange hostfile ':pass!@#' ':p455!@#' log_user 0 if {([llength $argv] != 3)} { puts stderr "Usage: $argv0 " puts stderr "" puts stderr " - File of user@host listed, one per line." puts stderr " - Current user pass, specify in single quotes." puts stderr " - New password to change to, specify in single quotes." puts stderr "" exit } set list [lindex $argv 0] set pass [lindex $argv 1] set newpass [lindex $argv 2] set timeout 20 set trylist [open $list r] set hosts [read $trylist] puts "Starting.." foreach entry $hosts { set tmp [split $entry "@"] set var1 [lindex $tmp 0] set var2 [lindex $tmp 1] spawn ssh -l $var1 $var2 expect "(yes/no)?" { send "yes\n" expect "Permanently added" expect "password: $" send "$pass\n" expect "\\$ $" { send "passwd $var1\n" expect -re "password:|Password:" send "$pass\n" expect -re "password:|Password:" { send "$newpass\n" expect -re "password:|Password:" send "$newpass\n" expect "successfully" puts "$var1@$var2 - Successfully changed." send "exit\n" } "less than" { puts "$var1@$var2 - Failed, less than 7 days since last change." send "exit\n" } "wrong passwd" { puts "$var1@$var2 - Failed, wrong password." send "exit\n" } } "$var1" { send "passwd $var1\n" expect -re "password:|Password:" send "$pass\n" expect -re "password:|Password:" { send "$newpass\n" expect -re "password:|Password:" send "$newpass\n" expect "successfully" puts "$var1@$var2 - Successfully changed." send "exit\n" } "less than" { puts "$var1@$var2 - Failed, less than 7 days since last change." send "exit\n" } "wrong passwd" { puts "$var1@$var2 - Failed, wrong password." send "exit\n" } } } -re "password:|Password" { send "$pass\n" expect "\\$ $" { send "passwd $var1\n" expect -re "password:|Password:" send "$pass\n" expect -re "password:|Password:" { send "$newpass\n" expect -re "password:|Password:" send "$newpass\n" expect "successfully" puts "$var1@$var2 - Successfully changed." send "exit\n" } "less than" { puts "$var1@$var2 - Failed, less than 7 days since last change." send "exit\n" } "wrong passwd" { puts "$var1@$var2 - Failed, wrong password." send "exit\n" } } "$var1" { send "passwd $var1\n" expect -re "password:|Password:" send "$pass\n" expect -re "password:|Password:" { send "$newpass\n" expect -re "password:|Password:" send "$newpass\n" expect "successfully" puts "$var1@$var2 - Successfully changed." send "exit\n" } "less than" { puts "$var1@$var2 - Failed, less than 7 days since last change." send "exit\n" } "wrong passwd" { puts "$var1@$var2 - Failed, wrong password." send "exit\n" } } } "\\$ $" { send "passwd $var1\n" expect -re "password:|Password:" send "$pass\n" expect -re "password:|Password:" { send "$newpass\n" expect -re "password:|Password:" send "$newpass\n" expect "successfully" puts "$var1@$var2 - Successfully changed." send "exit\n" } "less than" { puts "$var1@$var2 - Failed, less than 7 days since last change." send "exit\n" } "wrong passwd" { puts "$var1@$var2 - Failed, wrong password." send "exit\n" } } "$var1" { send "passwd $var1\n" expect -re "password:|Password:" send "$pass\n" expect -re "password:|Password:" { send "$newpass\n" expect -re "password:|Password:" send "$newpass\n" expect "successfully" puts "$var1@$var2 - Successfully changed." send "exit\n" } "less than" { puts "$var1@$var2 - Failed, less than 7 days since last change." send "exit\n" } "wrong passwd" { puts "$var1@$var2 - Failed, wrong password." send "exit\n" } } } puts "\n"