Sponsored Content
Top Forums Shell Programming and Scripting Expect script - going in loops can't stop Post 302588980 by radioactive9 on Tuesday 10th of January 2012 10:54:50 AM
Old 01-10-2012
Error Expect script - going in loops can't stop

Hi

First of all I tried lot of forums to create my first expect script. But I am totally stumped at the last step. I am no Linux Admin or ever trained in Linux. I just tried to create the script logically

History :

I need to to change my password across lot of servers in Linux over ssh

There can be following scenario
1. I have never logged in to the server ever and I am getting RSA Key confirmation to be added YES/NO for the one time
2. I have logged in to the server before and the password is still working and I should be able to issue passwd command to change my password
3. I have logged in to the server before and the password is not working and forcing me to change the password

My two scripts:

1. runip.sh - Simple shell script to run through all the IPs mentioned in iplist.txt

Quote:

#!/bin/bash

for ip in `cat iplist.txt` #All Ips are mentioned in iplist.txt
do
/home/user1/script/change.sh $ip # Calling change script and passing IP as parameter
done
Now comes the trouble script which is going in loops and not able to break out

Quote:

#!/usr/bin/expect -f

set ip [lindex $argv 0] #Catching the ip from shell script
spawn ssh user1@$ip
expect {
"sure you want to continue connecting (yes/no)?" {send "Yes\r" ; exp_continue} # In case of first time login you need to add the token YES/NO bla bla
"user1@$ip's password:" {send "Password\r" ; exp_continue} # IF getting password Prompt entering the current password

# Now two scenario 1. You get bash prompt as you have successfully logged in OR 2. You get change password prompt as your password has already expired so..

"~]$ " {send "passwd\r" ; exp_continue} # Get prompt that we have successfully logged in and sending passwd command to start initiating change of password

# In either scenario above the below code should get executed if I am getting change password prompt immidiately after login as password has expired OR I have started passwd command

"(current) UNIX password:" {send "Password\r" ; exp_continue} # Entering Current Password

"New UNIX password:" {send "P@ssw0rd\r" ; exp_continue} # Entering New password
"Retype new UNIX password:" {send "P@ssw0rd\r" ; exp_continue} # Re-entering New password
"~]$ " {send "exit\r"} # Now this is a trial logically after re-entering password I should get the same prompt back and can exit from here - doesn't work it seems it goes back to the initial prompt and issue passwd command again and goes in loop
eof {break} # Tried this thought it will break no go

sleep 5
exit 0
}
close $spawn_id



I have no clue how to break free and exit from the passwd loop and go to the next IP as in the script Smilie

Any help will be great and I am sure this is a common script in lot of environment helping users to do manually

Usually the bash prompt in environemnt looks like the below

[user1@server1 ~]$
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Expect While Loops - Partial Automation?

I've been reading the O'Reilley expect book and I'm trying to create partial automation for common questions asked on screen in a telnet session and return to interact so the user can resume control. For example while {1} { expect { -re "What color is the sky?" {send... (0 Replies)
Discussion started by: mlarivie
0 Replies

4. Shell Programming and Scripting

multiple while loops in expect script

Hi, I am trying to incorporate multiple while loops into an expect script written in ksh shell. This is on a Solaris 10 system. Here is the code: #!/bin/ksh EXPECT=/usr/local/bin/expect exp_internal i=1 h=0 while ]; do $EXPECT << DONE set stty_init raw ... (1 Reply)
Discussion started by: cic
1 Replies

5. Shell Programming and Scripting

korn shell for loops with expect issue

Hi I have the following Korn script having multiple for loops. #!/bin/ksh EXPECT=/usr/local/bin/expect exp_internal for d in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 i22 23 24 25 26; do for i in 01 02 03 04 05 06 07 ; do for h in 00 01 02 03 04 05 06 07 08 09 10 11 12... (2 Replies)
Discussion started by: cic
2 Replies

6. 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

7. Shell Programming and Scripting

Expect scripting telnet stop on bad username or password

I am trying to build and expect script to log into multiple aix boxes and change password. I need for the script to terminate if it cannot log into a server because the username or password is wrong. #!/usr/bin/expect set timeout 1 set host set user set password set uh "Unknown host" set... (3 Replies)
Discussion started by: leemalloy
3 Replies

8. 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

9. 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

10. UNIX for Advanced & Expert Users

Howto stop loops in CentOS

Good morning, At the client location os is CentOS. In all the terminals i.e F1, F2, F3....F10 PING command is continuously running. I tried to terminate it using CTRL C or quit but unable to stop that command in all the terminals. How to stop that? Howto find batch files which are being... (3 Replies)
Discussion started by: sureshbabu.anis
3 Replies
All times are GMT -4. The time now is 04:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy