| 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
|
5 |
use version ; our $VERSION = 'v1.2.0';
|
| 6 |
|
6 |
|
| 7 |
our $VERSION = '1.2';
|
- |
|
| 8 |
|
- |
|
| 9 |
# Linux network module for sysinfo client
|
7 |
# BSD 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 BSD 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
|
| - |
|
15 |
#
|
| - |
|
16 |
# Revision History
|
| - |
|
17 |
#
|
| - |
|
18 |
#
|
| 17 |
|
19 |
|
| 18 |
# find our location and use it for searching for libraries
|
20 |
# find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
|
| - |
|
21 |
# or, if run interactively, in the parent of the modules
|
| 19 |
BEGIN {
|
22 |
BEGIN {
|
| 20 |
use FindBin;
|
23 |
use FindBin;
|
| 21 |
use File::Spec;
|
24 |
use File::Spec;
|
| 22 |
use lib File::Spec->catdir($FindBin::Bin);
|
25 |
# prepend the bin directory and its parent
|
| 23 |
eval( 'use library;' ); die "Could not find library.pm in the code directory\n" if $@;
|
26 |
use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
|
| 24 |
eval( 'use Data::Dumper;' );
|
27 |
eval( 'use library;' );
|
| - |
|
28 |
die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
|
| 25 |
}
|
29 |
}
|
| 26 |
|
30 |
|
| - |
|
31 |
#####
|
| - |
|
32 |
##### Change these to match your needs
|
| - |
|
33 |
#####
|
| - |
|
34 |
|
| - |
|
35 |
# 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
|
| - |
|
36 |
my $modulesList = {
|
| - |
|
37 |
'Data::Dumper' => undef,
|
| - |
|
38 |
};
|
| - |
|
39 |
|
| - |
|
40 |
# hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
|
| - |
|
41 |
# the full path (from which or where)
|
| - |
|
42 |
my $commandsList = {
|
| 27 |
# check for valid OS.
|
43 |
'ifconfig' => undef,
|
| - |
|
44 |
};
|
| - |
|
45 |
|
| - |
|
46 |
# list of operating systems this module can be used on.
|
| - |
|
47 |
my $osList = {
|
| - |
|
48 |
# 'mswin32' => undef,
|
| 28 |
exit 1 unless &checkOS( { 'freebsd' => undef } );
|
49 |
'freebsd' => undef,
|
| - |
|
50 |
# 'linux' => undef,
|
| - |
|
51 |
};
|
| 29 |
|
52 |
|
| 30 |
# check for required commands, return 2 if they don't exist. Enter an full list of all commands required. If one doesn't exist
|
53 |
# the category the return data should go into. See sysinfo for a list
|
| 31 |
# script returns a 2
|
- |
|
| 32 |
foreach my $command ( '/sbin/ifconfig' ) {
|
54 |
my $CATEGORY = 'network';
|
| 33 |
exit 2 unless &validCommandOnSystem( $command );
|
- |
|
| 34 |
}
|
- |
|
| 35 |
|
55 |
|
| - |
|
56 |
#####
|
| - |
|
57 |
##### End of required
|
| - |
|
58 |
#####
|
| - |
|
59 |
|
| - |
|
60 |
# some variables needed for our system
|
| - |
|
61 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
| - |
|
62 |
my @out; # temporary location for each line of output
|
| - |
|
63 |
|
| - |
|
64 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
| - |
|
65 |
for my $module ( keys %$modulesList ) {
|
| - |
|
66 |
eval ( "use $module;" );
|
| - |
|
67 |
push @out, "$errorPrepend Could not load $module" if $@;
|
| - |
|
68 |
}
|
| 36 |
|
69 |
|
| - |
|
70 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
| 37 |
my $CATEGORY = 'network';
|
71 |
push @out, "$errorPrepend Invalid Operating System";
|
| - |
|
72 |
}
|
| - |
|
73 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
| - |
|
74 |
push @out, "$errorPrepend Can not find some commands needed";
|
| - |
|
75 |
}
|
| - |
|
76 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
| - |
|
77 |
#####
|
| - |
|
78 |
##### Your code starts here. Remember to push all output onto @out
|
| - |
|
79 |
#####
|
| - |
|
80 |
|
| 38 |
# xn0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
|
81 |
# xn0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
|
| 39 |
# ether 00:16:3e:ff:ff:04
|
82 |
# ether 00:16:3e:ff:ff:04
|
| 40 |
my $regexHWADDR = 'ether\s+([0-9a-f:]+)';
|
83 |
my $regexHWADDR = 'ether\s+([0-9a-f:]+)';
|
| 41 |
# inet 74.113.60.189 netmask 0xffffffc0 broadcast 74.113.60.191
|
84 |
# inet 74.113.60.189 netmask 0xffffffc0 broadcast 74.113.60.191
|
| 42 |
my $regexINET = 'inet\s*([0-9.]+)[^0-9].*netmask 0x([0-9a-z]+)';
|
85 |
my $regexINET = 'inet\s*([0-9.]+)[^0-9].*netmask 0x([0-9a-z]+)';
|
| 43 |
# inet6 addr: fe80::216:3eff:fe1f:ef4f/64 Scope:Link
|
86 |
# inet6 addr: fe80::216:3eff:fe1f:ef4f/64 Scope:Link
|
| 44 |
my $regexINET6 = 'inet6\s*([0-9a-f:]+).*prefixlen\s*(\d+)';
|
87 |
my $regexINET6 = 'inet6\s*([0-9a-f:]+).*prefixlen\s*(\d+)';
|
| 45 |
# UP LOOPBACK RUNNING MTU:16436 Metric:1
|
88 |
# UP LOOPBACK RUNNING MTU:16436 Metric:1
|
| 46 |
my $regexMTU = 'mtu\s([0-9]+)';
|
89 |
my $regexMTU = 'mtu\s([0-9]+)';
|
| 47 |
my $temp = qx( /sbin/ifconfig );
|
90 |
my $temp = qx( ifconfig );
|
| 48 |
my @temp = split( "\n", $temp );
|
91 |
my @temp = split( "", $temp );
|
| 49 |
my $currentIF;
|
92 |
my $currentIF;
|
| 50 |
while ( @temp ) {
|
93 |
while ( @temp ) {
|
| 51 |
my $line = shift @temp;
|
94 |
my $line = shift @temp;
|
| 52 |
next unless $line;
|
95 |
next unless $line;
|
| 53 |
if ( $line =~ m/^([^ ]+):/) { # if the first character is not a space, starting new entry
|
96 |
if ( $line =~ m/^([^ ]+):/) { # if the first character is not a space, starting new entry
|
| 54 |
$currentIF = $1;
|
97 |
$currentIF = $1;
|
| 55 |
if ( $line =~ m/$regexMTU/i ) {
|
98 |
if ( $line =~ m/$regexMTU/i ) {
|
| - |
|
99 |
push @out, "$CATEGORY\t$currentIF\tmtu\t$1";
|
| - |
|
100 |
}
|
| - |
|
101 |
} elsif ( $line =~ m/$regexHWADDR/i ) {
|
| - |
|
102 |
push @out, "$CATEGORY\t$currentIF\tmac\t$1";
|
| - |
|
103 |
} elsif ( $line =~ m/$regexINET/i ) {
|
| - |
|
104 |
push @out, "$CATEGORY\t$currentIF\taddress\t$1";
|
| - |
|
105 |
push @out, "$CATEGORY\t$currentIF\tnetmask\t$2";
|
| - |
|
106 |
} elsif ( $line =~ m/$regexINET6/i ) {
|
| - |
|
107 |
push @out, "$CATEGORY\t$currentIF\tip6address\t$1";
|
| - |
|
108 |
push @out, "$CATEGORY\t$currentIF\tip6networkbits\t$2";
|
| - |
|
109 |
} elsif ( $line =~ m/$regexMTU/i ) {
|
| 56 |
print "$CATEGORY\t$currentIF\tmtu\t$1\n";
|
110 |
push @out, "$CATEGORY\t$currentIF\tmtu\t$1";
|
| 57 |
}
|
111 |
}
|
| 58 |
} elsif ( $line =~ m/$regexHWADDR/i ) {
|
- |
|
| 59 |
print "$CATEGORY\t$currentIF\tmac\t$1\n";
|
- |
|
| 60 |
} elsif ( $line =~ m/$regexINET/i ) {
|
- |
|
| 61 |
print "$CATEGORY\t$currentIF\taddress\t$1\n";
|
- |
|
| 62 |
print "$CATEGORY\t$currentIF\tnetmask\t$2\n";
|
- |
|
| 63 |
} elsif ( $line =~ m/$regexINET6/i ) {
|
- |
|
| 64 |
print "$CATEGORY\t$currentIF\tip6address\t$1\n";
|
- |
|
| 65 |
print "$CATEGORY\t$currentIF\tip6networkbits\t$2\n";
|
- |
|
| 66 |
} elsif ( $line =~ m/$regexMTU/i ) {
|
- |
|
| 67 |
print "$CATEGORY\t$currentIF\tmtu\t$1\n";
|
- |
|
| 68 |
}
|
112 |
}
|
| - |
|
113 |
|
| - |
|
114 |
#####
|
| - |
|
115 |
##### Your code ends here.
|
| - |
|
116 |
#####
|
| 69 |
}
|
117 |
}
|
| - |
|
118 |
|
| - |
|
119 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
| - |
|
120 |
print join( "\n", @out ) . "\n" unless caller;
|
| - |
|
121 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
| - |
|
122 |
my $return = join( "\n", @out );
|
| - |
|
123 |
|