0

How can I write an "Expect" script to:

  1. iterate over a list of IP addresses and log in to each to perform commands (like in a BASH for loop). Right now I just copy/pasted the few lines that are my Expect script and manually put in each unique IP of each device - not so efficient, I know.

  2. Have Expect accept User Input and write it to a variable (like "read" in BASH)? I'd like to write a script which says something like: "enter the IP address of the router" "enter the port on the router you want to check" and then will launch Expect, SSH to that IP addresses, and run some commands using that router port.

1

I returned to "Exploring Expect" and found that, apparently, if some expect statement fails, then expect sees that as End of File (eof), and quits, and exits script too. So, I simply added:

expect {
{eof} {action_here}
}

And it works.

  • If this solves your original problem, you should mark your answer as accepted, so it is easy to see for others that this is the solution. Please read help→tour, also about the no-chit-chat policy we have on this site. – Anthon May 20 '15 at 10:50
0

The question is more goal oriented than particular to the technologies mentioned, so I'm just going to address the problem according to my spirit. I am going to use python.

Re: issue number 1:

Basically it sounds like you want to parse ip/addresses from a file and run an expect (like) back-and-forth sequence for each ip address.

You don't want to be parsing ip addresses from an except script -- but rather you want to parse ipaddresses from file with python and run the same expect command for each ip address.

So you would need to write the expect command with the ipaddress being a variable so that for each ipaddress in your loop, you assign a different ipaddress to that variable and execute the expect command.

As for the 'expect' like functionality, take a look at the 'pexpect' library: It's a python module that exposes to your python scripts expect like functionality. (See this answer that contains an example and links to other docs/examples: https://stackoverflow.com/a/11160553/1248361). Lets flesh it out a bit:

Looking at the example in the forgoing link, note expect sequence begins with, """

child = pexpect.spawn('some command that requires a password') child.expect('Enter password') child.sendline('password')

""" You can easily replace that string argument to the pexpect.spawn method with pexpect.spawn('some command that requires a password that takes an ipaddress as an option' + ipaddress_variable)

So in python I would just define a function that takes as an argument(s) the part(s) of the expect sequence that will change in each iteration of your for loop, and write the expect sequence inside the body of the function using the variable arguments.

Like this (this is pseudo code):

def expect_function(ipaddress_variable): pexpect.spawn('some command that requires a password' +ipaddress_variable) child.expect('Enter Password') child.sendline('password') child.expect..... (here is where you take over and do what you need to do on the remote machine)

(Sorry about the formatting, pre and code didn't do what I had hoped)

With that funciton defined, the rest is easy.

Simply loop through your ipaddress and call the function, like this psuedo code:

for ipaddress in ipfile: expect_function(ipaddress)

problem number two I would also handle through python.

  • context, I know the OP and introducing python isn't out of left field in our real life context. – naftalimich Apr 1 '15 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.