Expect scripts

From Unix Wiki
Jump to: navigation, search

Reset passwords on Solaris using ssh

#!/usr/bin/expect -f
#Usage ./chpwd.exp <user> <password> <host>
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh -q $hostname
#Block in case of successfull root login
expect {
"#" {
send "passwd $username\r"
#No such user block
expect {
"User unknown" {
send_user "\n No such user\n"
exit
}
}
#End of no such user block
#Exist user block
expect {
"New Password:" {
send "$password\r"
expect "Re-enter"
send "$password\r"
expect "successfully"
send "exit\r"
}
}
#End of exist user block
}
#Block in case of unsuccessfull RSA login
"Password:" {
send_user "no RSA-authentication\n"
exit
}
#End of unsuccessfull RSA login block
}
exit

I suggest to modify this script so on as much as possible, but now it works good with RSA "root" authentication