1 package Ocsinventory::Agent::Backend::OS::Linux::Network::Networks;
11 my $common = $params->{common};
12 return unless ($common->can_run("ifconfig") || $common->can_run("ip")) && $common->can_run("route") && $common->can_load("Net::IP qw(:PROC)");
24 /var/lib/NetworkManager
26 my @patterns = ("*$if*.lease", "*.lease", "dhclient.leases.$if");
29 foreach my $directory (@directories) {
30 next unless -d $directory;
31 foreach my $pattern (@patterns) {
34 glob("$directory/$pattern");
40 sort {$a->[1]->ctime()<=>$b->[1]->ctime()}
55 if( $leasepath = getLeaseFile($if) ) {
56 if (open DHCP, $leasepath) {
59 $lease = 1 if(/lease\s*{/i);
60 $lease = 0 if(/^\s*}\s*$/);
62 if ($lease) { #inside a lease section
63 if (/interface\s+"(.+?)"\s*/){
64 $dhcp = ($1 =~ /^$if$/);
67 if (/option\s+dhcp-server-identifier\s+(\d{1,3}(?:\.\d{1,3}){3})\s*;/ and $dhcp){
74 warn "Can't open $leasepath\n";
80 # Initialise the distro entry
84 my $common = $params->{common};
85 my $logger = $params->{logger};
117 if ($common->can_run("ip")){
118 my @ipLink = `ip -o link`;
119 foreach my $line (`ip -o addr`) {
120 $ipsubnet = $ipaddress = $ipmask = $ipgateway = $macaddr = $status = $type = $speed = $duplex = $driver = $pcislot = $virtualdev = $mtu = undef;
122 if ($line =~ /lo.*(127\.0\.0\.1\/8)/) { # ignore default loopback interface
125 if ($line =~ /lo.*(::1\/128)/) { # ignore default loopback interface
129 $description = $1 if ($line =~ /\d:\s+([\w\d]+)\s+/i);
131 if ($line =~ /inet (\S+)\/(\d{2})/i){
134 } elsif ($line =~ /inet6 (\S+)\/(\d{2})/i){
139 if (ip_is_ipv4($ipaddress)){
140 foreach (`route -n`) {
141 if (/^(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+)/) {
146 if (defined ($gateway{'0.0.0.0'})) {
147 $common->setHardware({
148 DEFAULTGATEWAY => $gateway{'0.0.0.0'}
153 $ipmask = ip_bintoip(ip_get_mask($mask4,4),4) if $mask4;
154 $binip = ip_iptobin($ipaddress,4) if $ipaddress;
155 $binmask = ip_iptobin($ipmask,4) if $ipmask;
156 $binsubnet = $binip & $binmask if ($binip && $binmask);
157 $ipsubnet = ip_bintoip($binsubnet,4) if $binsubnet;
158 } elsif (ip_is_ipv6($ipaddress)) {
160 foreach (`route -nA inet6`) {
161 if(/^(\S+)\/\d{1,3}\s+(\S+)/) {
167 $ipmask = ip_bintoip(ip_get_mask($mask6,6),6) if $mask6;;
168 $binip = ip_iptobin(ip_expand_address($ipaddress,6),6) if $ipaddress;
169 $binmask = ip_iptobin(ip_expand_address($ipmask,6),6) if $ipmask;
170 $binsubnet = $binip & $binmask if ($binip && $binmask);
171 $ipsubnet = ip_compress_address(ip_bintoip($binsubnet,6),6) if $binsubnet;
174 my @line1 = split " ", $line;
175 my @linkData = split " ", (grep {/$line1[1]/} @ipLink)[0];
176 $macaddr = uc $linkData[-3];
179 if ($linkData[2] =~ /,UP/) {
185 $type = $linkData[-4];
186 if ($type =~ /ether/){
188 } elsif ($type =~ /loopback/) {
192 $ipgateway = $gateway{$ipsubnet} if $ipsubnet;
193 # replace '0.0.0.0' and '::' (ie 'default gateway') by the default gateway IP address if it exists
194 if (defined($ipgateway) and $ipgateway eq '0.0.0.0' and defined($gateway{'0.0.0.0'})) {
195 $ipgateway = $gateway{'0.0.0.0'};
197 if (defined($ipgateway) and $ipgateway eq '::' and defined($gateway{'::'})) {
198 $ipgateway = $gateway{'::'};
201 if (open UEVENT, "</sys/class/net/$description/device/uevent") {
203 $driver = $1 if /^DRIVER=(\S+)/;
204 $pcislot = $1 if /^PCI_SLOT_NAME=(\S+)/;
209 # Retrieve speed from /sys/class/net/$description/speed
210 if ( ! -z "/sys/class/net/$description/speed") {
211 if(open SPEED, "</sys/class/net/$description/speed") {
218 if (defined $current_speed) {
219 chomp($current_speed);
220 if ($current_speed eq "65535"){
222 } elsif ( $current_speed gt 100 ){
223 $speed = ($current_speed/1000)." Gbps";
225 $speed = $current_speed." Mbps";
230 # Retrieve duplex from /sys/class/net/$description/duplex
231 if (open DUPLEX, "</sys/class/net/$description/duplex"){
238 # Reliable way to get the info
239 if (-d "/sys/devices/virtual/net/") {
240 $virtualdev = (-d "/sys/devices/virtual/net/$description")?"1":"0";
241 } elsif ($common->can_run("brctl")) {
244 foreach (`brctl show`) {
245 next if /^bridge name/;
246 $bridge{$1} = 1 if /^(\w+)\s/;
250 } elsif ($bridge{$description}) {
256 if (-d "/sys/class/net/$description/wireless"){
257 my @wifistatus = `iwconfig $description`;
258 foreach my $line (@wifistatus){
259 $ssid = $1 if ($line =~ /ESSID:(\S+)/);
260 $version = $1 if ($line =~ /IEEE (\S+)/);
261 $mode = $1 if ($line =~ /Mode:(\S+)/);
262 $bssid = $1 if ($line =~ /Access Point: (\S+)/);
263 $bitrate = $1 if ($line =~ /Bit\sRate=\s*(\S+\sMb\/s)/i);
267 if ($description ne "") {
268 if ($type eq "Wifi") {
269 $common->addNetwork({
270 DESCRIPTION => $description,
272 IPADDRESS => $ipaddress,
273 IPDHCP => _ipdhcp($description),
274 IPGATEWAY => $ipgateway,
276 IPSUBNET => $ipsubnet,
288 $common->addNetwork({
289 DESCRIPTION => $description,
291 IPADDRESS => $ipaddress,
292 IPDHCP => _ipdhcp($description),
293 IPGATEWAY => $ipgateway,
295 IPSUBNET => $ipsubnet,
300 VIRTUALDEV => $virtualdev,
301 DUPLEX => $duplex?"Full":"Half",
310 elsif ($common->can_run("ifconfig")){
311 foreach my $line (`ifconfig -a`) {
312 if ($line =~ /^$/ && $description && $macaddr) {
313 # end of interface section
315 if (ip_is_ipv4($ipaddress)){
316 foreach (`route -n`) {
317 if (/^(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+)/) {
322 if (defined ($gateway{'0.0.0.0'})) {
323 $common->setHardware({
324 DEFAULTGATEWAY => $gateway{'0.0.0.0'}
328 $binip = ip_iptobin ($ipaddress ,4) if $ipaddress;
329 $binmask = ip_iptobin ($ipmask,4) if $ipmask;
330 $binsubnet = $binip & $binmask if ($binip && $binmask);
331 $ipsubnet = ip_bintoip($binsubnet,4) if $binsubnet;
332 } elsif (ip_is_ipv6($ipaddress)) {
333 # Table de routage IPv6 du noyau
334 # Destination Next Hop Flag Met Ref Use If
335 # fe80::/64 [::] U 256 0 0 wlp6s0
336 # foreach (`route -6`) {
337 # if (/^(\S+\/\d{2})\s+(\S+)/) {
341 #if (defined ($gateway{'fe80::/64'})) {
342 # $common->setHardware({
343 # DEFAULTGATEWAY => $gateway{'fe80::/64'}
347 foreach (`route -nA inet6`) {
348 if(/^(\S+)\/\d{1,3}\s+(\S+)/) {
355 $ipmask = ip_bintoip(ip_get_mask($mask6,6),6) if $mask6;;
356 $binip = ip_iptobin(ip_expand_address($ipaddress,6),6) if $ipaddress;
357 $binmask = ip_iptobin(ip_expand_address($ipmask,6),6) if $ipmask;
358 $binsubnet = $binip & $binmask if ($binip && $binmask);
359 $ipsubnet = ip_compress_address(ip_bintoip($binsubnet,6),6) if $binsubnet;
362 $ipgateway = $gateway{$ipsubnet} if $ipsubnet;
364 if (-d "/sys/class/net/$description/wireless"){
365 my @wifistatus = `iwconfig $description`;
366 foreach my $line (@wifistatus){
367 $ssid = $1 if ($line =~ /ESSID:(\S+)/);
368 $version = $1 if ($line =~ /IEEE (\S+)/);
369 $mode = $1 if ($line =~ /Mode:(\S+)/);
370 $bssid = $1 if ($line =~ /Access Point: (\S+)/);
371 $bitrate = $1 if ($line =~ /Bit\sRate=\s*(\S+\sMb\/s)/i);
376 if (-f "/sys/class/net/$description/mode"){
377 $type = "infiniband";
380 # replace '0.0.0.0' and '::' (ie 'default gateway') by the default gateway IP address if it exists
381 if (defined($ipgateway) and $ipgateway eq '0.0.0.0' and defined($gateway{'0.0.0.0'})) {
382 $ipgateway = $gateway{'0.0.0.0'};
384 if (defined($ipgateway) and $ipgateway eq '::' and defined($gateway{'::'})) {
385 $ipgateway = $gateway{'::'};
388 if (open UEVENT, "</sys/class/net/$description/device/uevent") {
390 $driver = $1 if /^DRIVER=(\S+)/;
391 $pcislot = $1 if /^PCI_SLOT_NAME=(\S+)/;
396 # Retrieve speed from /sys/class/net/$description/speed
397 if ( ! -z "/sys/class/net/$description/speed") {
398 open SPEED, "</sys/class/net/$description/speed";
403 chomp($current_speed);
405 $current_speed=0 if $current_speed eq "";
406 if ($current_speed eq "65535"){
409 if ($current_speed gt 100 ){
410 $speed = ($current_speed/1000)." Gbps";
412 $speed = $current_speed." Mbps";
416 # Retrieve duplex from /sys/class/net/$description/duplex
417 if (! -z "/sys/class/net/$description/duplex") {
418 open DUPLEX, "</sys/class/net/$description/duplex";
425 # Reliable way to get the info
426 if (-d "/sys/devices/virtual/net/") {
427 $virtualdev = (-d "/sys/devices/virtual/net/$description")?"1":"0";
428 } elsif ($common->can_run("brctl")) {
431 foreach (`brctl show`) {
432 next if /^bridge name/;
433 $bridge{$1} = 1 if /^(\w+)\s/;
437 } elsif ($bridge{$description}) {
443 if ($type eq "Wifi") {
444 $common->addNetwork({
445 DESCRIPTION => $description,
447 IPADDRESS => $ipaddress,
448 IPDHCP => _ipdhcp($description),
449 IPGATEWAY => $ipgateway,
451 IPSUBNET => $ipsubnet,
463 $common->addNetwork({
464 DESCRIPTION => $description,
466 IPADDRESS => $ipaddress,
467 IPDHCP => _ipdhcp($description),
468 IPGATEWAY => $ipgateway,
470 IPSUBNET => $ipsubnet,
475 VIRTUALDEV => $virtualdev,
476 DUPLEX => $duplex?"Full":"Half",
483 if ($line =~ /^$/) { # End of section
484 $description = $driver = $ipaddress = $ipgateway = $ipmask = $ipsubnet = $macaddr = $pcislot = $status = $type = $virtualdev = $speed = $duplex = $mtu = undef;
485 } else { # In a section
486 if ($line =~ /^(\S+):\s\S+\s*mtu\s(\d+)/) {
487 $description = $1; # Interface name
491 $description = $1 if($line =~ /^(\w+)\s+/); # Interface name
493 # BUG: ipv4 address gets overwritten by ipv6 address but we want all ip addresses
494 if ($line =~ /inet add?r:(\S+)/i || $line =~ /^\s*inet\s+(\S+)/i || $line =~ /inet (\S+)\s+netmask/i){
496 } elsif ($line =~ /inet6 (\S+)\s+prefixlen\s+(\d{2})/i){
500 if ($line =~ /\S*mask:(\S+)/i || $line =~ /\S*netmask (\S+)\s/i){
503 $macaddr = $1 if ($line =~ /hwadd?r\s+(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})/i || $line =~ /ether\s+(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})/i);
504 $status = 1 if ($line =~ /^\s+UP\s/ || $line =~ /flags=.*[<,]UP[,>]/);
505 $type = $1 if ($line =~ /link encap:(\S+)/i);
506 $type = $2 if ($line =~ /^\s+(loop|ether).*\((\S+)\)/i);
507 $mtu = $1 if ($line =~ /MTU:(\d+)/i);