Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
PerlMonks Example(s)?

by traveler (Parson)
on Sep 04, 2001 at 21:51 UTC ( #110103=perlquestion: print w/replies, xml ) Need Help??
traveler has asked for the wisdom of the Perl Monks concerning the following question:

Brothers and Sisters, I need to interact with a Cisco router. I need to send a break and respond to prompts. I've looked at Net::Telnet:Cisco, Cisco::Conf and Expect and the latter looks the most promising for what I have to do (for one, I have to use the console serial port so telnet access is out).

My problem is that I'm not a tcl-Expect guru and trying to translate what little I know about tcl-Expect to is proving very difficult and confusing. So my question is, does anyone have any good pointers to examples with that I can use to help me figure out how to use it?


Replies are listed 'Best First'.
Re: Example(s)?
by Rex(Wrecks) (Curate) on Sep 04, 2001 at 22:51 UTC
    Well I'm no Expect god so no help there. However you can look into Device::SerialPort for Linux or Win32::SerialPort if you are on win32

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
      Or tax me when I am dead ... :-)
        There are only 3 certainties in life... death, taxes and trolls.
Re: Example
by grinder (Bishop) on Sep 05, 2001 at 16:27 UTC

    I have written exactly one (1) Perl program that used Expect. Unfortunately, after frenzied grepping of four different machines, I cannot lay my hands on it.

    What I do remember is that by reading the perldoc I was able to figure out what I needed, except for one thing: I couldn't determine how you actually sent input to the process you were controlling through the Perl Expect object. It turns out that you treat the object as a filehandle, and print to it. I was looking for something like

      $obj->send( $username )

    when in fact all you have to to is

      print $obj $username

    That's never clearly stated in the documentation (because it assumes you're familiar with Tcl/Tk, I guess). Apart from that it works pretty much as advertised. At the simplest, you do something like this:

    # untested code, reciting from memory my $e = Expect->spawn( '/usr/local/bin/foo', '-f', '/tmp/bar' ); print $e "zug\n"; if( $e->expect(10, 'zwang')) { print "got what I expected\n"; } else { print "hmm..."; }

    g r i n d e r
Re: Example(s)?
by zengargoyle (Deacon) on Nov 07, 2011 at 08:38 UTC
    I use Expect enough but via ssh, not exactly sure about serial port usage. I would assume from ancient experience that you would use setserial to configure the port and then get a r/w IO::Handle for /dev/ttyS0 or whatever and pass that to Expect->exp_init($fh). Or maybe spawn a kermit and interact with that to handle all of the serial setting up stuff. After you have an Expect object, it's pretty simple, make sure to read the perldoc to the end, there are several examples there (that's where I picked up everything). Some useful things:
    use Expect; $Expect::Debug = 1; # verbose debug output $Expect::Log_Stdout = 1; # show chatter for debugging # stolen from Net::Telnet::Cisco I think. my $prompt = '[\w-]+\s?(?:\(config[^\)]*\))?\s?[\$#>]\s?(?:\(enable\)) +?\s*$'; my $exp = Expect->spawn( @command ); # or my $exp = Expect->exp_init($filehandle); sub cmd { my ($exp, $cmd) = @_; $exp->print($cmd, "\n"); $exp->expect($timeout, -re => $prompt) or return; my $output = $exp->before; $output =~ s/\r//g; # silly telnet line endings $output =~ s/^$cmd\n//; # remove the sent cmd return $output; } sub login { my ($exp, $user, $pass) = @_; my $rc = $exp->expect($timeout, [qr/[Pp]assword:\s*/, sub { my $exp = shift; $exp->print($pass,"\n"); exp_continue; }], [qr/[Uu]sername:\s*/, sub { my $exp = shift; $exp->print($user,"\n"); exp_continue; }], -re => $prompt, ); return defined($rc) ? $rc : 0; } sub enable { my ($exp, $pass) = @_; $exp->print("enable","\n"); $exp->login('',$pass); } # generic chat once you have your $exp from a spawn or filehandle login($exp, "username", "password"); cmd($exp,"terminal length 0"); enable($exp, "enablepass"); my $config = cmd($exp, "sho mem"); cmd($exp, "exit"); # enable cmd($exp, "exit"); $exp->soft_close;
    But I really put those subs in a subclass of Expect so I can just call them as $exp->cmd('foo"). The expect function can match simple strings or regex, and there's the giving it an arrayref with a match and coderef to be executed on match and the exp_continue which causes it to restart the matching process. Mostly it's just the print and expect and before methods doing most of the work. I haven't needed to muck about with the pty/tty options in any of the router/switch tweaking scripts I've written so far.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://110103]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-12-17 12:29 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (464 votes). Check out past polls.