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

Issue

This Content is from Stack Overflow. Question asked by Danny Luk

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 Net::SSH module. I execute the perl script the following error comes up –

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:

  1 #!/usr/bin/perl
  2
  3 use Net::SSH;
  4 my $log_path="/usr/local/nagios/var/log";
  5
  6 if ($ARGV[0] eq "-H"){
  7         if ($ARGV[1]=~/d+.d+.d+.d+/){
  8         $router=$ARGV[1];
  9         }
 10         else {
 11         print "ERROR: Invalid Router's IP format!n";
 12         &usage;
 13         }
 14 }
 15 else {
 16 print "ERROR: Invalid Argumentn";
 17 &usage;
 18 }
 19 if ($ARGV[2] eq "-R"){
 20         if ($ARGV[3]=~/d+.d+.d+.d+/){
 21         $remote_ip=$ARGV[3];
 22         }
 23         else {
 24         print "ERROR: Invalid Remote IP format!n";
 25         &usage;
 26         }
 27 }
 28 else {
 29 print "ERROR: Invalid Argumentn";
 30 &usage;
 31 }
 32 if ($ARGV[4] eq "-w") {
 33         if ($ARGV[5]) {
 34         $warn=$ARGV[5]
 35         }
 36         else {
 37         print "ERROR: Warning value missing!n";
 38         &usage;
 39         }
 40 }
 41 if ($ARGV[6] eq "-c") {
 42         if ($ARGV[7]) {
 43         $crit=$ARGV[7]
 44         }
 45         else {
 46         print "ERROR: Critial value missing!n";
 47         &usage;
 48         }
 49 }
 50
 51 ($wrta,$wpl)=split(",",$warn);
 52 chop($wpl);
 53 ($crta,$cpl)=split(",",$crit);
 54 chop($cpl);
 55
 56 ($result1,$result2)=&ping($router,$remote_ip);
 57
 58 chop($result1);
 59 chop($result2);
 60 ($na,$na,$na,$na,$na,$na,$percent,$na,$na,$na)=split(" ",$result1);
 61 chop($percent);
 62 ($na,$na,$na,$rtt,$na)=split(" ",$result2);
 63 ($min,$rta,$max,$stddev)=split("/",$rtt);
 64
 65 if ((!$percent)&&(!$min)&&(!$rta)&&(!$max)&&(!$stddev)){
 66 print "CRITICAL - No data Collected from Gatewayn";
 67 exit(2);
 68 }
 69
 70 if ($percent eq 100) {
 71 print "CRITICAL - Remote Host Unreachable $remote_ipn";
 72 exit(2);
 73 }
 74 elsif (($rta>=$crta)||($percent>=$cpl)) {
 75         print "PING CRITICAL - Packet loss = $percent%, RTA = $rta ms MIN = $min ms MAX = $max msn";
 76 exit(2);
 77 }
 78 elsif (($rta>=$wrta)||($percent>=$wpl)) {
 79         print "PING WARNING - Packet loss = $percent%, RTA = $rta ms MIN = $min ms MAX = $max msn";
 80 exit(1);
 81 }
 82 else {
 83         print "PING OK - Packet loss = $percent%, RTA = $rta ms MIN = $min ms MAX = $max msn";
 84 exit(0);
 85 }
 86 close(LOG);
 87
 88 sub ping {
 89
 90 $Login = "abcde";
 91 $Pass = "12345";
 92
 93    my $router = $_[0];
 94    my $remote_ip = $_[1];
 95    my $Port="22";
 96    my $Prompt=">";
 97    my $Timeout=10;
 98
 99    my $cmd = "ping $remote_ip rapid";
100
101    my $ses;
102    my $log1;
103    my $log2;
104    my $file="test.log";
105
106    my $ses = Net::SSH(Host => $router,
107                         User => $Login,
108                         Password => $Pass,
109                         Port => $Port,
110                         Prompt => "/$Prompt/i",
111                         Errmode => "return",
112                         Timeout => $Timeout);
113
114    if (!$ses) {
115         print "CRITICAL - Router Unreachable $routern";
116         exit(2);
117    }
118
119    if ($Login ne '' and $Pass ne '') {
120         $ses->login($Login, $Pass);
121    } elsif ($Login eq '' and $Pass ne '') {
122         $ses->waitfor('/password[: ]*$/i');
123         $ses->cmd($Pass);
124    } else {
125         $ses->waitfor("/$Prompt/i");
126    }
127
128    $ses->print($cmd);
129
130    $first = 1;
131    $ses->input_record_separator("xff");
132
133    while (($junk, $_) = $ses->waitfor("/(^.*n|$Prompt)/i")) {
134         if ($first) {
135                 $first = 0;
136                 next;
137         }
138
139                 if ($_=~/transmitted/){
140                 $log1=$_;
141                 }
142                 if ($_=~/^round/){
143                 $log2=$_;
144                 last;
145                 }
146
147    }
148
149    $ses->input_record_separator("n");
150
151    $ses->close;
152
153    return ($log1,$log2);
154
155 }
156
157 sub usage {
158 $usage="check_juniper_ping:
159 usage:check_juniper_ping -H <router_address> -R <remote_address> -w <wrta>,<wpl>% -c <crta>,<cpl>%n";
160 print "$usagen";
161 exit(3);
162 }

My question is –
Why I am getting above mentioned error?
I doubt about the error catching method is not proper. Please suggest a better solution?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?