diff options
author | Daniil Baturin <daniil@baturin.org> | 2010-12-08 01:48:35 +0600 |
---|---|---|
committer | Stig Thormodsrud <stig@vyatta.com> | 2010-12-07 15:08:34 -0800 |
commit | 76f873016c5996ea2c1a8219a01213b8aca64742 (patch) | |
tree | 263661c0974e80934e2c922f3c727b327796448f /lib/Vyatta | |
parent | 9b02651d5f2ccea3be4a16b7a15c220b8141926c (diff) | |
download | vyatta-cfg-76f873016c5996ea2c1a8219a01213b8aca64742.tar.gz vyatta-cfg-76f873016c5996ea2c1a8219a01213b8aca64742.zip |
Add a method to check whether an IPv4 address is a primary address to Vyatta::Misc
Diffstat (limited to 'lib/Vyatta')
-rwxr-xr-x | lib/Vyatta/Misc.pm | 22 |
1 files changed, 21 insertions, 1 deletions
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 ) = @_; |