Help with expect script

Hey all, I'm very new to expect. I'm trying to automate creating a default password for a user using expect. Here's what I'm trying to do:

Connect to remote systems via ssh. The system has a trusted key, so no password is required for log in.
Invoke the password command for the user
Input password
Input password again
Exit.

I'm not sure why, but when the passwd command invokes, it does so on the local, and not remote host. Looking at the debug (see below script) I can see that I do spawn the ssh session to the remote host, I'm just not sure why the spawn of passwd is happening on the local rather than remote host.

Thanks in advance for the help! Any other comments or advice on making this more robust would be appreciated as well.

Here's the script:


#!/opt/freeware/bin/expect -f
# Useage login site 
if { $argc < 1 } {
       send_user "Usage: prog host user \n"
       exit
}


set host [lindex $argv 0]
set user [lindex $argv 1]
set password testpass
set prompt "# "


spawn ssh $host
expect $prompt
spawn /usr/bin/passwd $user
expect "password:"
send -- "$password\r"
expect "Enter the new password again:"
send -- "$password\r"
send "\n\n"
exit


Here's the debug output

expect version 5.42.1
argv[0] = /opt/freeware/bin/expect  argv[1] = -f  argv[2] = ./setpasshib1  argv[3] = -d  argv[4] = tptestmq2  argv[5] = testuser  
set argc 2
set argv0 "./setpasshib1"
set argv "tptestmq2 testuser"
executing commands from command file ./setpasshib1
spawn ssh tptestmq2
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {21032}

expect: does "" (spawn_id exp4) match glob pattern "# "? no

expect: does "l login: Wed AprLast unsuccessful login: Wed Apr 30 09:16:43 CDT 2008 on ssh from \r\nLast login: Thu May  1 10:52:54 CDT 2008 on /dev/pts/0 from \r\n" (spawn_id exp4) match glob pattern "# "?  May  1 10:52:54no
v/pts/0 from 

expect: does "****************Last unsuccessful login: Wed Apr 30 09:16:43 CDT 2008 on ssh from \r\nLast login: Thu May  1 10:52:54 CDT 2008 on /dev/pts/0 from \r\n*******************************************************************************\r\n*                                                                             *\r\n*                                                                             *\r\n" (spawn_id exp4) match glob pattern "# "?                 no
               *
*                                                                             *

expect: does "Last unsuccessful login: Wed Apr 30 09:16:43 CDT 2008 on ssh from \r\nLast login: Thu May  1 10:52:54 CDT 2008 on /dev/pts/0 from \r\n*******************************************************************************\r\n*                                                                             *\r\n*                                                                             *\r\n*  Welcome to AIX Version 5.3!                                                *\r\n*                                                                             *\r\n*                                                                             *\r\n*  Please see the README file in /usr/lpp/bos for information pertinent to    *\r\n*  this release of the AIX Operating System.                                  *\r\n*                                                                             *\r\n*                                                                             *\r\n*******************************************************************************\r\n" (spawn_id exp4) match glob pattern "# "? no
*  Welcome to AIX Version 5.3!                                                *
*                                                                             *
*                                                                             *
*  Please see the README file in /usr/lpp/bos for information pertinent to    *
*  this release of the AIX Operating System.                                  *
*                                                                             *
*                                                                             *
*******************************************************************************
tptestmq2:/# Last unsuccessful login: Wed Apr 30 09:16:43 CDT 2008 on ssh from \r\nLast login: Thu May  1 10:52:54 CDT 2008 on /dev/pts/0 from \r\n*******************************************************************************\r\n*                                                                             *\r\n*                                                                             *\r\n*  Welcome to AIX Version 5.3!                                                *\r\n*                                                                             *\r\n*                                                                             *\r\n*  Please see the README file in /usr/lpp/bos for information pertinent to    *\r\n*  this release of the AIX Operating System.                                  *\r\n*                                                                             *\r\n*                                                                             *\r\n*******************************************************************************\r\ntptestmq2:/# " (spawn_id exp4) match glob pattern "# "? yes
expect: set expect_out(0,string) "# "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Last unsuccessful login: Wed Apr 30 09:16:43 CDT 2008 on ssh from \r\nLast login: Thu May  1 10:52:54 CDT 2008 on /dev/pts/0 from \r\n*******************************************************************************\r\n*                                                                             *\r\n*                                                                             *\r\n*  Welcome to AIX Version 5.3!                                                *\r\n*                                                                             *\r\n*                                                                             *\r\n*  Please see the README file in /usr/lpp/bos for information pertinent to    *\r\n*  this release of the AIX Operating System.                                  *\r\n*                                                                             *\r\n*                                                                             *\r\n*******************************************************************************\r\ntptestmq2:/# "
spawn /usr/bin/passwd testuser
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {41754}

expect: does "" (spawn_id exp5) match glob pattern "password:"? no

expect: does "3004-687 User "testuser" does not exist.\r\n" (spawn_id exp5) match glob pattern "password:"? no
3004-687 User "testuser" does not exist.
expect: read eof
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "3004-687 User "testuser" does not exist.\r\n"
send: sending "testpass\r" to { exp5 send: spawn id exp5 not open
    while executing
"send -- "$password\r""
    (file "./setpasshib1" line 19)

Try changing:
spawn /usr/bin/passwd $user
to just be:
/usr/bin/passwd $user

Thanks, but that didn't work, just gave me the error:

invalid command name "/usr/bin/passwd"
while executing
"/usr/bin/passwd $user"
(file "./setpasshib1" line 17)

I do appreciate the input though :slight_smile:

From an old script of mine I used for this:

spawn ssh $username@$name
expect {
                                     -re $prompt {
                                                     send_user "Logged in to host: $name as $username\n"
                                                     send "passwd\r\n"
                                                     expect -re ".*asswor.*" {
                                                               set new [getInput "Password change for $username on $name: "]
                                                                send "$new\r\n"
                                                                expect -re "\[Rr\]e.*asswor.*" {
                                                                          send "$new\r\n"
                                                                          expect  -re "$prompt" {
                                                                                  send_user "Password changed successfully for $name\n"
                                                                                                             }
                                                                                      }
                                                             }
                                                }

HTH.

Try replacing:

spawn /usr/bin/passwd $user

with

send "/usr/bin/passwd $user\r"