line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# network::device Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Network::Device; |
7
|
2
|
|
|
2
|
|
792
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
59
|
|
8
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
10
|
use base qw(Metabrik::System::Package); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
2924
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable interface) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
device => [ qw(device) ], |
20
|
|
|
|
|
|
|
enable_warnings => [ qw(0|1) ], |
21
|
|
|
|
|
|
|
}, |
22
|
|
|
|
|
|
|
attributes_default => { |
23
|
|
|
|
|
|
|
enable_warnings => 0, |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
commands => { |
26
|
|
|
|
|
|
|
install => [ ], # Inherited |
27
|
|
|
|
|
|
|
list => [ ], |
28
|
|
|
|
|
|
|
get => [ qw(device|OPTIONAL) ], |
29
|
|
|
|
|
|
|
default => [ qw(destination_ip|OPTIONAL) ], |
30
|
|
|
|
|
|
|
show => [ qw(device_array|OPTIONAL) ], |
31
|
|
|
|
|
|
|
internet_address => [ ], |
32
|
|
|
|
|
|
|
my_ipv4 => [ qw(device|OPTIONAL) ], |
33
|
|
|
|
|
|
|
my_ipv6 => [ qw(device|OPTIONAL) ], |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
require_modules => { |
36
|
|
|
|
|
|
|
'Net::Libdnet::Intf' => [ ], |
37
|
|
|
|
|
|
|
'Net::Routing' => [ ], |
38
|
|
|
|
|
|
|
'Net::IPv4Addr' => [ ], |
39
|
|
|
|
|
|
|
'Metabrik::Client::Www' => [ ], |
40
|
|
|
|
|
|
|
}, |
41
|
|
|
|
|
|
|
need_packages => { |
42
|
|
|
|
|
|
|
ubuntu => [ qw(libpcap-dev libnet-libdnet-perl libnet-pcap-perl) ], |
43
|
|
|
|
|
|
|
debian => [ qw(libpcap-dev libnet-libdnet-perl libnet-pcap-perl) ], |
44
|
|
|
|
|
|
|
kali => [ qw(libpcap-dev libnet-libdnet-perl libnet-pcap-perl) ], |
45
|
|
|
|
|
|
|
freebsd => [ qw(p5-Net-Pcap libdnet) ], |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub brik_use_properties { |
51
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return { |
54
|
0
|
|
0
|
|
|
|
attributes_default => { |
55
|
|
|
|
|
|
|
device => defined($self->global) && $self->global->device || 'eth0', |
56
|
|
|
|
|
|
|
}, |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub list { |
61
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $dev = {}; |
64
|
0
|
|
|
|
|
|
my $err = ''; |
65
|
0
|
|
|
|
|
|
my @devs = Net::Pcap::findalldevs($dev, \$err); |
66
|
0
|
0
|
|
|
|
|
if (length($err)) { |
|
|
0
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return $self->log->error("list: findalldevs failed with error [$err]"); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
elsif (@devs == 0) { |
70
|
0
|
|
|
|
|
|
return $self->log->error("list: findalldevs found no device"); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
return \@devs; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub get { |
77
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
78
|
0
|
|
|
|
|
|
my ($device) = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
0
|
|
|
|
$device ||= $self->device; |
81
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('get', $device) or return; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my $intf = Net::Libdnet::Intf->new; |
84
|
0
|
0
|
|
|
|
|
if (! defined($intf)) { |
85
|
0
|
0
|
|
|
|
|
$self->enable_warnings |
86
|
|
|
|
|
|
|
&& $self->log->warning("get: Net::Libdnet::Intf new failed for device [$device]"); |
87
|
0
|
|
|
|
|
|
return {}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
my $get = $intf->get($device); |
91
|
0
|
0
|
|
|
|
|
if (! defined($get)) { |
92
|
0
|
0
|
|
|
|
|
$self->enable_warnings |
93
|
|
|
|
|
|
|
&& $self->log->error("get: Net::Libdnet::Intf get failed for device [$device]"); |
94
|
0
|
|
|
|
|
|
return {}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Populate HASH from Net::Libdnet::Entry::Intf object |
98
|
0
|
|
|
|
|
|
my $dev = { |
99
|
|
|
|
|
|
|
device => $device, |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if (my $ip = $get->ip) { |
103
|
0
|
|
|
|
|
|
$dev->{ipv4} = $ip; |
104
|
|
|
|
|
|
|
} |
105
|
0
|
0
|
|
|
|
|
if (my $broadcast = $get->broadcast) { |
106
|
0
|
|
|
|
|
|
$dev->{broadcast} = $get->broadcast; |
107
|
|
|
|
|
|
|
} |
108
|
0
|
0
|
|
|
|
|
if (my $netmask = $get->cidr2mask) { |
109
|
0
|
|
|
|
|
|
$dev->{netmask} = $get->cidr2mask; |
110
|
|
|
|
|
|
|
} |
111
|
0
|
0
|
|
|
|
|
if (my $cidr = $get->cidr) { |
112
|
0
|
|
|
|
|
|
$dev->{cidr} = $cidr; |
113
|
|
|
|
|
|
|
} |
114
|
0
|
0
|
|
|
|
|
if (my $mac = $get->linkAddr) { |
115
|
0
|
|
|
|
|
|
$dev->{mac} = $mac; |
116
|
|
|
|
|
|
|
} |
117
|
0
|
|
|
|
|
|
my $cidr; |
118
|
|
|
|
|
|
|
my $subnet; |
119
|
0
|
0
|
0
|
|
|
|
if ($subnet = $get->subnet and $cidr = $get->cidr) { |
120
|
0
|
|
|
|
|
|
$dev->{subnet4} = "$subnet/$cidr"; |
121
|
|
|
|
|
|
|
} |
122
|
0
|
|
|
|
|
|
my @aliases = $get->aliasAddrs; |
123
|
0
|
0
|
|
|
|
|
if (@aliases > 0) { |
124
|
|
|
|
|
|
|
# IPv6 are within aliases. First one if the main IPv6 address. |
125
|
0
|
0
|
|
|
|
|
if (defined($aliases[0])) { |
126
|
0
|
|
|
|
|
|
my $subnet6 = $aliases[0]; |
127
|
0
|
|
|
|
|
|
(my $ipv6 = $subnet6) =~ s/\/\d+$//; |
128
|
0
|
|
|
|
|
|
$dev->{ipv6} = $ipv6; |
129
|
0
|
|
|
|
|
|
$dev->{subnet6} = $subnet6; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return $dev; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub default { |
137
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
138
|
0
|
|
|
|
|
|
my ($destination) = @_; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Default route to Internet using Google DNS nameserver |
141
|
0
|
|
0
|
|
|
|
$destination ||= '8.8.8.8'; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
my $family = Net::Routing::NR_FAMILY_INET4(); |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my $nr = Net::Routing->new( |
146
|
|
|
|
|
|
|
target => $destination, |
147
|
|
|
|
|
|
|
family => $family, |
148
|
|
|
|
|
|
|
); |
149
|
0
|
0
|
|
|
|
|
if (! defined($nr)) { |
150
|
0
|
|
|
|
|
|
return $self->log->error("default: new failed: $Net::Routing::Error"); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
0
|
0
|
|
|
|
|
my $list = $nr->get |
154
|
|
|
|
|
|
|
or return $self->log->error("default: get failed: $Net::Routing::Error"); |
155
|
|
|
|
|
|
|
# Only one possibility, that's great |
156
|
0
|
0
|
|
|
|
|
if (@$list == 1) { |
157
|
0
|
|
|
|
|
|
return $list->[0]->{interface}; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
# Or we return every possible interface |
160
|
|
|
|
|
|
|
else { |
161
|
0
|
|
|
|
|
|
my %interfaces = (); |
162
|
0
|
|
|
|
|
|
for my $i (@$list) { |
163
|
0
|
|
|
|
|
|
$interfaces{$i->{interface}}++; |
164
|
|
|
|
|
|
|
} |
165
|
0
|
|
|
|
|
|
return [ keys %interfaces ]; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Error |
169
|
0
|
|
|
|
|
|
return; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub show { |
173
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
174
|
0
|
|
|
|
|
|
my ($devices) = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
0
|
0
|
|
|
|
$devices ||= $self->list or return; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
for my $this (@$devices) { |
179
|
0
|
|
|
|
|
|
$self->log->debug("show: found device [$this]"); |
180
|
0
|
|
|
|
|
|
my $device = $self->get($this); |
181
|
0
|
0
|
0
|
|
|
|
if (! defined($device) || ! exists($device->{device})) { |
182
|
0
|
0
|
|
|
|
|
$self->enable_warnings |
183
|
|
|
|
|
|
|
&& $self->log->warning("show: get failed for device [$this]"); |
184
|
0
|
|
|
|
|
|
next; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
printf("device: %s\nipv4: %s subnet4: %s\nipv6: %s subnet6: %s\n\n", |
188
|
|
|
|
|
|
|
$device->{device} || 'undef', |
189
|
|
|
|
|
|
|
$device->{ipv4} || 'undef', |
190
|
|
|
|
|
|
|
$device->{subnet4} || 'undef', |
191
|
|
|
|
|
|
|
$device->{ipv6} || 'undef', |
192
|
0
|
|
0
|
|
|
|
$device->{subnet6} || 'undef' |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
193
|
|
|
|
|
|
|
); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
return 1; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub internet_address { |
200
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
201
|
|
|
|
|
|
|
|
202
|
0
|
0
|
|
|
|
|
my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
#my $url = 'http://ip.nu'; |
205
|
0
|
|
|
|
|
|
my $url = 'http://www.whatsmyip.net/'; |
206
|
0
|
0
|
|
|
|
|
my $get = $cw->get($url) or return; |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
my $html = $get->{content}; |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
my ($ip) = $html =~ /(\d+\.\d+\.\d+\.\d+)/; |
211
|
|
|
|
|
|
|
|
212
|
0
|
|
0
|
|
|
|
return $ip || 'undef'; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub my_ipv4 { |
216
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
217
|
0
|
|
|
|
|
|
my ($device) = @_; |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
0
|
|
|
|
$device ||= $self->device; |
220
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('my_ipv4', $device) or return; |
221
|
|
|
|
|
|
|
|
222
|
0
|
0
|
|
|
|
|
my $get = $self->get($device) or return; |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
my $ip = $get->{ipv4}; |
225
|
0
|
0
|
|
|
|
|
if (! defined($ip)) { |
226
|
0
|
|
|
|
|
|
return $self->log->error("my_ipv4: IPv4 address not found for device [$device]"); |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
0
|
|
|
|
|
|
return $ip; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub my_ipv6 { |
233
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
234
|
0
|
|
|
|
|
|
my ($device) = @_; |
235
|
|
|
|
|
|
|
|
236
|
0
|
|
0
|
|
|
|
$device ||= $self->device; |
237
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('my_ipv6', $device) or return; |
238
|
|
|
|
|
|
|
|
239
|
0
|
0
|
|
|
|
|
my $get = $self->get($device) or return; |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
|
my $ip = $get->{ipv6}; |
242
|
0
|
0
|
|
|
|
|
if (! defined($ip)) { |
243
|
0
|
|
|
|
|
|
return $self->log->error("my_ipv6: IPv6 address not found for device [$device]"); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
return $ip; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
1; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__ |