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.

In reply to Re: Expect.pm Example(s)? by zengargoyle
in thread Expect.pm Example(s)? by traveler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.