Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to get prompts for expect

by wulvrine (Friar)
on Apr 16, 2007 at 15:09 UTC ( [id://610369]=perlquestion: print w/replies, xml ) Need Help??

wulvrine has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,
I have been playing recently with perls Expect module, and came across a variety of instances where I needed to wait for the command prompt to appear before I moved on with the program. So how does one find the 'current prompt' for use later in the program to wait for said prompts to appear before continuing?
The problem I have is that the command prompt is different for root, regular users, etc. I have tried sending just a newline down on the Expect handle, but it returns nothing. before/match/after are all always empty etc.
Thanks a lot!

s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Replies are listed 'Best First'.
Re: How to get prompts for expect
by jasonk (Parson) on Apr 16, 2007 at 15:38 UTC

    Generally you just try to find something that matches any of the possible prompts. The Expect documentation uses qr'[#>:] $'. When dealing with interactive shells, I usually either set the prompt myself to something that is easier to match on, or (more commonly) wrap the command being sent to the other side in something easily identifiable such as:

    # Instead of this: # $exp->send( "hostname\n" ); # $exp->expect( $timeout, $shell_prompt ); # # Try something like this $exp->send( "echo START_COMMAND; hostname; echo END_COMMAND" ); $exp->expect( $timout, [ qr/START_COMMAND\n(.*)END_COMMAND.*?$shell_prompt/, sub {}, # do whatever with the data... ], );

    We're not surrounded, we're in a target-rich environment!
Re: How to get prompts for expect
by swares (Monk) on Apr 17, 2007 at 06:17 UTC
    Here is the generic prompt string I used good effect with the Net::Telnet module. I think it will work with the expect module also.
    '/[\$%#>] $/'
Re: How to get prompts for expect
by wulvrine (Friar) on Apr 17, 2007 at 11:23 UTC
    Thanks jasonk, what a great idea! I am unsure whether that works when I need to do some 'expecting' during the command, but I will definately try that. But even if it doesn't, I have a few places that I know doing this will make my life easier. Much appreciated!!
    swares Thanks to you too, I will try this as well. I really appreciate the assistance.

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://610369]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found