How do I use expect to connect via ssh to a system and change the password of the host system?

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I am automating the process of: Connect to a system named "alpha" via ssh with password "alpha" for username "alpha". Once connected I would like to set the root password (to "kickass"). The system I am connecting to doesn't have a root password by default.

I wrote this expect script to do the job but it doesn't work consistently. It works once and then if I change the password to test again, it waits at the "Enter new UNIX password:" prompt after issuing "sudo passwd root". Any ideas?

#! /usr/bin/expect -f set arg1 lindex $argv 0 set force_conservative 1 ;# set to 1 to force conservative mode even if ;# script wasn't run conservatively originally if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg} { sleep .1 exp_send -s -- $arg } } set timeout -1 spawn ssh alpha@$arg1 match_max 100000 expect -exact "password: " send -- "alpha\r" expect -exact "alpha@alpha:~\$ " send -- "sudo passwd root\r" expect -exact "password for alpha: " send -- "alpha\r" expect -exact "new UNIX password: " send -- "kickass\r" expect -exact "Retype new UNIX password: " send -- "kickass\r" expect -exact "alpha@alpha:~\$ " send -- "exit\r" expect eof Thanks. Bash passwords ssh expect passwd link|improve this question asked Dec 30 '10 at 22:41fzkl936 79% accept rate.

It's better, if you can, to use short strings in the expect command. It may make a difference to include only the last few characters of the prompt. – Dennis Williamson Dec 31 '10 at 0:50 Tried this.

Didn't work :(. I can't figure why it works the first time around and then fails if I change the password in the script and run it again. – fzkl Dec 31 '10 at 7:36 2 Maybe sudo works the second time without requesting a password.

– Andrei Sfrent Jan 8 '11 at 20:51.

Expanding on Andrei Sfrent's comment: Sudo normally has some kind of time-out value, so that if you issue a second sudo command within the time-out period you will not be asked for your password again. You could modify the code to something like this: send -- "sudo passwd root\r" expect { -exact "password for alpha: " { send -- "alpha\r" exp_continue } -exact "new UNIX password: " { send -- "kickass\r" } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions