Wireless Access

last person joined: 10 hours ago 

Access network design for branch, remote, outdoor, and campus locations with HPE Aruba Networking access points and mobility controllers.
Expand all | Collapse all

[Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

This thread has been viewed 0 times
  • 1.  [Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

    Posted Jan 13, 2014 09:01 PM
      |   view attached

    Attachment has been updated 1/16/2014.

     

    This is going to be a pretty straight forward tutorial for getting the results of the previous days logins from the audit-trail command from the controller via a perl script.

     

    This script is running on an Ubuntu machine and the controller is a 7240 running 6.3.1.1 with enable bypass configured.

     

    You will need to set the host, username and password variables so they coincide with your controller and then set the array @CmdList to the commands that you want to run. The commands I have set are "no paging" so that we can get the complete results of the next command that I have set which is "show audit-trail login | include \"$date\"" which in this case we show the audit-trail for the previous day. The $date variable using the *nix date command to get the current date and subtract one day from it then store that result into $date. It works great when going from say the 1st of one month to the 31st of the previous month without having to do any of the extra work.

     

    This script can be adapted to run many different commands and it can be used to connect to all types of devices/servers that accept SSH connections.

     

    Script attached:

     

    1. Save the attached file into a directory on your Ubuntu machine, I'll put it in  /scripts/aruba and call it getDailyAudit.pl
    2. Open the file in your favorite text editor
    3. Set the host, username and password variables
    4. Set the array @CmdList to have the commands you want to run on the server (separated by commas)
    5. Run the following command from the terminal: sudo perl /scripts/aruba/getDailyAudit.pl
    6. Profit

    I hope this helps some of you who are looking to modify some of the regular tasks that are performed from the CLI. If you are looking for some ideas: I use a variation of this script to get results, copy them to a file and then I use additional grep statements to further narrow down the results and email the results daily/weekly to our audit analysts. 

     

    Have fun, post any ideas or custom scripts you've written using this tutorial as a base and please don't forget to Kudo if this has helped.

     

     

    Reference:

    Install Ubuntu: http://www.ubuntu.com/download/desktop/install-desktop-latest

    Net::SSH:Expect http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod


    #7240

    Attachment(s)

    txt
    getDailyAudit.txt   2 KB 1 version


  • 2.  RE: [Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

    Posted Jan 16, 2014 11:15 AM

    Some people reported problems running in a virtual environment and interacting with stdout in execArubaCmds. I adjusted this section and now it will only use the local variable.

     

    if ($error !~ m/.*SSHAuthenticationError Login timed out.*/){
    foreach (@$cmdList) {
    my ($stdout,$stderr,$exit) = $conn->exec($_,1); # 1 is the delay between commands for some situations this needs to be increased or it can be removed completly
    $results.=$stdout;
    }
    }
    else {
    $results = "Login timed out";
    }

     


    if ($error !~ m/.*SSHAuthenticationError Login timed out.*/){
    foreach (@$cmdList) {
    $results .= $conn->exec($_,1); # 1 is the delay between commands for some situations this needs to be increased or it can be removed completly
    }
    }
    else {
    $results = "Login timed out";
    }



  • 3.  RE: [Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

    Posted Jan 16, 2014 11:28 AM

    Adding mail support is pretty easy. Simply add the following highlighted code:

     

    #!/usr/bin/perl

    use warnings;
    use Net::SSH::Expect; # this will need to be installed via cpan
    use Mail::Mailer; # this will need to be installed via cpan

    .

    .

    .

    ## magic to get the date formatting correct to get the previous days logs
    $date = `date --date="-1 day" +%b`;
    $dom = `date --date="-1 day" +%Oe`;
    $date = $date." ".$dom;
    $date =~ s/
    //g;

     

    ## Mailer variables
    my $from_address = "test@thisisanexample.com";  # the address the email will be coming from
    my $to_address = "test@thisisanexample.com";  # the address the email will be going to
    my $subject = "Controller Audit-Trail for $date"; # the subject for the email

    .

    .

    .

    print "Logins for " . $date . " listed below:

    ". $output. "

    ";

     

    ## let's send some mail
    my $mailer = Mail::Mailer->new("sendmail");
    $mailer->open({
    From => $from_address,
    To => $to_address,
    Subject => $subject,
    });

    print $mailer "Logins for " . $date . " listed below:

    ".$output;

    $mailer->close();

    .

    .

    .



  • 4.  RE: [Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

    Posted Jan 16, 2014 12:12 PM

    Love Perl... Love this script!  Thanks for sharing.



  • 5.  RE: [Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

    Posted Jan 16, 2014 01:10 PM

    @JPoley wrote:

    Love Perl... Love this script!  Thanks for sharing.


    Coming from a C++/Java background Perl is a new found passion of mine.

     

    I'll continue to share different tweaks that I've made to it as time allows.



  • 6.  RE: [Tutorial] Running CLI commands remotely using Perl Jan14-Tutorial

    Posted Jun 23, 2016 05:46 PM

     

    I came across this by accidient, although it has been something I have been wanting to get involved with. I do appreciate the baseline script with getting started with Perl as this is not something I have spent time with before. It made it very easy to alter and adjust as necessary commands I was looking for. Will need to bursh up a bit more n the language, since I was able to get NET::SSH::EXPECT installed on my OSX. 

     

    http://triopter.com/archive/how-to-install-perl-modules-on-mac-os-x-in-4-easy-steps/