/
Networks.pm
120 lines (95 loc) · 3.21 KB
/
Networks.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package FusionInventory::Agent::Task::Inventory::AIX::Networks;
use strict;
use warnings;
use parent 'FusionInventory::Agent::Task::Inventory::Module';
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Unix;
use FusionInventory::Agent::Tools::Network;
sub isEnabled {
my (%params) = @_;
return 0 if $params{no_category}->{network};
return canRun('lscfg');
}
sub doInventory {
my (%params) = @_;
my $inventory = $params{inventory};
my $logger = $params{logger};
my $routes = getRoutingTable(command => 'netstat -nr', logger => $logger);
my $default = $routes->{'0.0.0.0'};
my @interfaces = _getInterfaces(logger => $logger);
foreach my $interface (@interfaces) {
# if the default gateway address and the interface address belongs to
# the same network, that's the gateway for this network
$interface->{IPGATEWAY} = $default if isSameNetwork(
$default, $interface->{IPADDRESS}, $interface->{IPMASK}
);
$inventory->addEntry(
section => 'NETWORKS',
entry => $interface
);
}
$inventory->setHardware({
DEFAULTGATEWAY => $default
});
}
sub _getInterfaces {
my (%params) = @_;
my $logger = $params{logger};
# get a list of interfaces from ifconfig
my @interfaces =
map { { DESCRIPTION => $_ } }
split(/ /, getFirstLine(command => 'ifconfig -l'));
# complete with hardware addresses, extracted from lscfg
my %addresses = _parseLscfg(
command => 'lscfg -v -l ent*',
logger => $logger
);
foreach my $interface (@interfaces) {
next unless $addresses{$interface->{DESCRIPTION}};
$interface->{TYPE} = 'ethernet';
$interface->{MACADDR} = $addresses{$interface->{DESCRIPTION}};
}
# complete with network information, extracted from lsattr
foreach my $interface (@interfaces) {
my $handle = getFileHandle(
command => "lsattr -E -l $interface->{DESCRIPTION}",
logger => $logger
);
next unless $handle;
while (my $line = <$handle>) {
$interface->{IPADDRESS} = $1
if $line =~ /^netaddr \s+ ($ip_address_pattern)/x;
$interface->{IPMASK} = $1
if $line =~ /^netmask \s+ ($ip_address_pattern)/x;
$interface->{STATUS} = $1
if $line =~ /^state \s+ (\w+)/x;
}
close $handle;
}
foreach my $interface (@interfaces) {
$interface->{IPSUBNET} = getSubnetAddress(
$interface->{IPADDRESS},
$interface->{IPMASK},
);
$interface->{STATUS} = "Down" unless $interface->{IPADDRESS};
$interface->{IPDHCP} = "No";
}
return @interfaces;
}
sub _parseLscfg {
my $handle = getFileHandle(@_);
return unless $handle;
my %addresses;
my $current_interface;
while (my $line = <$handle>) {
if ($line =~ /^\s+ ent(\d+) \s+ \S+ \s+/x) {
$current_interface = "en$1";
}
if ($line =~ /Network Address\.+($alt_mac_address_pattern)/) {
$addresses{$current_interface} = alt2canonical($1);
}
}
close $handle;
return %addresses;
}
1;