Sponsored Content
Top Forums Shell Programming and Scripting Expect Scripting - Using the "interact" command? Post 302674423 by mrm5102 on Thursday 19th of July 2012 04:03:54 PM
Old 07-19-2012
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...?

Smilie

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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help on "Expect" command in shell programming

Hi, Iam using Solaris 10 OS in the server.iam unable to find expect command in bin directory.how can i install the script in the server to use "expect" in shell programming. (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

2. Shell Programming and Scripting

question about "sleep" command in expect script

I wrote some expect script to telnet to some device to execute some commands.Firstly,I can't get full result some time,then I try to add some "sleep" command in it.Fortunately it works. My idea about it is that it uses sleep command to wait the result to be displayed.Am I right or correct the... (4 Replies)
Discussion started by: robbiezr
4 Replies

3. UNIX for Dummies Questions & Answers

Expect "interact" fails when called from another script

So, I have an expect script (let's call it expect.exp) that takes 3 arguments. It logs into a remote server, runs a set of commands, then hands control over to the user by the "interact" command. If I call this script from the command line, it works properly. Now I'd like to apply this script... (2 Replies)
Discussion started by: treesloth
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

6. Shell Programming and Scripting

exec perl in expect script yields "invalid command"

I'm trying to execute something like this: exec perl -i -pe 's/\015/\012/g' '${file}' in my expect script and I get: error "invalid command name \"perl\". however, if I run perl -i -pe 's/\015/\012/g' "/Users/Shared/menu-items.txt" directly in my terminal, it runs fine. I'm an... (4 Replies)
Discussion started by: dpouliot
4 Replies

7. Shell Programming and Scripting

Expect scripting - How to match a double quotes " "

I am trying to match a text which contains the " ", from the log file. But it doesn't match. I understand that " " has got a special meaning to TCL/Expect. hence I tried the following, but no luck. expect -ex { "lp -c -demail -ot\\\"firstname_surname@gmail.com\\\"... (3 Replies)
Discussion started by: prakasuj
3 Replies

8. Shell Programming and Scripting

problem in automating "fdisk" command using send and expect

hi i want to automate fdisk command . i spawned a process containing fdisk command from a process and tried to send the options to fdisk promt from that process. but that spawed process is notstarting itself help me out trying for two days :wall: my code: #!/bin/bash echo... (5 Replies)
Discussion started by: jagak89
5 Replies

9. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

10. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies
All times are GMT -4. The time now is 12:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy