Sys/Net.pm

filter: Statementstmt 50.0% 17 / 34 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 38.9% 23 / 59 CC 9 SCAR 37.4
CC
9
Cov
35%
interfaces
72.0
resolv
6.0
BEGIN
1.0
CRAP 42.3
↑ Summary Test/Server/Util.pm »
1 package Sys::Net;
2  
3 =head1 NAME
4  
5 Sys::Net - system network information or actions
6  
7 =head1 SYNOPSIS
8  
9 my %if_named = %{Sys::Net::interfaces()};
10 foreach my $if_name (keys %if_named) {
11 print 'interface with name: ', $if_name, ' has ip: ', $if_named{$if_name}, "\n";
12 }
13  
14 use Sys::Net 'resolv';
15 my $hostname = resolv('127.0.0.1');
16 my $ip = resolv('localhost');
17  
18  
19 =head1 DESCRIPTION
20  
21 The purpouse is to find out network information or perform system network
22 actions.
23  
24 System network interfaces for the moment works just for Linux, and gets only
25 ipv4 ip of the system interfaces. Will be extended when a need arrise.
26  
27 =cut
28  
29 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 our $VERSION = '0.06';
33  
34 1 ▶use Socket 'inet_ntoa', 'inet_aton', 'AF_INET';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 our $IFCONFIG_CMD = '/sbin/ifconfig';
39 1 our $if_named;
40 1 our @EXPORT_OK = qw(resolv interfaces);
41  
42 =head1 METHODS
43  
44 =head2 EXPORTS
45  
46 our @EXPORT_OK = qw(resolv interfaces);
47  
48 =head2 interfaces()
49  
50 returns hash ref with:
51  
52 {
53 'lo' => { 'ip' => '127.0.0.1' },
54 'eth0' => { 'ip' => '192.168.100.6' },
55 };
56  
57 TODO more information than just an ip.
58  
59 =cut
60  
61 0 ▶sub interfaces {
Subroutine interfaces: not called CC=8 SCAR=42.8
Pod documented
62 0 ▶ return $if_named
Branch: 1 branch
BranchTrueFalseTotal
if defined $Sys::Net::if_named 0 0 0
63 if defined $if_named;
64  
65 0 $if_named = {};
66  
67 # TODO use File::Which to look if there is ifconfig (also any more folders then just one)
68 0 my @ifconfig_out = `$IFCONFIG_CMD`;
69
70 0 my $ifname;
71 my $ifip;
72 0 foreach my $line (@ifconfig_out) {
73 # empty line resets the values
74 0 ▶ if ($line =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/) 0 0 0
75 0 $ifname = undef;
76 0 $ifip = undef;
77 }
78  
79 # get columns 1, 2, 3
80 0 my ($c1,$c2,$c3) = split /\s+/, $line;
81  
82 # get interface name
83 0 ▶ $ifname = $c1
Branch: 1 branch
BranchTrueFalseTotal
if $c2 eq "Link" 0 0 0
84 if $c2 eq 'Link';
85 # get ip address
86 0 ▶ ($ifip) = $c3 =~ m/addr:(.+)/
Branch: 1 branch
BranchTrueFalseTotal
if $c2 eq "inet" 0 0 0
87 if $c2 eq 'inet';
88
89 # if we have both ip and interface name store it
90 0 ▶ $if_named->{$ifname} = { 'ip' => $ifip }
Branch: 1 branch
BranchTrueFalseTotal
if $ifname and $ifip 0 0 0
Condition0%0/3
$ifname and $ifip
!ll&&!rl&&r
000
MC/DC0%0/2
$ifname and $ifip
$ifname $ifip
Truth table0%0/3
A and B
A= $ifnameB= $ifip
ABresult
0X0
100
111
91 if ($ifname and $ifip);
92 }
93
94 0 return $if_named;
95 }
96  
97  
98 =head2 resolv()
99  
100 Resolv hostname to an ip or ip to an hostname.
101  
102 Using gethostbyaddr or gethostbyname so also hosts in /etc/hosts
103 are taken into an account.
104  
105 =cut
106  
107 0 ▶sub resolv {
Subroutine resolv: not called CC=2 SCAR=17.9
Pod documented
108 0 my $name = shift;
109
110 # resolv ip to a hostname
111 0 ▶ if ($name =~ m/^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\s*$/) { } 0 0 0
112 0 return scalar gethostbyaddr(inet_aton($name), AF_INET);
113 }
114 # resolv hostname to ip
115 else {
116 0 return inet_ntoa(scalar gethostbyname($name));
117 }
118 }
119  
120  
121 1 1;
122  
123  
124 __END__
↑ Summary Test/Server/Util.pm »