Perl - connecting via SSH to the junos device and running a ping command

Viewed 61

I am running a ping command in a Juniper router by connecting from my Nagios linux server. To achieve this I am connecting to the Juniper router using Perl CPAN Net::SSH module.

The following error are reported when the script runs:

check_juniper_ping.pl -H <router_address> -R <remote_address> -w <wrta>,<wpl>% -c <crta>,<cpl>%
/usr/local/nagios/libexec/check_juniper_ping.pl -H 1.1.1.1 -R 2.2.2.2 -w 100,20% -c 500,60%

Undefined subroutine &Net::SSH called at /usr/local/nagios/libexec/check_juniper_ping.pl line 106.

Here is my code to ssh access to a Juniper router:

#!/usr/bin/perl

use Net::SSH;
my $log_path="/usr/local/nagios/var/log";

if ($ARGV[0] eq "-H"){
        if ($ARGV[1]=~/\d+.\d+.\d+.\d+/){
        $router=$ARGV[1];
        }
        else {
        print "ERROR: Invalid Router's IP format!\n";
        &usage;
        }
}
else {
print "ERROR: Invalid Argument\n";
&usage;
}
if ($ARGV[2] eq "-R"){
        if ($ARGV[3]=~/\d+.\d+.\d+.\d+/){
        $remote_ip=$ARGV[3];
        }
        else {
        print "ERROR: Invalid Remote IP format!\n";
        &usage;
        }
}
else {
print "ERROR: Invalid Argument\n";
&usage;
}
if ($ARGV[4] eq "-w") {
        if ($ARGV[5]) {
        $warn=$ARGV[5]
        }
        else {
        print "ERROR: Warning value missing!\n";
        &usage;
        }
}
if ($ARGV[6] eq "-c") {
        if ($ARGV[7]) {
        $crit=$ARGV[7]
        }
        else {
        print "ERROR: Critial value missing!\n";
        &usage;
        }
}

($wrta,$wpl)=split(",",$warn);
chop($wpl);
($crta,$cpl)=split(",",$crit);
chop($cpl);

($result1,$result2)=&ping($router,$remote_ip);

chop($result1);
chop($result2);
($na,$na,$na,$na,$na,$na,$percent,$na,$na,$na)=split(" ",$result1);
chop($percent);
($na,$na,$na,$rtt,$na)=split(" ",$result2);
($min,$rta,$max,$stddev)=split("/",$rtt);

if ((!$percent)&&(!$min)&&(!$rta)&&(!$max)&&(!$stddev)){
print "CRITICAL - No data Collected from Gateway\n";
exit(2);
}

if ($percent eq 100) {
print "CRITICAL - Remote Host Unreachable $remote_ip\n";
exit(2);
}
elsif (($rta>=$crta)||($percent>=$cpl)) {
        print "PING CRITICAL - Packet loss = $percent\%, RTA = $rta ms MIN = $min ms MAX = $max ms\n";
exit(2);
}
elsif (($rta>=$wrta)||($percent>=$wpl)) {
        print "PING WARNING - Packet loss = $percent\%, RTA = $rta ms MIN = $min ms MAX = $max ms\n";
exit(1);
}
else {
        print "PING OK - Packet loss = $percent\%, RTA = $rta ms MIN = $min ms MAX = $max ms\n";
exit(0);
}
close(LOG);

sub ping {

$Login = "abcde";
$Pass = "12345";

   my $router = $_[0];
   my $remote_ip = $_[1];
   my $Port="22";
   my $Prompt=">";
   my $Timeout=10;

   my $cmd = "ping $remote_ip rapid";

   my $ses;
   my $log1;
   my $log2;
   my $file="test.log";

The next line is number 106:

   my $ses = Net::SSH(Host => $router,
                        User => $Login,
                        Password => $Pass,
                        Port => $Port,
                        Prompt => "/$Prompt/i",
                        Errmode => "return",
                        Timeout => $Timeout);

   if (!$ses) {
        print "CRITICAL - Router Unreachable $router\n";
        exit(2);
   }

   if ($Login ne '' and $Pass ne '') {
        $ses->login($Login, $Pass);
   } elsif ($Login eq '' and $Pass ne '') {
        $ses->waitfor('/password[: ]*$/i');
        $ses->cmd($Pass);
   } else {
        $ses->waitfor("/$Prompt/i");
   }

   $ses->print($cmd);

   $first = 1;
   $ses->input_record_separator("\xff");

   while (($junk, $_) = $ses->waitfor("/(^.*\n|$Prompt)/i")) {
        if ($first) {
                $first = 0;
                next;
        }

                if ($_=~/transmitted/){
                $log1=$_;
                }
                if ($_=~/^round/){
                $log2=$_;
                last;
                }

   }

   $ses->input_record_separator("\n");

   $ses->close;

   return ($log1,$log2);

}

sub usage {
$usage="check_juniper_ping:
usage:check_juniper_ping -H <router_address> -R <remote_address> -w <wrta>,<wpl>% -c <crta>,<cpl>%\n";
print "$usage\n";
exit(3);
}

My question is
Why I am getting above mentioned error?

0 Answers
Related