From 76f873016c5996ea2c1a8219a01213b8aca64742 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 8 Dec 2010 01:48:35 +0600 Subject: Add a method to check whether an IPv4 address is a primary address to Vyatta::Misc --- lib/Vyatta/Misc.pm | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Vyatta/Misc.pm b/lib/Vyatta/Misc.pm index a103c69..a6545ef 100755 --- a/lib/Vyatta/Misc.pm +++ b/lib/Vyatta/Misc.pm @@ -27,7 +27,7 @@ our @ISA = qw(Exporter); our @EXPORT = qw(getInterfaces getIP getNetAddIP get_sysfs_value is_address_enabled is_dhcp_enabled get_ipaddr_intf_hash isIpAddress is_ip_v4_or_v6 interface_description - is_local_address); + is_local_address, is_primary_address); our @EXPORT_OK = qw(generate_dhclient_intf_files getInterfacesIPadresses getPortRuleString); @@ -51,6 +51,26 @@ sub get_ipaddr_intf_hash { return \%config_ipaddrs; } +# Check whether an address is the primary address on some interface +sub is_primary_address { + my $ip_address = shift; + + my $ref = get_ipaddr_intf_hash(); + my %hash = %{$ref}; + if (!defined $hash{$ip_address}) { + return 1; + } + + my $line = `ip address show $hash{$ip_address} | grep 'inet' | head -n 1`; + chomp($line); + my $primary_address = undef; + if ($line =~ /inet\s+([0-9.]+)\/.*\s([\w.]+)$/) { + $primary_address = $1; + } + + return 1 if ($ip_address eq $primary_address); + return; +} sub get_sysfs_value { my ( $intf, $name ) = @_; -- cgit v1.2.3