#!/usr/bin/env perl use warnings; use strict; # Description: Networking for Unix systems our $VERSION = '1.3'; # Linux network module for sysinfo client # Author: R. W. Rodolico # Date: 2016-04-08 # module to get network interface information for Linux systems # assumes ifconfig is on the system and executable by the user # NOTE: this takes the ifconfig output and parses it, so changes to # this output invalidates this module # # 2018-05-20 RWR # Modified to use ip vs ifconfig, and changed the regex's. # kept the original ifconfig code but commented it out BEGIN { push @INC, shift; } use library; exit 1 unless lc ( &getOperatingSystem() ) eq 'linux'; my $command = &validCommandOnSystem('ip'); exit 1 unless $command; my $CATEGORY = 'network'; # get list of all interfaces except lo my @interfaces = `$command -o a s | cut -d' ' -f2 | sort | uniq | grep -v 'lo'`; chomp @interfaces; my $ipv4Regex = '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b'; my $ipv6Regex = '(([a-f0-9]+):{1,2})+[a-z0-9]+'; foreach my $if ( @interfaces ) { foreach my $line ( `$command address show $if` ) { # mtu if ( $line =~ m/mtu\s+(\d+)/i ) { print "$CATEGORY\t$if\tmtu\t$1\n"; # MAC } elsif ( $line =~ m/((([a-f0-9]{2}):){5}[a-f0-9]+)/i ) { print "$CATEGORY\t$if\tmac\t$1\n"; # inet 6 } elsif ( $line =~ m"inet6\s*((([a-f0-9]+):{1,2})+[a-z0-9]+)/(\d{1,2})"i ) { print "$CATEGORY\t$if\tip6address\t$1\n"; print "$CATEGORY\t$if\tip6networkbits\t$4\n"; # inet 4 } elsif ( $line =~ m"inet.*($ipv4Regex)/(\d{2})" ) { print "$CATEGORY\t$if\taddress\t$1\n"; print "$CATEGORY\t$if\tnetmask\t$2\n"; } } } # this is the original code, in case we ever need it again ## eth1 Link encap:Ethernet HWaddr 00:16:3E:1F:EF:4F #my $regexHWADDR = 'hwaddr[^0-9a-f:]+([0-9a-f:]+)[^0-9a-f:]'; ## inet addr:10.110.110.2 Bcast:10.110.110.255 Mask:255.255.255.0 #my $regexINET = 'inet addr:\s*([0-9.]+)[^0-9].*mask:([0-9.]+)'; ## inet6 addr: fe80::216:3eff:fe1f:ef4f/64 Scope:Link #my $regexINET6 = 'inet6 addr: ([0-9a-f:]+)\/([0-9]+)[^0-9]'; ## UP LOOPBACK RUNNING MTU:16436 Metric:1 #my $regexMTU = 'mtu:([0-9]+)[^0-9]'; #my $temp = qx/$command/; #my @temp = split( "\n", $temp ); #my $currentIF; #while ( @temp ) { # my $line = shift @temp; # next unless $line; # if ( $line =~ m/^([^ ]+) /) { # if the first character is not a space, starting new entry # $currentIF = $1; # if ( $line =~ m/$regexHWADDR/i ) { # print "$CATEGORY\t$currentIF\tmac\t$1\n"; # } else { # print "$CATEGORY\t$currentIF\tmac\tunknown\n"; # } # } elsif ( $line =~ m/$regexINET/i ) { # print "$CATEGORY\t$currentIF\taddress\t$1\n"; # print "$CATEGORY\t$currentIF\tnetmask\t$2\n"; # } elsif ( $line =~ m/$regexINET6/i ) { # print "$CATEGORY\t$currentIF\tip6address\t$1\n"; # print "$CATEGORY\t$currentIF\tip6networkbits\t$2\n"; # } elsif ( $line =~ m/$regexMTU/i ) { # print "$CATEGORY\t$currentIF\tmtu\t$1\n"; # } #} #