20 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
26 |
rodolico |
3 |
use strict;
|
2 |
rodolico |
4 |
|
37 |
rodolico |
5 |
# Description: Networking for Unix systems
|
20 |
rodolico |
6 |
|
65 |
rodolico |
7 |
our $VERSION = '1.3';
|
37 |
rodolico |
8 |
|
2 |
rodolico |
9 |
# Linux network module for sysinfo client
|
|
|
10 |
# Author: R. W. Rodolico
|
20 |
rodolico |
11 |
# Date: 2016-04-08
|
2 |
rodolico |
12 |
|
|
|
13 |
# module to get network interface information for Linux systems
|
|
|
14 |
# assumes ifconfig is on the system and executable by the user
|
|
|
15 |
# NOTE: this takes the ifconfig output and parses it, so changes to
|
|
|
16 |
# this output invalidates this module
|
65 |
rodolico |
17 |
#
|
|
|
18 |
# 2018-05-20 RWR
|
|
|
19 |
# Modified to use ip vs ifconfig, and changed the regex's.
|
|
|
20 |
# kept the original ifconfig code but commented it out
|
2 |
rodolico |
21 |
|
|
|
22 |
BEGIN {
|
|
|
23 |
push @INC, shift;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
use library;
|
|
|
27 |
|
48 |
rodolico |
28 |
exit 1 unless lc ( &getOperatingSystem() ) eq 'linux';
|
|
|
29 |
|
65 |
rodolico |
30 |
my $command = &validCommandOnSystem('ip');
|
2 |
rodolico |
31 |
|
|
|
32 |
exit 1 unless $command;
|
|
|
33 |
|
|
|
34 |
my $CATEGORY = 'network';
|
65 |
rodolico |
35 |
|
|
|
36 |
# get list of all interfaces except lo
|
|
|
37 |
my @interfaces = `$command -o a s | cut -d' ' -f2 | sort | uniq | grep -v 'lo'`;
|
|
|
38 |
chomp @interfaces;
|
|
|
39 |
|
|
|
40 |
my $ipv4Regex = '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b';
|
|
|
41 |
my $ipv6Regex = '(([a-f0-9]+):{1,2})+[a-z0-9]+';
|
|
|
42 |
|
|
|
43 |
foreach my $if ( @interfaces ) {
|
|
|
44 |
foreach my $line ( `$command address show $if` ) {
|
|
|
45 |
# mtu
|
|
|
46 |
if ( $line =~ m/mtu\s+(\d+)/i ) {
|
|
|
47 |
print "$CATEGORY\t$if\tmtu\t$1\n";
|
|
|
48 |
# MAC
|
|
|
49 |
} elsif ( $line =~ m/((([a-f0-9]{2}):){5}[a-f0-9]+)/i ) {
|
|
|
50 |
print "$CATEGORY\t$if\tmac\t$1\n";
|
|
|
51 |
# inet 6
|
|
|
52 |
} elsif ( $line =~ m"inet6\s*((([a-f0-9]+):{1,2})+[a-z0-9]+)/(\d{1,2})"i ) {
|
|
|
53 |
print "$CATEGORY\t$if\tip6address\t$1\n";
|
|
|
54 |
print "$CATEGORY\t$if\tip6networkbits\t$4\n";
|
|
|
55 |
# inet 4
|
|
|
56 |
} elsif ( $line =~ m"inet.*($ipv4Regex)/(\d{2})" ) {
|
|
|
57 |
print "$CATEGORY\t$if\taddress\t$1\n";
|
|
|
58 |
print "$CATEGORY\t$if\tnetmask\t$2\n";
|
2 |
rodolico |
59 |
}
|
|
|
60 |
}
|
|
|
61 |
}
|
65 |
rodolico |
62 |
|
|
|
63 |
# this is the original code, in case we ever need it again
|
|
|
64 |
|
|
|
65 |
## eth1 Link encap:Ethernet HWaddr 00:16:3E:1F:EF:4F
|
|
|
66 |
#my $regexHWADDR = 'hwaddr[^0-9a-f:]+([0-9a-f:]+)[^0-9a-f:]';
|
|
|
67 |
## inet addr:10.110.110.2 Bcast:10.110.110.255 Mask:255.255.255.0
|
|
|
68 |
#my $regexINET = 'inet addr:\s*([0-9.]+)[^0-9].*mask:([0-9.]+)';
|
|
|
69 |
## inet6 addr: fe80::216:3eff:fe1f:ef4f/64 Scope:Link
|
|
|
70 |
#my $regexINET6 = 'inet6 addr: ([0-9a-f:]+)\/([0-9]+)[^0-9]';
|
|
|
71 |
## UP LOOPBACK RUNNING MTU:16436 Metric:1
|
|
|
72 |
#my $regexMTU = 'mtu:([0-9]+)[^0-9]';
|
|
|
73 |
#my $temp = qx/$command/;
|
|
|
74 |
#my @temp = split( "\n", $temp );
|
|
|
75 |
#my $currentIF;
|
|
|
76 |
#while ( @temp ) {
|
|
|
77 |
# my $line = shift @temp;
|
|
|
78 |
# next unless $line;
|
|
|
79 |
# if ( $line =~ m/^([^ ]+) /) { # if the first character is not a space, starting new entry
|
|
|
80 |
# $currentIF = $1;
|
|
|
81 |
# if ( $line =~ m/$regexHWADDR/i ) {
|
|
|
82 |
# print "$CATEGORY\t$currentIF\tmac\t$1\n";
|
|
|
83 |
# } else {
|
|
|
84 |
# print "$CATEGORY\t$currentIF\tmac\tunknown\n";
|
|
|
85 |
# }
|
|
|
86 |
# } elsif ( $line =~ m/$regexINET/i ) {
|
|
|
87 |
# print "$CATEGORY\t$currentIF\taddress\t$1\n";
|
|
|
88 |
# print "$CATEGORY\t$currentIF\tnetmask\t$2\n";
|
|
|
89 |
# } elsif ( $line =~ m/$regexINET6/i ) {
|
|
|
90 |
# print "$CATEGORY\t$currentIF\tip6address\t$1\n";
|
|
|
91 |
# print "$CATEGORY\t$currentIF\tip6networkbits\t$2\n";
|
|
|
92 |
# } elsif ( $line =~ m/$regexMTU/i ) {
|
|
|
93 |
# print "$CATEGORY\t$currentIF\tmtu\t$1\n";
|
|
|
94 |
# }
|
|
|
95 |
#}
|
|
|
96 |
#
|