#!/usr/bin/env perl use warnings; use strict; use version ; our $VERSION = 'v1.2.0'; # BSD network module for sysinfo client # Author: R. W. Rodolico # Date: 2016-04-08 # # module to get network interface information for BSD 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 # # # 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 = { 'ifconfig' => 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 ##### # xn0: flags=8943 metric 0 mtu 1500 # ether 00:16:3e:ff:ff:04 my $regexHWADDR = 'ether\s+([0-9a-f:]+)'; # inet 74.113.60.189 netmask 0xffffffc0 broadcast 74.113.60.191 my $regexINET = 'inet\s*([0-9.]+)[^0-9].*netmask 0x([0-9a-z]+)'; # inet6 addr: fe80::216:3eff:fe1f:ef4f/64 Scope:Link my $regexINET6 = 'inet6\s*([0-9a-f:]+).*prefixlen\s*(\d+)'; # UP LOOPBACK RUNNING MTU:16436 Metric:1 my $regexMTU = 'mtu\s([0-9]+)'; my $temp = qx( ifconfig ); my @temp = split( "\n", $temp ); # die Dumper( \@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/$regexMTU/i ) { push @out, "$CATEGORY\t$currentIF\tmtu\t$1"; } } elsif ( $line =~ m/$regexHWADDR/i ) { push @out, "$CATEGORY\t$currentIF\tmac\t$1"; } elsif ( $line =~ m/$regexINET/i ) { push @out, "$CATEGORY\t$currentIF\taddress\t$1"; push @out, "$CATEGORY\t$currentIF\tnetmask\t$2"; } elsif ( $line =~ m/$regexINET6/i ) { push @out, "$CATEGORY\t$currentIF\tip6address\t$1"; push @out, "$CATEGORY\t$currentIF\tip6networkbits\t$2"; } elsif ( $line =~ m/$regexMTU/i ) { push @out, "$CATEGORY\t$currentIF\tmtu\t$1"; } } ##### ##### 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 );