Expect passwd scripting

I am trying to write an expect script which will read information from a file that contains username and password, and change the password for each user accordingly. The list contains around 100 users. I am new to both Solaris and expect. I have successfully been able to set the first user's password, but not the remaining users. This is the command I pass:

~$ cat userlist.txt | xargs expect expectscript 

Any suggestions would be greatly appreciated.

You may need to have it pass each line from the userlist.txt file instead of all at once.
You can try this
for i in `cat userlist.txt`
do
cat $i | xargs expect expectscript
done

Just a thought.

I tried to use this command, but it doesn't understand the formatting of the userlist file. This is the output I get when executing:

/expecttest: line 3: temp1: command not found
spawn /usr/bin/passwd 
passwd: unknown user 
send: spawn id exp6 not open
    while executing
"send "$password\r""
    (file "expectdev" line 6)
./expecttest: line 3: temp1: command not found
spawn /usr/bin/passwd 
passwd: unknown user 
send: spawn id exp6 not open
    while executing
"send "$password\r""
    (file "expectdev" line 6)
./expecttest: line 3: temp2: command not found
spawn /usr/bin/passwd 
passwd: unknown user 
send: spawn id exp6 not open
    while executing
"send "$password\r""
    (file "expectdev" line 6)
./expecttest: line 3: temp: command not found
spawn /usr/bin/passwd 
passwd: unknown user 
send: spawn id exp6 not open
    while executing
"send "$password\r""
    (file "expectdev" line 6)

And this is what the userlist file looks like:

temp1 temp1 
temp2 temp 

And this is what the expectdev file looks like:

#!/usr/bin/expect
#password automator script
set password [lindex $argv 1]
spawn /usr/bin/passwd [lindex $argv 0]
expect "password:"
send "$password\r"
expect "password:"
send "$password\r"
done
expect eof

Any suggestions would be appreciated. Thanks

Try using a while loop

while read user
do
echo $user | xargs expect expectscript
done < userlist.txt

cheers,
Devaraj Takhellambam

Thanks for your help devtakh. I executed the command, but it returned with errors.

spawn /usr/bin/passwd temp1
Enter new UNIX password: 
Retype new UNIX password: invalid command name "done"
    while executing
"done"
    (file "expectdev" line 9)
spawn /usr/bin/passwd temp2
Enter new UNIX password: 
Retype new UNIX password: invalid command name "done"
    while executing
"done"
    (file "expectdev" line 9)

It appears that it is processing the userlist line by line, but it's not getting the second password for the passwd command.

How would you normally run the expect script and what parameters does it take.

And also, what does the users.txt file contain?

To run the expect script, you must first invoke expect then specify the code you want it to execute.

~$expect codetobeexecuted

The users file contains the following (First column:User, Second Column:Password):

temp1 temp1
temp2 temp2

Thanks

The error is coming from line :9 from the expect script.
You may want to check it and verify it is working correctly..

Also, check if this works:

echo "temp1 temp2" | xargs expect expecdev

cheers,
Devaraj Takhellambam

Line 9 of the expect script contains a "done" statement. Upon commenting the statement out, the script appears to execute properly. Thank you for your help.

The final script that works looks like this:

while read user
do
echo $user | xargs expect expectdev
done < tempusers

The expectdev script:

#!/usr/bin/expect
#password automator script
set password [lindex $argv 1]
spawn /usr/bin/passwd [lindex $argv 0]
expect "password:"
send "$password\r"
expect "password:"
send "$password\r"
#done
expect eof

And the output:

spawn /usr/bin/passwd temp1
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
spawn /usr/bin/passwd temp2
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
spawn /usr/bin/passwd temp3
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

Thank you for your help Devaraj.

you are welcome..I am glad it working now...

cheers,
Devaraj Takhellambam