Perl Scripting to FW

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Perl Scripting to FW

L1 Bithead

So I got help from support and they gave me this nifty Perl example on how to get output from the firewalls.

However, I need to assign that output from the show command to a variable for further manipulation.  That was the whole purpose.

Does anyone know how to assign the outputs from the show commands to a variable?

Thanks!

And below is the script example:

example.pl

-----------------------------------------------------------------

#!/usr/bin/perl

use strict;

my $cmd = "ssh ssh\@10.40.10.228";

my $line = "show system info";

open CLI, " | $cmd ";

print CLI "$line\n";

close CLI;

10 REPLIES 10

L1 Bithead

What do you want to do?

It's already saved in the $line variable. If you want to process it using the same script just use that variable,

The value of the variable $line is "show system info" ...  Printing $line gives me ... "show system info".

What I need is the output from the show command.

I want to assign that to a variable or be able to manipulate/parse it in the script to get meaningful information.

L1 Bithead

Ok first let's check your code, is it working?


my $cmd = "ssh ssh\@10.40.10.228";  # external command to run, I assume you're using a UNIX in your computer

my $line = "show system info";               # Command to run


open CLI, " | $cmd ";                          #Executing the external command, in this case SSH login, you cand find more info in the following link but I prefer to use the backstiks http://www.perlhowto.com/executing_external_commands

print CLI "$line\n";                               # you have the CLI opened so you just put the command there and print it

close CLI;                                             # you close the SSH external session


You have a lot of ways to put this working, you can use the Net:SSH module http://search.cpan.org/~schwigon/Net-SSH-Perl-1.37/lib/Net/SSH/Perl.pm

And create an script like this one:

use Net::SSH::Perl;                                                  #Loading the SSH module

my $cmd = shift;                         #command to execute

   my $ssh = Net::SSH::Perl->new($host);               #opening the SSH session

   $ssh->login($user, $pass);                                   #login into the session

   my($stdout, $stderr, $exit) = $ssh->cmd($cmd);   # the output will be saved in the $stdout varaible 


If your script is working may be you can try


@output = print CLI "$line\n";                                              # @ indicates it's an array


Also the easiest one is that you already set up SSH keys betwen the PA FW and your script machine. https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2


my @output = `ssh root@1.1.1.1 "which perl"`;


There is more info in the next link

http://stackoverflow.com/questions/2848725/how-can-i-ssh-inside-a-perl-script

L0 Member

You need two pipes one for input and one for output, this document goes over using pipes in Perl,

Pipes (Programming Perl)

In the example CLI is a unidirectional pipe so the output will need to be returned through another pipe.

The example below will not work to get data back,

@output = print CLI "$line\n";

This example uses two pipes, it does require an extra library and you may have problems with line buffering.

use IPC::Open2;

open2(\*INPUT, \*OUTPUT, 'ssh ssh\@firewall');

print OUTPUT "show system info";

foreach $line (<INPUT>) {

     print "$line";

}

close INPUT;

close OUTPUT;

Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!