mirror of
				https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
				synced 2025-11-02 19:58:03 +00:00 
			
		
		
		
	lf_generic_ping.pl: various:
- improves sketchy port maching, fixes skipping ports lacking IP address - improves help
This commit is contained in:
		@@ -24,8 +24,8 @@
 | 
				
			|||||||
# done
 | 
					# done
 | 
				
			||||||
package main;
 | 
					package main;
 | 
				
			||||||
use strict;
 | 
					use strict;
 | 
				
			||||||
use diagnostics;
 | 
					 | 
				
			||||||
use warnings;
 | 
					use warnings;
 | 
				
			||||||
 | 
					use diagnostics;
 | 
				
			||||||
use Carp;
 | 
					use Carp;
 | 
				
			||||||
use Data::Dumper;
 | 
					use Data::Dumper;
 | 
				
			||||||
$SIG{ __DIE__ }   = sub { Carp::confess( @_ )};
 | 
					$SIG{ __DIE__ }   = sub { Carp::confess( @_ )};
 | 
				
			||||||
@@ -53,9 +53,10 @@ $0 --mgr {host-name | IP}
 | 
				
			|||||||
   --radio {wiphy} | --parent {eth}
 | 
					   --radio {wiphy} | --parent {eth}
 | 
				
			||||||
   --match {simple prefix, no stars or questions marks}
 | 
					   --match {simple prefix, no stars or questions marks}
 | 
				
			||||||
   --cmd {"double quoted command"} # can contain special parameters
 | 
					   --cmd {"double quoted command"} # can contain special parameters
 | 
				
			||||||
 | 
					   --name {prefix to name connection, appended with padded number}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 Examples:
 | 
					 Examples:
 | 
				
			||||||
  $0 --mgr localhost --resource 1 --dest 192.168.0.1 -i wlan0 -i sta3000
 | 
					  $0 --mgr localhost --resource 1 --dest 192.168.0.1 -i wlan0 -i sta3000 --name "wlan_ping"
 | 
				
			||||||
  This will match just sta3000
 | 
					  This will match just sta3000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 All interfaces on a parent radio or MAC VLANs on parent Ethernet port:
 | 
					 All interfaces on a parent radio or MAC VLANs on parent Ethernet port:
 | 
				
			||||||
@@ -74,9 +75,13 @@ $0 --mgr {host-name | IP}
 | 
				
			|||||||
   %i port IPv4 address
 | 
					   %i port IPv4 address
 | 
				
			||||||
   %p port name
 | 
					   %p port name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 Example command
 | 
					 Example with curl wrapper provides better feedback to LANforge:
 | 
				
			||||||
 $0 --cmd "curl -sqL --dns-ipv4-addr %i --dns-interface %p --interface %p --localaddr %i -o /tmp/results-%p http://%d/"
 | 
					 $0 --mgr cholla-f19 -r 2 -n curl_ex_ --match 'eth2#' \\
 | 
				
			||||||
 | 
					   --cmd './scripts/lf_curl.sh -n 10 -o /tmp/curl_%p.out -i %i -p %p -d %d' --dest http://localhost/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 Example curl command doesn't provide good feedback to LANforge:
 | 
				
			||||||
 | 
					 $0 --cmd "curl -sqL --dns-ipv4-addr %i --dns-interface %p \\
 | 
				
			||||||
 | 
					   --interface %p --localaddr %i -o /tmp/results-%p http://%d/"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 The default name of the generic endpoints given will be "lfping_[port]".
 | 
					 The default name of the generic endpoints given will be "lfping_[port]".
 | 
				
			||||||
 You can create multiple generic connections per port by altering
 | 
					 You can create multiple generic connections per port by altering
 | 
				
			||||||
@@ -108,6 +113,8 @@ our $radio        = '';
 | 
				
			|||||||
our $pattern      = '';
 | 
					our $pattern      = '';
 | 
				
			||||||
our $name_pref    = "lfping";
 | 
					our $name_pref    = "lfping";
 | 
				
			||||||
our $ref_cmd      = ''; # user supplied command
 | 
					our $ref_cmd      = ''; # user supplied command
 | 
				
			||||||
 | 
					our $ref_name     = '';
 | 
				
			||||||
 | 
					our $verbose      = ((defined $ENV{'DEBUG'}) && ($ENV{'DEBUG'} eq "1")) ? 1:0;
 | 
				
			||||||
