1830234 Members
2398 Online
109999 Solutions
New Discussion

Re: Expect script help

 
Christine Zhao
Occasional Contributor

Expect script help

I am new to the expect and am trying to write a small script which changes 100 user's password. I want the script read user ID and passwords form 2 files. I can only make the script change 100 user's password to same which is not i want.

Here's my scripts
set password [exec cat /tmp/passwords]
set id [exec cat /tmp/id ]
foreach user $id {
spawn passwd $user
expect "New password:"
send "$password\r"
expect "Re-enter new password:"
send "$password\r"
expect "Passwd successfully changed"
}

Thanks in advance!
Christine
7 REPLIES 7
curt larson_1
Honored Contributor

Re: Expect script help

well, in your foreach loop your always using the same password ($password). you'll need to reference the appropriate password for each user.

given the lack of error checking, probably the quickest way of achiving this is by adding
lreplace $password 0 0 at the end of your loop.
this will remove the first element from the list.

.
.
.
send "$password\r"
expect "Passwd successfully changed"
lreplace $password 0 0
}
Christine Zhao
Occasional Contributor

Re: Expect script help

Thank you for your help! I tried, it's still reference the same password for each user.

Christine
Karthik S S
Honored Contributor

Re: Expect script help

Try,

...
expect "New password:"
send "$password$id\r"
expect "Re-enter new password:"
send "$password$id\r"
....


This ways the password will become $password$id which will be unique for that user.

Regards,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Christine Zhao
Occasional Contributor

Re: Expect script help

No, Still not working! wu wu wu :(
curt larson_1
Honored Contributor

Re: Expect script help

instead of having two files, combine them into a single file

paste -d " " file1 file2 > file3

this will create one file with a single space between the fields

then for your expect script

if {$argc == 0 || $argc > 1} {
send_user "Usage: [file tail $argv0] filename\n"
exit
}

set infile [lindex $argv 0]

if {[file readable $infile] == 0} {
send_user "*** error: cannot read name file \"$infile\", exit\n"
exit
}

while { [gets $infile line] != 1) {
set idpwd [split $line "\ "]
set id [lindex $idpwd 0]
set pwd [lindex $idpwd 1]
send_user "id = $id, passwd = $pwd\n"
}

in the split there is a blank (space) after the \.

this should print each user id and password as it goes through the loop. This way you can make sure the id and password is changing for each iteration. Then you can easily add your code to change the user's password.
Karthik S S
Honored Contributor

Re: Expect script help

What is the length of $password??? If it is 8 or more characted length then adding $id behind $password won't make any sense...

Make $password 5 or less characters then add the user id to it ...

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
curt larson_1
Honored Contributor

Re: Expect script help

oops hopefully you already caught my typo

while { [gets $infile line] != 1) {

the 1 should be a -1 or

while { [gets $infile line] != -1) {