#!/usr/bin/env perl use warnings; use strict; use version ; our $VERSION = '1.2.0'; # Microsoft network module for sysinfo client # Author: R. W. Rodolico # Date: 2025-03-30 # # module to get network interface information for Windows systems # uses Win32::Net::Info # install with cpan install Win32::Net::Info # find our location and use it for searching for libraries 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, 'Win32::Net::Info' => 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 = { # 'smartctl' => 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 ##### foreach my $adapter ( Win32::Net::Info->interfaces) { my $interface = Win32::Net::Info->new( $adapter ); my $name = $interface->name(); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'description', $interface->description() ); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'mac', $interface->mac() ); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'address', $interface->ipv4() ); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'netmask', $interface->ipv4_netmask() ); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'gateway', $interface->ipv4_default_gateway() ) if $interface->ipv4_default_gateway(); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'mtu', $interface->mtu() ); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'ip6address', $interface->ipv6() ) if $interface->ipv6(); push @out, printf ( "%s\t$name\t%s\t%s", $CATEGORY, 'ip6networkbits', $interface->ipv6_netmask() )if $interface->ipv6_netmask(); } ##### ##### 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 );