#!/usr/bin/env perl # Copyright (c) 2025, R. W. Rodolico # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # freebsd_network # FreeBSD network interfaces and addresses from ifconfig. # # Revision History: # 2026-10-18 Claude v1.0.0 new sensor, split from unix_network (bug 210) package freebsd_network; use strict; use warnings; use version; our $VERSION = version->declare('v1.0.0'); use FindBin; use File::Spec; use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); use library qw(:all); my $CATEGORY = 'network'; ####################################################### # # parseBsdNetwork( $ifconfigText ) # # Parses BSD ifconfig output into old network key paths. # # Parameters: # $ifconfigText - text from ifconfig -a # # Returns: # array reference of output lines # # Side Effects: # none # ####################################################### sub parseBsdNetwork { my $ifconfigText = shift; my @out; my $current = ''; # same expressions and else-if order as v3.7.1 bsd_networking; netmask has no 0x my $regexHWADDR = 'ether\s+([0-9a-f:]+)'; my $regexINET = 'inet\s*([0-9.]+)[^0-9].*netmask 0x([0-9a-z]+)'; my $regexINET6 = 'inet6\s*([0-9a-f:]+).*prefixlen\s*(\d+)'; my $regexMTU = 'mtu\s([0-9]+)'; foreach my $line (split(/\n/, defined($ifconfigText) ? $ifconfigText : '')) { next unless $line; if ($line =~ /^([^ ]+):/) { # first character is not a space, so a new interface $current = $1; push @out, "$CATEGORY\t$current\tmtu\t$1" if $line =~ /$regexMTU/i; } elsif ($line =~ /$regexHWADDR/i) { push @out, "$CATEGORY\t$current\tmac\t$1"; } elsif ($line =~ /$regexINET/i) { push @out, "$CATEGORY\t$current\taddress\t$1"; push @out, "$CATEGORY\t$current\tnetmask\t$2"; } elsif ($line =~ /$regexINET6/i) { push @out, "$CATEGORY\t$current\tip6address\t$1"; push @out, "$CATEGORY\t$current\tip6networkbits\t$2"; } elsif ($line =~ /$regexMTU/i) { push @out, "$CATEGORY\t$current\tmtu\t$1"; } } return \@out; } ####################################################### # # main() # # Emits network keys. # # Parameters: # none # # Returns: # integer exit status (1 wrong OS, 2 not applicable) # # Side Effects: # runs ifconfig, writes STDOUT/STDERR # ####################################################### sub main { if (!checkOS({ 'freebsd' => undef })) { print STDERR "unsupported os\n"; return 1; } unless (validCommandOnSystem({ 'ifconfig' => undef })) { print STDERR "ifconfig not found\n"; return 2; } my @out; push @out, @{parseBsdNetwork(runCommand('ifconfig', '-a'))}; print join("\n", @out), "\n" if @out; return 0; } exit main() unless caller; 1;