#!/usr/bin/env perl use warnings; use strict; use version ; 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 # # Revision History # 2018-05-20 RWR # Modified to use ip vs ifconfig, and changed the regex's. # kept the original ifconfig code but commented it out # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script # or, if run interactively, in the parent of the modules BEGIN { use FindBin; use File::Spec; # prepend the bin directory and its parent use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); eval( 'use library;' ); die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@; } ##### ##### Change these to match your needs ##### # 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 my $modulesList = { 'Data::Dumper' => undef, }; # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become # the full path (from which or where) my $commandsList = { 'ip' => undef, }; # list of operating systems this module can be used on. my $osList = { # 'mswin32' => undef, # 'freebsd' => undef, 'linux' => undef, }; # the category the return data should go into. See sysinfo for a list my $CATEGORY = 'network'; ##### ##### End of required ##### # some variables needed for our system my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages my @out; # temporary location for each line of output # Try to load the modules we need. If we can not, then make a list of missing modules for error message. for my $module ( keys %$modulesList ) { eval ( "use $module;" ); push @out, "$errorPrepend Could not load $module" if $@; } if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system push @out, "$errorPrepend Invalid Operating System"; } if ( !@out && ! validCommandOnSystem ( $commandsList ) ) { push @out, "$errorPrepend Can not find some commands needed"; } if ( !@out ) { # we made it, we have everything, so do the processing ##### ##### Your code starts here. Remember to push all output onto @out ##### # get list of all interfaces except lo my @interfaces = `ip -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 ( `ip address show $if` ) { # mtu if ( $line =~ m/mtu\s+(\d+)/i ) { push @out, "$CATEGORY\t$if\tmtu\t$1"; # MAC } elsif ( $line =~ m/((([a-f0-9]{2}):){5}[a-f0-9]+)/i ) { push @out, "$CATEGORY\t$if\tmac\t$1"; # inet 6 } elsif ( $line =~ m"inet6\s*((([a-f0-9]+):{1,2})+[a-z0-9]+)/(\d{1,2})"i ) { push @out, "$CATEGORY\t$if\tip6address\t$1"; push @out, "$CATEGORY\t$if\tip6networkbits\t$4"; # inet 4 } elsif ( $line =~ m"inet.*($ipv4Regex)/(\d{2})" ) { push @out, "$CATEGORY\t$if\taddress\t$1"; push @out, "$CATEGORY\t$if\tnetmask\t$2"; } } } ##### ##### Your code ends here. ##### } # If we are testing from the command line (caller is undef), print the results for debugging print join( "\n", @out ) . "\n" unless caller; # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return my $return = join( "\n", @out );