Subversion Repositories sysadmin_scripts

Rev

Rev 139 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 139 Rev 157
Line 72... Line 72...
72
# run time to 33% value when not used. Test system decreases from 15s to 5-7s
72
# run time to 33% value when not used. Test system decreases from 15s to 5-7s
73
# reorganized so system runs smoother (test system decreased from 40 seconds to 15)
73
# reorganized so system runs smoother (test system decreased from 40 seconds to 15)
74
#
74
#
75
# 20230326 RWR v3.0.2
75
# 20230326 RWR v3.0.2
76
# set it up so if net::dns not loadable, will revert to ionet
76
# set it up so if net::dns not loadable, will revert to ionet
-
 
77
# 20240810 RWR v3.4.0
-
 
78
# Added code in getDNSNames to use dig if it is available and all other ways have failed to find a hostname
-
 
79
 
77
 
80
 
78
# for Debian, to get the required libraries
81
# for Debian, to get the required libraries
79
# apt install libyaml-tiny-perl libnet-dns-perl snmp
82
# apt install libyaml-tiny-perl libnet-dns-perl snmp
80
 
83
 
81
use strict;
84
use strict;
Line 89... Line 92...
89
my $DEBUG = 0;
92
my $DEBUG = 0;
90
 
93
 
91
# define the version number
94
# define the version number
92
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
95
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
93
use version;
96
use version;
94
our $VERSION = version->declare("v3.3.1");
97
our $VERSION = version->declare("v3.4.0");
95
 
98
 
96
# see https://perldoc.perl.org/Getopt/Long.html
99
# see https://perldoc.perl.org/Getopt/Long.html
97
use Getopt::Long;
100
use Getopt::Long;
98
# allow -vvn (ie, --verbose --verbose --dryrun)
101
# allow -vvn (ie, --verbose --verbose --dryrun)
99
Getopt::Long::Configure ("bundling");
102
Getopt::Long::Configure ("bundling");
Line 403... Line 406...
403
         $macList->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'} if ! $macList->{$mac}->{'hostname'} && $routerARP->{$mac}->{'hostname'};
406
         $macList->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'} if ! $macList->{$mac}->{'hostname'} && $routerARP->{$mac}->{'hostname'};
404
      }
407
      }
405
   }
408
   }
406
}
409
}
407
 
410
 
-
 
411
# As a last ditch effort, we will use dig (if available) to try an get rdns entries
-
 
412
# for the IP's we've found.
-
 
413
sub doDig {
-
 
414
   return unless `which dig`;
-
 
415
   my $macList = shift;
-
 
416
   my @dnsServers = @_;
-
 
417
   foreach my $mac ( keys %$macList ) {
-
 
418
      next if $macList->{$mac}->{'hostname'}; # we already have a name
-
 
419
      next unless $macList->{$mac}->{'ip'}; # can't process if we have no IP
-
 
420
      foreach my $ns ( @dnsServers ) {
-
 
421
         my $name = `dig \@$ns -x $macList->{$mac}->{ip} +short`;
-
 
422
         chomp $name;
-
 
423
         if ( $name ) {
-
 
424
            $macList->{$mac}->{'hostname'} = $name;
-
 
425
            last;
-
 
426
         }
-
 
427
      }
-
 
428
   }
-
 
429
}
-
 
430
 
408
# given a value of a number follwed by a unit (ie, 2w, 12h, 3m), determine 
431
# given a value of a number follwed by a unit (ie, 2w, 12h, 3m), determine 
409
# the unixtime for that long ago.
432
# the unixtime for that long ago.
410
sub calcAge {
433
sub calcAge {
411
   my $time = shift;
434
   my $time = shift;
412
   my %seconds = ( 
435
   my %seconds = ( 
Line 541... Line 564...
541
&getIPAddresses( $saveFile->{'macList'}, $routerARP );
564
&getIPAddresses( $saveFile->{'macList'}, $routerARP );
542
print "Routers after getIPAddresses\n" . Dumper ($routerARP ) if $DEBUG > 2;
565
print "Routers after getIPAddresses\n" . Dumper ($routerARP ) if $DEBUG > 2;
543
# get the DNS names
566
# get the DNS names
544
&getDNSNames( $saveFile->{'macList'}, $dnsResource, $routerARP ) if $refresh;
567
&getDNSNames( $saveFile->{'macList'}, $dnsResource, $routerARP ) if $refresh;
545
print "Routers after getDNSNames\n" . Dumper ($routerARP ) if $DEBUG > 2;
568
print "Routers after getDNSNames\n" . Dumper ($routerARP ) if $DEBUG > 2;
-
 
569
&doDig( $saveFile->{'macList'}, keys %{$config->{'routers'}} );
546
# clean up any old entries
570
# clean up any old entries
547
&cleanUpOldEntries( $config->{'ttl'}, $saveFile ) if $config->{'ttl'} && $refresh;
571
&cleanUpOldEntries( $config->{'ttl'}, $saveFile ) if $config->{'ttl'} && $refresh;
548
# save the file
572
# save the file
549
my $yaml = YAML::Tiny->new( $saveFile );
573
my $yaml = YAML::Tiny->new( $saveFile );
550
$yaml->write( $STATEFILE );
574
$yaml->write( $STATEFILE );