From a4fba2e30594b74c77b607dc6131db9fe5bb26ec Mon Sep 17 00:00:00 2001 From: Papoteur Date: Sat, 6 Feb 2021 19:05:02 +0100 Subject: Get the interface data from ip command --- common/IFCFG.pm | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/common/IFCFG.pm b/common/IFCFG.pm index ca9cb71e..3c4b76ea 100644 --- a/common/IFCFG.pm +++ b/common/IFCFG.pm @@ -26,18 +26,50 @@ use lib qw(/usr/lib/libDrakX); use Data::Dumper; use MDK::Common; use network::network; +use detect_devices; #my $file = "/etc/sysconfig/network-scripts/drakconnect_conf"; #!-f $file and die "no such $file"; sub new { + my @network_masks = ( + "128.0.0.0", + "192.0.0.0", + "224.0.0.0", + "240.0.0.0", + "248.0.0.0", + "252.0.0.0", + "254.0.0.0", + "255.0.0.0", + "255.128.0.0", + "255.192.0.0", + "255.224.0.0", + "255.240.0.0", + "255.248.0.0", + "255.252.0.0", + "255.254.0.0", + "255.255.0.0", + "255.255.128.0", + "255.255.192.0", + "255.255.224.0", + "255.255.240.0", + "255.255.248.0", + "255.255.252.0", + "255.225.254.0", + "255.255.255.0", + "255.255.255.128", + "255.255.255.192", + "255.255.255.224", + "255.255.255.240", + "255.255.255.248", + "255.255.255.252", + "255.255.255.254", + "255.255.255.255"); my $self = {}; - - my $ifconfig = `LC_ALL=C /sbin/ifconfig -a`; - my $device = 'NONE'; - foreach (split('\n', $ifconfig)) { - my ($temp) = /(^eth[0-9]*:?[0-9]*)/; - $device = $temp if defined $temp; - my ($ip, $bcast, $netmask) = /\s*inet addr:([0-9\.]*)\s*Bcast:([0-9\.]*)\s*Mask:([0-9\.]*)/; + my @devices = detect_devices::get_all_net_devices(); + foreach my $device (@devices) { + if( detect_devices::is_lan_interface($device)) { + $_ = `/sbin/ip -4 address show dev $device |grep inet`; + my ($ip, $netmask, $bcast,) = /inet\s([0-9\.]*)\/([0-9]*)\sbrd\s([0-9\.]*)/; if (defined $ip && defined $bcast && defined $netmask) { $self->{itf}{$device} = { IPADDR => $ip, BROADCAST => $bcast, NETMASK => $netmask }; my %conf = getVarsFromSh("/etc/sysconfig/network-scripts/ifcfg-$device"); @@ -52,6 +84,7 @@ sub new { $self->{network}{HOSTNAME} ||= `/bin/hostname` and chomp $self->{network}{HOSTNAME}; ($self->{network}{DOMAINNAME}) = $self->{network}{HOSTNAME} =~ /\.(.*)/; $self->{network}{DOMAINNAME} ||= `/bin/dnsdomainname` and chomp $self->{network}{DOMAINNAME}; + } bless $self; } @@ -62,7 +95,6 @@ sub is_dhcp { $self->{itf}{$o_itf}{BOOTPROTO} eq 'dhcp'; } -#- TODO : return the main interface sub default_itf { $_ = `/sbin/route | grep default | tr -s " " | cut -d " " -f8`; s/\n$//; -- cgit v1.2.1