my $help;
 | 
					my $help;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (@ARGV < 2) {
 | 
					if (@ARGV < 2) {
 | 
				
			||||||
@@ -120,6 +127,7 @@ GetOptions
 | 
				
			|||||||
  'mgr_port|p=i'              => \$lfmgr_port,
 | 
					  'mgr_port|p=i'              => \$lfmgr_port,
 | 
				
			||||||
  'resource|r=i'              => \$::resource,
 | 
					  'resource|r=i'              => \$::resource,
 | 
				
			||||||
  'quiet|q'                   => \$::quiet,
 | 
					  'quiet|q'                   => \$::quiet,
 | 
				
			||||||
 | 
					  'verbose|v'                 => \$::verbose,
 | 
				
			||||||
  'radio|parent|o=s'          => \$::radio,
 | 
					  'radio|parent|o=s'          => \$::radio,
 | 
				
			||||||
  'match=s'                   => \$::pattern,
 | 
					  'match=s'                   => \$::pattern,
 | 
				
			||||||
  'interface|intf|int|i=s'    => \@::interfaces,
 | 
					  'interface|intf|int|i=s'    => \@::interfaces,
 | 
				
			||||||
@@ -182,22 +190,22 @@ $utils->log_cli("# $0 ".`date "+%Y-%m-%d %H:%M:%S"`);
 | 
				
			|||||||
our @ports_lines = split("\n", $::utils->doAsyncCmd("nc_show_ports 1 $::resource ALL"));
 | 
					our @ports_lines = split("\n", $::utils->doAsyncCmd("nc_show_ports 1 $::resource ALL"));
 | 
				
			||||||
chomp(@ports_lines);
 | 
					chomp(@ports_lines);
 | 
				
			||||||
our %eid_map = ();
 | 
					our %eid_map = ();
 | 
				
			||||||
my ($eid, $card, $port, $type, $mac, $dev, $rh_eid, $parent, $ip);
 | 
					my ($eid, $card, $port, $type, $mac, $dev, $parent, $ip);
 | 
				
			||||||
foreach my $line (@ports_lines) {
 | 
					foreach my $line (@ports_lines) {
 | 
				
			||||||
  # collect all stations on that radio add them to @interfaces
 | 
					  # collect all stations on that radio add them to @interfaces
 | 
				
			||||||
  if ($line =~ /^Shelf: /) {
 | 
					  if ($line =~ /^Shelf: /) {
 | 
				
			||||||
    $card = undef; $port = undef;
 | 
					    $card = undef; $port = undef;
 | 
				
			||||||
    $type = undef; $parent = undef;
 | 
					    $type = undef; $parent = undef;
 | 
				
			||||||
    $eid = undef; $mac = undef;
 | 
					    $eid = undef; $mac = undef;
 | 
				
			||||||
    $dev = undef; $rh_eid = undef;
 | 
					    $dev = undef;
 | 
				
			||||||
    $ip = undef;
 | 
					    $ip = undef;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # careful about that comma after card!
 | 
					  # careful about that comma after card!
 | 
				
			||||||
  ($card, $port, $type) = $line =~ m/ Card: (\d+), +Port: (\d+) +Type: (\w+) /;
 | 
					  ($card, $port, $type) = $line =~ m/^Shelf: 1, Card: (\d+), +Port: (\d+) +Type: (\w+) /;
 | 
				
			||||||
  if ((defined $card) && ($card ne "") && (defined $port) && ($port ne "")) {
 | 
					  if ((defined $card) && ($card ne "") && (defined $port) && ($port ne "")) {
 | 
				
			||||||
    $eid = "1.${card}.${port}";
 | 
					    $eid = "1.${card}.${port}";
 | 
				
			||||||
    $rh_eid = {
 | 
					    my $rh_eid = {
 | 
				
			||||||
      eid => $eid,
 | 
					      eid => $eid,
 | 
				
			||||||
      type => $type,
 | 
					      type => $type,
 | 
				
			||||||
      parent => undef,
 | 
					      parent => undef,
 | 
				
			||||||
@@ -208,26 +216,28 @@ foreach my $line (@ports_lines) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  ($mac, $dev) = $line =~ / MAC: ([0-9:a-fA-F]+)\s+DEV: (\S+)/;
 | 
					  ($mac, $dev) = $line =~ / MAC: ([0-9:a-fA-F]+)\s+DEV: (\S+)/;
 | 
				
			||||||
  if ((defined $mac) && ($mac ne "")) {
 | 
					  if ((defined $mac) && ($mac ne "")) {
 | 
				
			||||||
    $rh_eid->{mac} = $mac;
 | 
					    $::eid_map{$eid}->{mac} = $mac;
 | 
				
			||||||
    $rh_eid->{dev} = $dev;
 | 
					    $::eid_map{$eid}->{dev} = $dev;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ($parent) = $line =~ / Parent.Peer: (\S+) /;
 | 
					  ($parent) = $line =~ / Parent.Peer: (\S+) /;
 | 
				
			||||||
  if ((defined $parent) && ($parent ne "")) {
 | 
					  if ((defined $parent) && ($parent ne "")) {
 | 
				
			||||||
    $rh_eid->{parent} = $parent;
 | 
					    $::eid_map{$eid}->{parent} = $parent;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  ($ip) = $line =~ m/ IP: ([^ ]+) +MASK: /;
 | 
					
 | 
				
			||||||
 | 
					  ($ip) = $line =~ m/ IP: *([^ ]+) */;
 | 
				
			||||||
  if ((defined $ip) && ($ip ne "")) {
 | 
					  if ((defined $ip) && ($ip ne "")) {
 | 
				
			||||||
    $rh_eid->{ip} = $ip;
 | 
					    $::eid_map{$eid}->{ip} = $ip;
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					} # foreach
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#foreach $eid (keys %eid_map) {
 | 
					#foreach $eid (keys %eid_map) {
 | 
				
			||||||
#  print "eid $eid ";
 | 
					#  print "eid $eid ";
 | 
				
			||||||
#}
 | 
					#}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (defined $::radio) {
 | 
					if (defined $::radio) {
 | 
				
			||||||
  while (($eid, $rh_eid) = each %::eid_map) {
 | 
					  while (my ($eid, $rh_eid) = each %::eid_map) {
 | 
				
			||||||
    if ((defined $rh_eid->{parent}) && ($rh_eid->{parent} eq $::radio)) {
 | 
					    if ((defined $rh_eid->{parent}) && ($rh_eid->{parent} eq $::radio)) {
 | 
				
			||||||
      push(@interfaces, $rh_eid->{dev});
 | 
					      push(@interfaces, $rh_eid->{dev});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -238,7 +248,7 @@ if (defined $::pattern && $pattern ne "") {
 | 
				
			|||||||
   my $pat = $::pattern;
 | 
					   my $pat = $::pattern;
 | 
				
			||||||
   $pat =~ s/[+]//g;
 | 
					   $pat =~ s/[+]//g;
 | 
				
			||||||
   # collect all stations on that resource add them to @interfaces
 | 
					   # collect all stations on that resource add them to @interfaces
 | 
				
			||||||
   while (($eid, $rh_eid) = each %::eid_map) {
 | 
					   while (my($eid, $rh_eid) = each %::eid_map) {
 | 
				
			||||||
     if ((defined $rh_eid->{dev}) && ($rh_eid->{dev} =~ /$pat/)) {
 | 
					     if ((defined $rh_eid->{dev}) && ($rh_eid->{dev} =~ /$pat/)) {
 | 
				
			||||||
       push(@interfaces, $rh_eid->{dev});
 | 
					       push(@interfaces, $rh_eid->{dev});
 | 
				
			||||||
     }
 | 
					     }
 | 
				
			||||||
@@ -277,11 +287,16 @@ Parameters that can be replaced:
 | 
				
			|||||||
=cut
 | 
					=cut
 | 
				
			||||||
sub create_generic {
 | 
					sub create_generic {
 | 
				
			||||||
   my ($name, $port_name, $eid)=@_;
 | 
					   my ($name, $port_name, $eid)=@_;
 | 
				
			||||||
 | 
					   #print "= 1 =====================================================\n";
 | 
				
			||||||
 | 
					   #print Dumper($eid);
 | 
				
			||||||
   my $endp_name = "${name_pref}_${port_name}";
 | 
					   my $endp_name = "${name_pref}_${port_name}";
 | 
				
			||||||
   my $type = "gen_generic";
 | 
					   my $type = "gen_generic";
 | 
				
			||||||
   my $rh_eid = $::eid_map{$eid};
 | 
					   my $rh_idr = $::eid_map{$eid};
 | 
				
			||||||
   my $port_ip = $rh_eid->{ip};
 | 
					   my $port_ip = $rh_idr->{'ip'};
 | 
				
			||||||
   #print Dumper($rh_eid);
 | 
					   #print Dumper($rh_idr);
 | 
				
			||||||
 | 
					   #print Dumper($rh_idr->{'ip'});
 | 
				
			||||||
 | 
					   #print "$endp_name PORT_IP $port_ip \n";
 | 
				
			||||||
 | 
					   #print "= 2 =====================================================\n";
 | 
				
			||||||
   my $ping_cmd = "lfping -I $port_name $::dest_ip";
 | 
					   my $ping_cmd = "lfping -I $port_name $::dest_ip";
 | 
				
			||||||
   if ((defined $::ref_cmd) && ($::ref_cmd ne "")) {
 | 
					   if ((defined $::ref_cmd) && ($::ref_cmd ne "")) {
 | 
				
			||||||
      $ping_cmd = $::ref_cmd;
 | 
					      $ping_cmd = $::ref_cmd;
 | 
				
			||||||
@@ -306,7 +321,8 @@ sub create_generic {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
   }
 | 
					   }
 | 
				
			||||||
   $::command_map{$eid} = $ping_cmd;
 | 
					   $::command_map{$eid} = $ping_cmd;
 | 
				
			||||||
   print "CMD: $ping_cmd\n";
 | 
					   
 | 
				
			||||||
 | 
					   print "CMD: $ping_cmd\n" if ($::verbose);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   $::utils->doCmd($::utils->fmt_cmd("add_gen_endp", $endp_name, 1, $::resource, $port_name, $type));
 | 
					   $::utils->doCmd($::utils->fmt_cmd("add_gen_endp", $endp_name, 1, $::resource, $port_name, $type));
 | 
				
			||||||
   $::utils->doCmd("set_gen_cmd $endp_name $ping_cmd");
 | 
					   $::utils->doCmd("set_gen_cmd $endp_name $ping_cmd");
 | 
				
			||||||
@@ -329,17 +345,38 @@ sub create_generic {
 | 
				
			|||||||
#print Dumper(\@interfaces);
 | 
					#print Dumper(\@interfaces);
 | 
				
			||||||
#print Dumper(\%::eid_map);
 | 
					#print Dumper(\%::eid_map);
 | 
				
			||||||
our %command_map = ();
 | 
					our %command_map = ();
 | 
				
			||||||
#my $type = "gen_generic";
 | 
					my @map_keys = sort keys %eid_map;
 | 
				
			||||||
for my $port (sort @interfaces) {
 | 
					for my $port (sort @interfaces) {
 | 
				
			||||||
   my $endp_name = "${name_pref}_$port";
 | 
					   my $endp_name = "${name_pref}_$port";
 | 
				
			||||||
   my $matching_eid = "";
 | 
					   my $matching_eid = "";
 | 
				
			||||||
   while (my ($eid, $rh_eid) = each %eid_map) {
 | 
					   #print "Searching for port $port ";
 | 
				
			||||||
      if ($rh_eid->{dev} eq $port) {
 | 
					   #while (my ($eid, $rh_pid) = each %eid_map) {
 | 
				
			||||||
 | 
					   for my $eid (@map_keys) {
 | 
				
			||||||
 | 
					      my $rh_pid = $eid_map{$eid};
 | 
				
			||||||
 | 
					      #print " $port/$rh_pid->{dev} ";
 | 
				
			||||||
 | 
					      if ("$port" eq "$rh_pid->{dev}") {
 | 
				
			||||||
 | 
					         #print " ** ";
 | 
				
			||||||
         $matching_eid = $eid;
 | 
					         $matching_eid = $eid;
 | 
				
			||||||
         last;
 | 
					         last;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
   }
 | 
					   }
 | 
				
			||||||
   print "EID $matching_eid\n";
 | 
					   if ($matching_eid eq "") {
 | 
				
			||||||
 | 
					      print "\nSkipping $port no eid [$matching_eid]\n";
 | 
				
			||||||
 | 
					      next;
 | 
				
			||||||
 | 
					   }
 | 
				
			||||||
 | 
					   #print "\n= 3 =====================================================\n";
 | 
				
			||||||
 | 
					   #print " $matching_eid => ".$eid_map{$matching_eid}->{dev}."\n";
 | 
				
			||||||
 | 
					   #print Dumper($eid_map{$matching_eid});
 | 
				
			||||||
 | 
					   #print "= 4 =====================================================\n";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   if (! (defined $eid_map{$matching_eid}->{ip}) 
 | 
				
			||||||
 | 
					      || $eid_map{$matching_eid}->{ip} eq ""
 | 
				
			||||||
 | 
					      || $eid_map{$matching_eid}->{ip} eq "0.0.0.0") {
 | 
				
			||||||
 | 
					      print "\nSkipping $port: ".$eid_map{$matching_eid}->{ip}."\n";
 | 
				
			||||||
 | 
					      sleep 1;
 | 
				
			||||||
 | 
					      next;
 | 
				
			||||||
 | 
					   }
 | 
				
			||||||
   create_generic($endp_name, $port, $matching_eid);
 | 
					   create_generic($endp_name, $port, $matching_eid);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#print Dumper(\%command_map);
 | 
					#print Dumper(\%command_map);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user