| 1 | package Parse::CPinfo; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.006_001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw/Exporter/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.882'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # extracted from Regexp::Common | ||||||||||||||||||||||||||||
| 14 | 3 | my $re_mac = | |||||||||||||||||||||||||||
| 15 | '(?:(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}))'; | ||||||||||||||||||||||||||||
| 16 | 3 | my $re_net_ipv4 = | |||||||||||||||||||||||||||
| 17 | '(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))'; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 21 | 2 | my $self = {}; | |||||||||||||||||||||||||||
| 22 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 23 | 2 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub readfile { | ||||||||||||||||||||||||||
|
Subroutine
readfile: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 2 | my $filename = shift; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # keep copy of filename in object | ||||||||||||||||||||||||||||
| 31 | 2 | $self->{'_filename'} = $filename; | |||||||||||||||||||||||||||
| 32 | 2 | my $fh = new IO::File($filename, 'r'); | |||||||||||||||||||||||||||
| 33 | 2 | ▶ | if (!defined($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | croak "Unable to open $filename for reading"; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # ensure we are in binary mode as some system (win32 for example) | ||||||||||||||||||||||||||||
| 38 | # will assume we are handling text otherwise. | ||||||||||||||||||||||||||||
| 39 | 1 | binmode $fh, ':raw'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | my @lines = <$fh>; | |||||||||||||||||||||||||||
| 42 | 1 | chomp @lines; | |||||||||||||||||||||||||||
| 43 | 1 | my $linenumber = 0; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | while ($linenumber < $#lines) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 459 | $linenumber++; | |||||||||||||||||||||||||||
| 46 | 459 | ▶ | if ($lines[$linenumber] =~ m/^\={46}$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 18 | $linenumber++; | |||||||||||||||||||||||||||
| 48 | 18 | my $section = $lines[$linenumber]; | |||||||||||||||||||||||||||
| 49 | 18 | $linenumber++; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 18 | foreach (0 .. $linenumber) { | |||||||||||||||||||||||||||
| 52 | 1801 | $linenumber++; | |||||||||||||||||||||||||||
| 53 | 1801 | ▶ | if ($lines[$linenumber] !~ m/^\={46}$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1786 | $lines[$linenumber] =~ s/\r\n//g; | |||||||||||||||||||||||||||
| 55 | 1786 | $lines[$linenumber] =~ s/\r//g; | |||||||||||||||||||||||||||
| 56 | 1786 | $lines[$linenumber] =~ s/\n//g; | |||||||||||||||||||||||||||
| 57 | 1786 | $self->{'config'}->{$section} = $self->{'config'}->{$section} . "$lines[$linenumber]\n"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 15 | $linenumber--; | |||||||||||||||||||||||||||
| 61 | 15 | last; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 1 | $self->_parseinterfacelist(); | |||||||||||||||||||||||||||
| 67 | 1 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub _parseinterfacelist { | ||||||||||||||||||||||||||
|
Subroutine
_parseinterfacelist: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 71 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 1 | my $ifconfigtext = $self->{'config'}->{'IP Interfaces'}; | |||||||||||||||||||||||||||
| 73 | 1 | ▶ | if (!$ifconfigtext) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 1 | my @s = split /\n/, $ifconfigtext; | |||||||||||||||||||||||||||
| 77 | 1 | my ($int); | |||||||||||||||||||||||||||
| 78 | 1 | foreach my $line (@s) { | |||||||||||||||||||||||||||
| 79 | 20 | chomp $line; | |||||||||||||||||||||||||||
| 80 | 20 | ▶ | if ($line =~ m/^(\w+)\s+/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 3 | my $match = $1; | |||||||||||||||||||||||||||
| 82 | 3 | ▶ | if ($match !~ m/ifconfig/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | $int = $1; | |||||||||||||||||||||||||||
| 84 | 2 | $self->{'interface'}->{$int}->{'name'} = $int; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 20 | ▶ | if ($line =~ m/Link encap:(\w+)\s+/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 2 | $self->{'interface'}->{$int}->{'encap'} = $1; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 20 | ▶ | if ($line =~ m/HWaddr ($re_mac)/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | $self->{'interface'}->{$int}->{'hwaddr'} = $1; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 20 | ▶ | if ($line =~ m/inet addr:($re_net_ipv4)/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | $self->{'interface'}->{$int}->{'inetaddr'} = $1; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 20 | ▶ | if ($line =~ m/bcast:($re_net_ipv4)/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | $self->{'interface'}->{$int}->{'broadcast'} = $1; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 20 | ▶ | if ($line =~ m/mask:($re_net_ipv4)/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 2 | $self->{'interface'}->{$int}->{'mask'} = $1; | |||||||||||||||||||||||||||
| 101 | 2 | $self->{'interface'}->{$int}->{'masklength'} = $self->_ipv4_msk2cidr($self->{'interface'}->{$int}->{'mask'}); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 20 | ▶ | if ($line =~ m/MTU:(\d+)/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 2 | $self->{'interface'}->{$int}->{'mtu'} = $1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 1 | return 1; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | sub _ipv4_msk2cidr { | ||||||||||||||||||||||||||
|
Subroutine
_ipv4_msk2cidr: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||
| 111 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 2 | my $mask = shift; | |||||||||||||||||||||||||||
| 113 | 2 | ($mask) = $mask =~ m/(\d+\.\d+\.\d+\.\d+)/o; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | if (! defined($mask)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | return undef; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | for (split /\./, $mask) { | |||||||||||||||||||||||||||
| 120 | 8 | ▶ | if ($_ < 0 or $_ > 255) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ < 0 or $_ > 255
MC/DC0%0/2
$_ < 0 or $_ > 255
$_ < 0
$_ > 255
Truth table33%1/3
A or B
A= $_ < 0B= $_ > 255
| |||||||||||||||||||||||||||||
| 121 | 0 | return undef; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 2 | my @bytes = split /\./, $mask; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | my $cidr = 0; | |||||||||||||||||||||||||||
| 127 | 2 | for (@bytes) { | |||||||||||||||||||||||||||
| 128 | 8 | my $bits = unpack("B*", pack("C", $_)); | |||||||||||||||||||||||||||
| 129 | 8 | $cidr += $bits =~ tr /1/1/; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 2 | return $cidr; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub getInterfaceList { | ||||||||||||||||||||||||||
|
Subroutine
getInterfaceList: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 1 | return keys %{$self->{'interface'}}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | ▶ | sub getInterfaceInfo { | ||||||||||||||||||||||||||
|
Subroutine
getInterfaceInfo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 2 | my $interface = shift; | |||||||||||||||||||||||||||
| 142 | 2 | return $self->{'interface'}{$interface}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub getSectionList { | ||||||||||||||||||||||||||
|
Subroutine
getSectionList: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 147 | 0 | my @r; | |||||||||||||||||||||||||||
| 148 | 0 | foreach my $section (sort keys %{$self->{config}}) { | |||||||||||||||||||||||||||
| 149 | 0 | push @r, $section; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | return @r; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | sub getSection { | ||||||||||||||||||||||||||
|
Subroutine
getSection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | 1 | my $query = shift; | |||||||||||||||||||||||||||
| 157 | 1 | my $section = $self->{'config'}->{$query}; | |||||||||||||||||||||||||||
| 158 | 1 | return $section; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | ▶ | sub getHostname { | ||||||||||||||||||||||||||
|
Subroutine
getHostname: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 1 | my @section = split(/\n/, $self->getSection('System Information')); | |||||||||||||||||||||||||||
| 164 | 1 | my $hostname; | |||||||||||||||||||||||||||
| 165 | 1 | foreach my $linenumber (0 .. $#section) { | |||||||||||||||||||||||||||
| 166 | 14 | ▶ | if ($section[$linenumber] =~ m/Issuing 'hostname'/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 1 | $hostname = $section[$linenumber + 2]; | |||||||||||||||||||||||||||
| 168 | 1 | chomp $hostname; | |||||||||||||||||||||||||||
| 169 | 1 | last; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 1 | ▶ | if (defined($hostname)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 1 | return $hostname; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||
| 176 | 0 | return undef; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 3 | 1; | |||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||