Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Screen Scraping from Juniper Switch using Expect.pm - Multiple Commands

by mrdavis94 (Initiate)
on Jun 17, 2015 at 14:48 UTC ( [id://1130823]=note: print w/replies, xml ) Need Help??


in reply to Screen Scraping from Juniper Switch using Expect.pm - Multiple Commands

Julio -

I've updated your script a little and I believe this is what you're looking to do. Since it's looking for a specific "$match" string, password prompt and "---more" string, this will only work on JunOS. You can modify it for other vendors - or get cute with regex and make it multi-vendor.
#!/usr/bin/perl use Expect; my $access_protocol = 'ssh'; my $host = 'myhost.mycompany.com'; my $timeout = 15; my $username = 'svc_network_readonly'; my $password = 'sc00byd00byD00'; $Expect::Log_Stdout = 0; my @commands = ("show virtual-chassis", "show version", "show arp no-r +esolve", "show ethernet-switching table", "exit"); my $exp = Expect->spawn("$access_protocol -l $username $host") or die +"Can't connect to $host: $!\n"; my $match="^$username\@$host. "; $exp->expect ($timeout, ['^\-\-\-.more', sub { $x = $exp->before(); print "$x"; $exp->send(" "); exp_continue; }], [ qr /$username\@$host\'s password: /, sub { $exp->send("$password\n"); exp_continue; }], [ $match, sub { my $cmd=shift(@commands); $x = $exp->before(); print "$x"; $exp->send("$cmd\n"); exp_continue; }], ['timeout', sub { print "A timeout has occurred - no match found\n"; }], '-re', '] $');

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-03-19 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found