| Line 1... |
Line 1... |
| 1 |
#!/usr/bin/env perl
|
1 |
#!/usr/bin/env perl
|
| 2 |
use warnings;
|
2 |
use warnings;
|
| 3 |
use strict;
|
3 |
use strict;
|
| 4 |
|
4 |
|
| 5 |
# Description: Networking for Unix systems
|
- |
|
| 6 |
|
- |
|
| 7 |
our $VERSION = '1.3';
|
5 |
use version ; our $VERSION = '1.3';
|
| 8 |
|
6 |
|
| 9 |
# Linux network module for sysinfo client
|
7 |
# Linux network module for sysinfo client
|
| 10 |
# Author: R. W. Rodolico
|
8 |
# Author: R. W. Rodolico
|
| 11 |
# Date: 2016-04-08
|
9 |
# Date: 2016-04-08
|
| 12 |
|
10 |
|
| 13 |
# module to get network interface information for Linux systems
|
11 |
# module to get network interface information for Linux systems
|
| 14 |
# assumes ifconfig is on the system and executable by the user
|
12 |
# assumes ifconfig is on the system and executable by the user
|
| 15 |
# NOTE: this takes the ifconfig output and parses it, so changes to
|
13 |
# NOTE: this takes the ifconfig output and parses it, so changes to
|
| 16 |
# this output invalidates this module
|
14 |
# this output invalidates this module
|
| 17 |
#
|
15 |
#
|
| - |
|
16 |
# Revision History
|
| 18 |
# 2018-05-20 RWR
|
17 |
# 2018-05-20 RWR
|
| 19 |
# Modified to use ip vs ifconfig, and changed the regex's.
|
18 |
# Modified to use ip vs ifconfig, and changed the regex's.
|
| 20 |
# kept the original ifconfig code but commented it out
|
19 |
# kept the original ifconfig code but commented it out
|
| 21 |
|
20 |
|
| 22 |
# find our location and use it for searching for libraries
|
21 |
# find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
|
| - |
|
22 |
# or, if run interactively, in the parent of the modules
|
| 23 |
BEGIN {
|
23 |
BEGIN {
|
| 24 |
use FindBin;
|
24 |
use FindBin;
|
| 25 |
use File::Spec;
|
25 |
use File::Spec;
|
| 26 |
use lib File::Spec->catdir($FindBin::Bin);
|
26 |
# prepend the bin directory and its parent
|
| 27 |
eval( 'use library;' ); die "Could not find library.pm in the code directory\n" if $@;
|
27 |
use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
|
| 28 |
eval( 'use Data::Dumper;' );
|
28 |
eval( 'use library;' );
|
| - |
|
29 |
die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
|
| 29 |
}
|
30 |
}
|
| 30 |
|
31 |
|
| - |
|
32 |
#####
|
| 31 |
# check for valid OS.
|
33 |
##### Change these to match your needs
|
| - |
|
34 |
#####
|
| - |
|
35 |
|
| - |
|
36 |
# Make this a list of all the modules we are going to use. You can replace undef with the version you need, if you like
|
| - |
|
37 |
my $modulesList = {
|
| 32 |
exit 1 unless &checkOS( { 'linux' => undef } );
|
38 |
'Data::Dumper' => undef,
|
| - |
|
39 |
};
|
| 33 |
|
40 |
|
| 34 |
# check for required commands, return 2 if they don't exist. Enter an full list of all commands required. If one doesn't exist
|
41 |
# hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
|
| 35 |
# script returns a 2
|
42 |
# the full path (from which or where)
|
| 36 |
foreach my $command ( 'ip' ) {
|
43 |
my $commandsList = {
|
| 37 |
exit 2 unless &validCommandOnSystem( $command );
|
44 |
'ip' => undef,
|
| - |
|
45 |
};
|
| 38 |
}
|
46 |
|
| - |
|
47 |
# list of operating systems this module can be used on.
|
| - |
|
48 |
my $osList = {
|
| - |
|
49 |
# 'mswin32' => undef,
|
| - |
|
50 |
# 'freebsd' => undef,
|
| - |
|
51 |
'linux' => undef,
|
| - |
|
52 |
};
|
| 39 |
|
53 |
|
| - |
|
54 |
# the category the return data should go into. See sysinfo for a list
|
| 40 |
my $CATEGORY = 'network';
|
55 |
my $CATEGORY = 'network';
|
| 41 |
|
56 |
|
| - |
|
57 |
#####
|
| - |
|
58 |
##### End of required
|
| - |
|
59 |
#####
|
| - |
|
60 |
|
| - |
|
61 |
# some variables needed for our system
|
| - |
|
62 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
| - |
|
63 |
my @out; # temporary location for each line of output
|
| - |
|
64 |
|
| - |
|
65 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
| - |
|
66 |
for my $module ( keys %$modulesList ) {
|
| - |
|
67 |
eval ( "use $module;" );
|
| - |
|
68 |
push @out, "$errorPrepend Could not load $module" if $@;
|
| - |
|
69 |
}
|
| - |
|
70 |
|
| - |
|
71 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
| - |
|
72 |
push @out, "$errorPrepend Invalid Operating System";
|
| - |
|
73 |
}
|
| - |
|
74 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
| - |
|
75 |
push @out, "$errorPrepend Can not find some commands needed";
|
| - |
|
76 |
}
|
| - |
|
77 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
| - |
|
78 |
#####
|
| - |
|
79 |
##### Your code starts here. Remember to push all output onto @out
|
| - |
|
80 |
#####
|
| 42 |
# get list of all interfaces except lo
|
81 |
# get list of all interfaces except lo
|
| 43 |
my @interfaces = `ip -o a s | cut -d' ' -f2 | sort | uniq | grep -v 'lo'`;
|
82 |
my @interfaces = `ip -o a s | cut -d' ' -f2 | sort | uniq | grep -v 'lo'`;
|
| 44 |
chomp @interfaces;
|
83 |
chomp @interfaces;
|
| 45 |
|
84 |
|
| 46 |
my $ipv4Regex = '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b';
|
85 |
my $ipv4Regex = '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b';
|
| 47 |
my $ipv6Regex = '(([a-f0-9]+):{1,2})+[a-z0-9]+';
|
86 |
my $ipv6Regex = '(([a-f0-9]+):{1,2})+[a-z0-9]+';
|
| 48 |
|
87 |
|
| 49 |
foreach my $if ( @interfaces ) {
|
88 |
foreach my $if ( @interfaces ) {
|
| 50 |
foreach my $line ( `ip address show $if` ) {
|
89 |
foreach my $line ( `ip address show $if` ) {
|
| 51 |
# mtu
|
90 |
# mtu
|
| 52 |
if ( $line =~ m/mtu\s+(\d+)/i ) {
|
91 |
if ( $line =~ m/mtu\s+(\d+)/i ) {
|
| 53 |
print "$CATEGORY\t$if\tmtu\t$1\n";
|
92 |
push @out, "$CATEGORY\t$if\tmtu\t$1";
|
| 54 |
# MAC
|
93 |
# MAC
|
| 55 |
} elsif ( $line =~ m/((([a-f0-9]{2}):){5}[a-f0-9]+)/i ) {
|
94 |
} elsif ( $line =~ m/((([a-f0-9]{2}):){5}[a-f0-9]+)/i ) {
|
| 56 |
print "$CATEGORY\t$if\tmac\t$1\n";
|
95 |
push @out, "$CATEGORY\t$if\tmac\t$1";
|
| 57 |
# inet 6
|
96 |
# inet 6
|
| 58 |
} elsif ( $line =~ m"inet6\s*((([a-f0-9]+):{1,2})+[a-z0-9]+)/(\d{1,2})"i ) {
|
97 |
} elsif ( $line =~ m"inet6\s*((([a-f0-9]+):{1,2})+[a-z0-9]+)/(\d{1,2})"i ) {
|
| 59 |
print "$CATEGORY\t$if\tip6address\t$1\n";
|
98 |
push @out, "$CATEGORY\t$if\tip6address\t$1";
|
| 60 |
print "$CATEGORY\t$if\tip6networkbits\t$4\n";
|
99 |
push @out, "$CATEGORY\t$if\tip6networkbits\t$4";
|
| 61 |
# inet 4
|
100 |
# inet 4
|
| 62 |
} elsif ( $line =~ m"inet.*($ipv4Regex)/(\d{2})" ) {
|
101 |
} elsif ( $line =~ m"inet.*($ipv4Regex)/(\d{2})" ) {
|
| 63 |
print "$CATEGORY\t$if\taddress\t$1\n";
|
102 |
push @out, "$CATEGORY\t$if\taddress\t$1";
|
| 64 |
print "$CATEGORY\t$if\tnetmask\t$2\n";
|
103 |
push @out, "$CATEGORY\t$if\tnetmask\t$2";
|
| - |
|
104 |
}
|
| 65 |
}
|
105 |
}
|
| 66 |
}
|
106 |
}
|
| - |
|
107 |
#####
|
| - |
|
108 |
##### Your code ends here.
|
| - |
|
109 |
#####
|
| 67 |
}
|
110 |
}
|
| 68 |
|
111 |
|
| 69 |
# this is the original code, in case we ever need it again
|
112 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
| - |
|
113 |
print join( "\n", @out ) . "\n" unless caller;
|
| - |
|
114 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
| - |
|
115 |
my $return = join( "\n", @out );
|
| 70 |
|
116 |
|
| 71 |
## eth1 Link encap:Ethernet HWaddr 00:16:3E:1F:EF:4F
|
- |
|
| 72 |
#my $regexHWADDR = 'hwaddr[^0-9a-f:]+([0-9a-f:]+)[^0-9a-f:]';
|
- |
|
| 73 |
## inet addr:10.110.110.2 Bcast:10.110.110.255 Mask:255.255.255.0
|
- |
|
| 74 |
#my $regexINET = 'inet addr:\s*([0-9.]+)[^0-9].*mask:([0-9.]+)';
|
- |
|
| 75 |
## inet6 addr: fe80::216:3eff:fe1f:ef4f/64 Scope:Link
|
- |
|
| 76 |
#my $regexINET6 = 'inet6 addr: ([0-9a-f:]+)\/([0-9]+)[^0-9]';
|
- |
|
| 77 |
## UP LOOPBACK RUNNING MTU:16436 Metric:1
|
- |
|
| 78 |
#my $regexMTU = 'mtu:([0-9]+)[^0-9]';
|
- |
|
| 79 |
#my $temp = qx/$command/;
|
- |
|
| 80 |
#my @temp = split( "\n", $temp );
|
- |
|
| 81 |
#my $currentIF;
|
- |
|
| 82 |
#while ( @temp ) {
|
- |
|
| 83 |
# my $line = shift @temp;
|
- |
|
| 84 |
# next unless $line;
|
- |
|
| 85 |
# if ( $line =~ m/^([^ ]+) /) { # if the first character is not a space, starting new entry
|
- |
|
| 86 |
# $currentIF = $1;
|
- |
|
| 87 |
# if ( $line =~ m/$regexHWADDR/i ) {
|
- |
|
| 88 |
# print "$CATEGORY\t$currentIF\tmac\t$1\n";
|
- |
|
| 89 |
# } else {
|
- |
|
| 90 |
# print "$CATEGORY\t$currentIF\tmac\tunknown\n";
|
- |
|
| 91 |
# }
|
- |
|
| 92 |
# } elsif ( $line =~ m/$regexINET/i ) {
|
- |
|
| 93 |
# print "$CATEGORY\t$currentIF\taddress\t$1\n";
|
- |
|
| 94 |
# print "$CATEGORY\t$currentIF\tnetmask\t$2\n";
|
- |
|
| 95 |
# } elsif ( $line =~ m/$regexINET6/i ) {
|
- |
|
| 96 |
# print "$CATEGORY\t$currentIF\tip6address\t$1\n";
|
- |
|
| 97 |
# print "$CATEGORY\t$currentIF\tip6networkbits\t$2\n";
|
- |
|
| 98 |
# } elsif ( $line =~ m/$regexMTU/i ) {
|
- |
|
| 99 |
# print "$CATEGORY\t$currentIF\tmtu\t$1\n";
|
- |
|
| 100 |
# }
|
- |
|
| 101 |
#}
|
- |
|
| 102 |
#
|
- |
|