Expect Scripting - Using the "interact" command? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Expect Scripting - Using the "interact" command?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2012
mrm5102 mrm5102 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 1:18 PM EDT
Posts: 131
Thanks: 66
Thanked 8 Times in 7 Posts
Expect Scripting - Using the "interact" command?

Hello All,

I am writing an Expect Script to execute some commands over ssh then exit the script.
The script works just fine if I automate everything and assuming the correct password was entered.

So this Expect Script gets executed from a Bash script... From the Bash script I pass along an IP Address,
then "I" prompt them for a Username and a Password from within the Bash script, then pass it along to the
Expect Script as arguments. If the password entered is correct, the script runs smoothly without any problems.
But I was trying to add a section in the script that, if the password is incorrect, then I wanted to issue the
"interact" command and have the user try again until either they get the password correct or until they surpass
the amount of allowed retries.

I was trying to accomplish this with a while loop but couldn't seem to get it working correctly.

Here's what I've tried so far: This example doesn't have the while loop...

Code:
set force_conservative 0  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}


#get command line arguments
set ip_address [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]

#Set the timeout limit
set timeout 5

#telnet to the router
spawn /usr/bin/ssh $username@$ip_address
sleep 1

expect_after {
    timeout {
       exit 100
    }
}

expect {
    "*assword:"    { send "$password\r" }
    timeout        { exit 1 }
}
expect {
    # If we find the "Password:" Prompt AGAIN, then pass control to the user to enter a new password...
    # interact command --> Pass control back to the Expect Script when the user hits "Enter/Return" Key --> i.e. "\r"
    "*assword:"    { interact -o "\r" return ; exp_continue }    # <--- INTERACT COMMAND

    # If we find a line with a "#" or ">" then we are at the command prompt, and issue some commands..
    -re ".*.#"    { send "SEND MY COMMAND HERE\r" }
    -re ".*.>" { send "ls -lt\r" }

    timeout        { exit 1 }
}
expect {
    -re ".*.#"    { send "exit\r" }
    timeout        { exit 1 }
}
expect eof

So like I was saying, this one works just fine when the password is correct. But when the password is NOT correct
and I pass control to the user, after he enters the password and hits "Enter", control stays with the user and
never goes back to the script.

Is there something wrong I am doing with the "interact" command..?

This code below here I had right inside the first "expect { }" section that continas the "*assword:" and the "timeout".
I tried doing this a bunch of different ways with the while loop, but I can't seem to get it working correctly.

Code:
### This code was in the First Expect {} Section from the original code above. But I tried the other code above
#   when I couldn't get this one to work...
expect {
    "*assword: " { 
        send "$password\r"
        expect ".*#" { puts "Password was correct. At 1st Command Prompt\n" }
        expect "*assword: " {
            while {$done == 0} {
                send "$password\r"
                expect "*assword: " { interact -o "\r" return ; exp_continue }    #--> continue to the next expected result
                expect -re ".*\#" { puts "IN REGEX HERE <---\n" ; set done 1 }    #--> Set done to one to exit the loop
            }
        }
    }
    timeout { exit 1 }
}

If anyone has any suggestions at all of what is a good way that I can accomplish this, PLEASE feel free. I am stumped

** Basically if the first password that is entered by the user, and passed to the Expect Script is incorrect I want to
pass control to the user to try another password. Then, after the user types the password and hits "Enter" pass
control back to Expect... and do this until we find a normal command prompt (which signifies a correct password was
entered)... Or until the maximum retries has been reached.


Thanks in Advance,
Matt

---------- Post updated at 04:03 PM ---------- Previous update was at 03:09 PM ----------

Humm... I think I was getting a little overwhelmed so I started over from scratch, and now it seems to be working...?



Not sure what was different in this interact command compared to the one in my OP...

Here's what seems to be working:

Code:
#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}


#get command line arguments
set ip_address [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]

#Set the timeout limit
set timeout 5

#telnet to the router
spawn /usr/bin/ssh $username@$ip_address
sleep 1

expect_after {
    timeout {
       exit 100
    }
}


### SECTION 1
expect {
    ### FIRST Password Automatic Try...
    "*assword: "    { send -- "$password\r" }
    timeout        { exit 1 }
}

### SECTION 2
expect {
    ### SECOND Password Try by User...
    "*assword: "    { interact -o "\r" return ; exp_continue }
    -re ".*#.*"    { send -- "ls -l\r" }
    timeout        { exit 2 }
}

### SECTION 3
expect {
    ### SECOND Password Try by User...
    "*assword: "    { interact -o "\r" return ; exp_continue }
    -re ".*#.*"    { send -- "ls -ltr\r" }-
    timeout        { exit 3 }
}
expect eof

But I would still like to try getting a while loop in there for this if anyone could at least show me how it should be formatted...
And what would the best way to break outta the loop.

Thanks in Advance,
Matt
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Expect scripting - How to match a double quotes " " prakasuj Shell Programming and Scripting 3 06-15-2012 11:35 PM
exec perl in expect script yields "invalid command" dpouliot Shell Programming and Scripting 4 01-12-2012 09:23 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Expect "interact" fails when called from another script treesloth UNIX for Dummies Questions & Answers 2 02-11-2010 06:35 PM
question about "sleep" command in expect script robbiezr Shell Programming and Scripting 4 06-01-2009 07:52 AM



All times are GMT -4. The time now is 04:25 PM.