Help with expect script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with expect script
# 1  
Old 05-01-2008
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:


Code:
#!/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)

# 2  
Old 05-01-2008
Try changing:
spawn /usr/bin/passwd $user
to just be:
/usr/bin/passwd $user
# 3  
Old 05-01-2008
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 Smilie
# 4  
Old 05-01-2008
From an old script of mine I used for this:
Code:
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.
# 5  
Old 06-04-2009
Try replacing:
Code:
spawn /usr/bin/passwd $user

with
Code:
send "/usr/bin/passwd $user\r"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

4. UNIX for Advanced & Expert Users

Unable to run the script on remote machine Using Expect script

Not able to execute the file in remote host using except utility I am automating the SFTP keys setp process: So i created the expect script for controlling the output of shell below is my main code: Code: #!/usr/bin/expect set fd set password close $fd set df set app close $df... (1 Reply)
Discussion started by: Manoj Bajpai
1 Replies

5. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

6. Shell Programming and Scripting

Expect script to execute a script on a remote host

Hi, I am new to the expect scripting. I have this expect script as below : spawn ssh remote_server -l id set pass "12345" set opt "s" expect "Password:" {send "$pass\r" ; } expect "*ENTER*" {send "Enter\r"; exp_continue } expect "Please select option :" {send... (2 Replies)
Discussion started by: curt137
2 Replies

7. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

8. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

9. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

10. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies
Login or Register to Ask a Question