| Line 69... |
Line 69... |
| 69 |
# Added ability to delete entries which haven't been seen since $config->{'ttl'} seconds. ttl can be
|
69 |
# Added ability to delete entries which haven't been seen since $config->{'ttl'} seconds. ttl can be
|
| 70 |
# an integer followed by a unit, with units being single char h,d,w,m,y (hours, days, weeks, months, years)
|
70 |
# an integer followed by a unit, with units being single char h,d,w,m,y (hours, days, weeks, months, years)
|
| 71 |
# added ability to set so it will only refresh dns and device names every $refresh seconds, decreasing
|
71 |
# added ability to set so it will only refresh dns and device names every $refresh seconds, decreasing
|
| 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 |
#
|
| - |
|
75 |
# 20230326 RWR v3.0.2
|
| - |
|
76 |
# set it up so if net::dns not loadable, will revert to ionet
|
| 74 |
|
77 |
|
| 75 |
# for Debian, to get the required libraries
|
78 |
# for Debian, to get the required libraries
|
| 76 |
# apt install libyaml-tiny-perl libnet-dns-perl snmp
|
79 |
# apt install libyaml-tiny-perl libnet-dns-perl snmp
|
| 77 |
|
80 |
|
| 78 |
use strict;
|
81 |
use strict;
|
| Line 80... |
Line 83... |
| 80 |
use Data::Dumper; # only used for debugging
|
83 |
use Data::Dumper; # only used for debugging
|
| 81 |
use Socket; # for reverse dns entries
|
84 |
use Socket; # for reverse dns entries
|
| 82 |
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
|
85 |
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
|
| 83 |
use File::Spec; # find location of script so we can put storage in same directory
|
86 |
use File::Spec; # find location of script so we can put storage in same directory
|
| 84 |
use File::Basename;
|
87 |
use File::Basename;
|
| 85 |
use Net::DNS; # dns lookup, apt install libnet-dns-perl
|
- |
|
| 86 |
|
- |
|
| 87 |
|
88 |
|
| 88 |
my $DEBUG = 0;
|
89 |
my $DEBUG = 0;
|
| 89 |
|
90 |
|
| 90 |
# define the version number
|
91 |
# define the version number
|
| 91 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
92 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
| Line 168... |
Line 169... |
| 168 |
return $return;
|
169 |
return $return;
|
| 169 |
}
|
170 |
}
|
| 170 |
|
171 |
|
| 171 |
# do a reverse dns lookup of an IP address and return the hostname
|
172 |
# do a reverse dns lookup of an IP address and return the hostname
|
| 172 |
# note that $dns is an instance of Net::DNS
|
173 |
# note that $dns is an instance of Net::DNS
|
| - |
|
174 |
# if it is not set, will revert to using inet_aton, which only
|
| - |
|
175 |
# works off of the resolv.conf on the local machine
|
| 173 |
sub getHostName {
|
176 |
sub getHostName {
|
| 174 |
my ($dns, $ip ) = @_;
|
177 |
my ($dns, $ip ) = @_;
|
| - |
|
178 |
if ( $dns ) {
|
| 175 |
foreach my $resource ( @$dns ) {
|
179 |
foreach my $resource ( @$dns ) {
|
| 176 |
my $reply = $resource->search( $ip );
|
180 |
my $reply = $resource->search( $ip );
|
| 177 |
next unless defined $reply;
|
181 |
next unless defined $reply;
|
| 178 |
my @answer = grep { ! /^((;.*)|(\s*))$/ } split( "\n", $reply->string );
|
182 |
my @answer = grep { ! /^((;.*)|(\s*))$/ } split( "\n", $reply->string );
|
| 179 |
$answer[0] =~ m/\s([a-z0-9.-]+)\.$/;
|
183 |
$answer[0] =~ m/\s([a-z0-9.-]+)\.$/;
|
| 180 |
return $1;
|
184 |
return $1;
|
| - |
|
185 |
}
|
| - |
|
186 |
} else {
|
| - |
|
187 |
return gethostbyaddr( inet_aton( $ip ), AF_INET );
|
| 181 |
}
|
188 |
}
|
| 182 |
return '';
|
189 |
return '';
|
| 183 |
}
|
190 |
}
|
| 184 |
|
191 |
|
| 185 |
# Load configuration file, die if we could not find it
|
192 |
# Load configuration file, die if we could not find it
|
| Line 472... |
Line 479... |
| 472 |
if ( -e $STATEFILE ) {
|
479 |
if ( -e $STATEFILE ) {
|
| 473 |
my $yaml = YAML::Tiny->read( $STATEFILE );
|
480 |
my $yaml = YAML::Tiny->read( $STATEFILE );
|
| 474 |
$saveFile = \%{ $yaml->[0] };
|
481 |
$saveFile = \%{ $yaml->[0] };
|
| 475 |
}
|
482 |
}
|
| 476 |
|
483 |
|
| - |
|
484 |
|
| - |
|
485 |
my $dnsResource = 0;
|
| - |
|
486 |
eval {
|
| - |
|
487 |
use Net::DNS; # dns lookup, apt install libnet-dns-perl
|
| - |
|
488 |
$dnsResource = [];
|
| 477 |
# set up one dns resource for every router we have
|
489 |
# set up one dns resource for every router we have
|
| 478 |
# too many failures when trying to define both of them at the same time
|
490 |
# too many failures when trying to define both of them at the same time
|
| 479 |
my $dnsResource = [];
|
- |
|
| 480 |
foreach my $router ( keys %{ $config->{'routers'} } ) {
|
491 |
foreach my $router ( keys %{ $config->{'routers'} } ) {
|
| 481 |
push @$dnsResource, Net::DNS::Resolver->new(
|
492 |
push @$dnsResource, Net::DNS::Resolver->new(
|
| 482 |
'nameservers' => [ $router ],
|
493 |
'nameservers' => [ $router ],
|
| 483 |
'recurse' => 0
|
494 |
'recurse' => 0
|
| 484 |
);
|
495 |
);
|
| - |
|
496 |
}
|
| 485 |
}
|
497 |
};
|
| 486 |
|
498 |
|
| 487 |
my $refresh = 1; # determines if we get extra information like switch info, port aliases, etc...
|
499 |
my $refresh = 1; # determines if we get extra information like switch info, port aliases, etc...
|
| 488 |
if ( defined( $config->{'refresh'} ) ) {
|
500 |
if ( defined( $config->{'refresh'} ) ) {
|
| 489 |
# get the number of seconds between refreshes
|
501 |
# get the number of seconds between refreshes
|
| 490 |
$refresh = &calcAge( $config->{'refresh'} );
|
502 |
$refresh = &calcAge( $config->{'refresh'} );
|