Hello, I'm trying to do some telnet/ssh (start with telnet for simplicity) screen scraping from a Juniper switch (netconf/snmp aren't available) using the Expect.pm perl module.

My goal is to be able to shove the output of a few commands into an array for each command. For example:

"show version" goes to @show_version and "show interfaces" goes to @show_interfaces

I've consulted http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod#NAME

I am failing utterly at this.

I can login to the switch and do a basic command and then wait for the session to timeout.

I've tried various combinations but I've so far failed at:

1. Understanding the flow control logic of the Expect module. So to figure out I'm logged in, I need to look for a CLI prompt. But how do I tell the difference between the initial login vs. subsequent prompts (after the successful completion of a CLI command)?

2. How do I piggy back several show commands together without logging out each time? I see that as part of the module's FAQ (How to expect on multiple spawned commands) where it mentions the -i parameter but I just don't get it.

#!/usr/bin/perl use strict; use warnings; use Expect; my $cmd1 = "show version | no-more "; my $access_protocol = 'telnet'; my $router = '172.16.2.1'; my $expect_log_file = 'expect-log-file.txt'; my $timeout = 15; my $username = 'username1'; my $password = 'asdfasdfasdf'; #Uncomment to hide stdout: #$Expect::Log_Stdout = 0; my $exp = Expect->spawn("$access_protocol $router") or die "Can't conn +ect to $router: $!\n"; $exp->expect ($timeout, ['(yes/no)',sub{my $fh = shift; $fh->send("yes\n"); exp_continue;}], ['(sername: )|(ogin: )$',sub{my $fh = shift; $fh->send("$username\n" +); exp_continue;}], ['(assword:)$',sub{my $fh = shift; $fh->send("$password\n"); exp_con +tinue;}], ['timeout',sub{&login_error();}], '-re', '> $', #wait for router prompt, then exit expect + ); $exp->log_file($expect_log_file); + $exp->expect ($timeout, ['',sub{my $fh = shift; $fh->send("$cmd1 \n");}] + ); $exp->expect ($timeout, ['More',sub{my $fh = shift; $fh->send(" "); exp_continue}], '-re', '[#>:]$', #wait for router prompt, then exit expect, + ); $exp->expect ($timeout, ['',sub{my $fh = shift; $fh->send("exit \n");}] + ); + sub login_error { print "Unable to login to router $router. Try to login manually wit +h SSH. \n"; exit; }
perl ./exp_1.pl Trying 172.16.2.1... Connected to 172.16.2.1 (172.16.2.1). Escape character is '^]'. EX3200-J (ttyp1) login: username1 Password: --- JUNOS 12.3R6.6 built 2014-03-13 06:58:47 UTC username1@EX3200-J> show version | no-more Hostname: EX3200-J Model: ex3200-24t JUNOS Base OS boot [12.3R6.6] JUNOS Base OS Software Suite [12.3R6.6] JUNOS Kernel Software Suite [12.3R6.6] JUNOS Crypto Software Suite [12.3R6.6] JUNOS Online Documentation [12.3R6.6] JUNOS Enterprise Software Suite [12.3R6.6] JUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R6.6] JUNOS Routing Software Suite [12.3R6.6] JUNOS Web Management [12.3R6.6] JUNOS FIPS mode utilities [12.3R6.6] username1@EX3200-J>

In reply to Screen Scraping from Juniper Switch using Expect.pm - Multiple Commands by julio-johnson

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.