| 1 | # | ||||||||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | package Rex::Hardware::Network::FreeBSD; | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Rex::Helper::Array; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub get_network_devices { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_network_devices: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 0 | my @device_list = map { /^([a-z0-9]+)\:/i } i_run "ifconfig -a"; | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 0 | @device_list = array_uniq(@device_list); | |||||||||||||||||||||||||||||||||
| 21 | 0 | return \@device_list; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub get_network_configuration { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_network_configuration: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 0 | my $devices = get_network_devices(); | |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 0 | my $device_info = {}; | |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 0 | for my $dev ( @{$devices} ) { | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 0 | my $ifconfig = i_run("ifconfig $dev"); | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $device_info->{$dev} = { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 36 | ip => [ ( $ifconfig =~ m/inet (\d+\.\d+\.\d+\.\d+)/ ) ]->[0], | ||||||||||||||||||||||||||||||||||
| 37 | netmask => $ifconfig =~ m/(?:netmask 0x|netmask )([a-f0-9]+)/ | ||||||||||||||||||||||||||||||||||
| 38 | ? sprintf( "%d.%d.%d.%d", unpack "C4", pack "H*", $1 ) | ||||||||||||||||||||||||||||||||||
| 39 | : undef, | ||||||||||||||||||||||||||||||||||
| 40 | broadcast => [ ( $ifconfig =~ m/broadcast (\d+\.\d+\.\d+\.\d+)/ ) ]->[0], | ||||||||||||||||||||||||||||||||||
| 41 | mac => [ | ||||||||||||||||||||||||||||||||||
| 42 | ( $ifconfig =~ m/(ether|address:|lladdr) (..?:..?:..?:..?:..?:..?)/ ) | ||||||||||||||||||||||||||||||||||
| 43 | ]->[1], | ||||||||||||||||||||||||||||||||||
| 44 | is_bridge => 0, | ||||||||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 0 | return $device_info; | |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub route { | ||||||||||||||||||||||||||||||||
|
Subroutine
route: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 0 | my @route = i_run "netstat -nr", fail_ok => 1; | |||||||||||||||||||||||||||||||||
| 56 | 0 | my @ret; | |||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | 0 | die("Error running netstat"); | |||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 0 | my ( $in_v6, $in_v4 ); | |||||||||||||||||||||||||||||||||
| 62 | 0 | for my $route_entry (@route) { | |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( $route_entry eq "Internet:" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | 0 | $in_v4 = 1; | |||||||||||||||||||||||||||||||||
| 65 | 0 | next; | |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( $route_entry eq "Internet6:" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 69 | 0 | $in_v6 = 1; | |||||||||||||||||||||||||||||||||
| 70 | 0 | $in_v4 = 0; | |||||||||||||||||||||||||||||||||
| 71 | 0 | next; | |||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ( $route_entry =~ m/^$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 0 | $in_v6 = 0; | |||||||||||||||||||||||||||||||||
| 76 | 0 | $in_v4 = 0; | |||||||||||||||||||||||||||||||||
| 77 | 0 | next; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( $route_entry =~ m/^Destination/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 81 | 0 | next; | |||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($in_v4) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 0 | my ( $dest, $gw, $flags, $refs, $use, $netif, $expire ) = | |||||||||||||||||||||||||||||||||
| 86 | split( /\s+/, $route_entry, 7 ); | ||||||||||||||||||||||||||||||||||
| 87 | 0 | push( | |||||||||||||||||||||||||||||||||
| 88 | @ret, | ||||||||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||||||||
| 90 | destination => $dest, | ||||||||||||||||||||||||||||||||||
| 91 | gateway => $gw, | ||||||||||||||||||||||||||||||||||
| 92 | flags => $flags, | ||||||||||||||||||||||||||||||||||
| 93 | iface => $netif, | ||||||||||||||||||||||||||||||||||
| 94 | refs => $refs, | ||||||||||||||||||||||||||||||||||
| 95 | use => $use, | ||||||||||||||||||||||||||||||||||
| 96 | expire => $expire, | ||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | 0 | next; | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($in_v6) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | my ( $dest, $gw, $flags, $netif, $expire ) = | |||||||||||||||||||||||||||||||||
| 105 | split( /\s+/, $route_entry, 5 ); | ||||||||||||||||||||||||||||||||||
| 106 | 0 | push( | |||||||||||||||||||||||||||||||||
| 107 | @ret, | ||||||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||||||
| 109 | destination => $dest, | ||||||||||||||||||||||||||||||||||
| 110 | gateway => $gw, | ||||||||||||||||||||||||||||||||||
| 111 | flags => $flags, | ||||||||||||||||||||||||||||||||||
| 112 | iface => $netif, | ||||||||||||||||||||||||||||||||||
| 113 | expire => $expire, | ||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | 0 | return @ret; | |||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub default_gateway { | ||||||||||||||||||||||||||||||||
|
Subroutine
default_gateway: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 0 | my ( $class, $new_default_gw ) = @_; | |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ($new_default_gw) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( default_gateway() ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | 0 | i_run "route del default", fail_ok => 1; | |||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 0 | die("Error running route del default"); | |||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 0 | i_run "route add default $new_default_gw", fail_ok => 1; | |||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 138 | 0 | die("Error route add default"); | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||||||||
| 143 | 0 | my @route = route(); | |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | my ($default_route) = grep { | ||||||||||||||||||||||||||||||||||
| 146 | 0 | $_->{"flags"} =~ m/UG/ | |||||||||||||||||||||||||||||||||
| 147 | && ( $_->{"destination"} eq "0.0.0.0" | ||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | || $_->{"destination"} eq "default" ) | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$_->{'flags'} =~ /UG/u && ($_->{'destination'} eq '0.0.0.0' || $_->{'destination'} eq 'default')
$_->{"destination"} eq "0.0.0.0" or $_->{"destination"} eq "default"
MC/DC0%0/3
$_->{'flags'} =~ /UG/u && ($_->{'destination'} eq '0.0.0.0' || $_->{'destination'} eq 'default')
$_->{'flags'} =~ /UG/u
$_->{"destination"} eq "0.0.0.0"
$_->{"destination"} eq "default"
Truth table0%0/4
A && B or C
A= $_->{'flags'} =~ /UG/uB= $_->{"destination"} eq "0.0.0.0"C= $_->{"destination"} eq "default"
| |||||||||||||||||||||||||||||||||||
| 149 | } @route; | ||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | return $default_route->{"gateway"} if $default_route; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub netstat { | ||||||||||||||||||||||||||||||||
|
Subroutine
netstat: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | 0 | my @ret; | |||||||||||||||||||||||||||||||||
| 157 | 0 | my @netstat = i_run "netstat -na", fail_ok => 1; | |||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 160 | 0 | die("Error running netstat"); | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | 0 | shift @netstat; | |||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | 0 | my ( $in_inet, $in_unix ) = ( 0, 0 ); | |||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | 0 | for my $line (@netstat) { | |||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ( $line =~ m/^Proto\s*Recv/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 169 | 0 | $in_inet = 1; | |||||||||||||||||||||||||||||||||
| 170 | 0 | next; | |||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ( $line =~ m/^Active UNIX/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 174 | 0 | $in_inet = 0; | |||||||||||||||||||||||||||||||||
| 175 | 0 | $in_unix = 1; | |||||||||||||||||||||||||||||||||
| 176 | 0 | next; | |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( $line =~ m/^Address\s*Type/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 180 | 0 | next; | |||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ($in_inet) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 184 | 0 | my ( $proto, $recvq, $sendq, $local_addr, $foreign_addr, $state ) = | |||||||||||||||||||||||||||||||||
| 185 | split( /\s+/, $line, 6 ); | ||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ( $proto eq "tcp4" ) { $proto = "tcp"; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 187 | push( | ||||||||||||||||||||||||||||||||||
| 188 | 0 | @ret, | |||||||||||||||||||||||||||||||||
| 189 | { | ||||||||||||||||||||||||||||||||||
| 190 | proto => $proto, | ||||||||||||||||||||||||||||||||||
| 191 | recvq => $recvq, | ||||||||||||||||||||||||||||||||||
| 192 | sendq => $sendq, | ||||||||||||||||||||||||||||||||||
| 193 | local_addr => $local_addr, | ||||||||||||||||||||||||||||||||||
| 194 | foreign_addr => $foreign_addr, | ||||||||||||||||||||||||||||||||||
| 195 | state => $state, | ||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||||||||
| 198 | 0 | next; | |||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if ($in_unix) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 202 | my ( | ||||||||||||||||||||||||||||||||||
| 203 | 0 | $address, $type, $recvq, $sendq, $inode, | |||||||||||||||||||||||||||||||||
| 204 | $conn, $refs, $nextref, $addr | ||||||||||||||||||||||||||||||||||
| 205 | ) = split( /\s+/, $line, 9 ); | ||||||||||||||||||||||||||||||||||
| 206 | 0 | push( | |||||||||||||||||||||||||||||||||
| 207 | @ret, | ||||||||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||||||||
| 209 | proto => "unix", | ||||||||||||||||||||||||||||||||||
| 210 | address => $address, | ||||||||||||||||||||||||||||||||||
| 211 | refcnt => $refs, | ||||||||||||||||||||||||||||||||||
| 212 | type => $type, | ||||||||||||||||||||||||||||||||||
| 213 | inode => $inode, | ||||||||||||||||||||||||||||||||||
| 214 | path => $addr, | ||||||||||||||||||||||||||||||||||
| 215 | recvq => $recvq, | ||||||||||||||||||||||||||||||||||
| 216 | sendq => $sendq, | ||||||||||||||||||||||||||||||||||
| 217 | conn => $conn, | ||||||||||||||||||||||||||||||||||
| 218 | nextref => $nextref, | ||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||
| 222 | 0 | next; | |||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | 0 | return @ret; | |||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | 1 | 1; | |||||||||||||||||||||||||||||||||