]> git.proxmox.com Git - pve-common.git/commitdiff
add get_local_ip_from_cidr
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 28 Oct 2016 09:53:24 +0000 (11:53 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 28 Oct 2016 10:01:38 +0000 (12:01 +0200)
The get_local_ip_from_cidr method can be used to determine addresses
configured on interfaces from the callers node which are located
in the subnet. The subnet can be passed in CIDR notation.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/Network.pm

index 3a0d778dc38bb66012b5a21395d629548be55db4..1c037702d3b62ffd3189a8a3e150f79319c2feee 100644 (file)
@@ -555,6 +555,27 @@ sub is_ip_in_cidr {
     return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP;
 }
 
+
+sub get_local_ip_from_cidr {
+    my ($cidr) = @_;
+
+    my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'];
+
+    my $IPs = [];
+
+    my $code = sub {
+       my $line = shift;
+
+       if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) {
+           push @$IPs, $1;
+       }
+    };
+
+    PVE::Tools::run_command($cmd, outfunc => $code);
+
+    return $IPs;
+}
+
 sub lock_network {
     my ($code, @param) = @_;
     my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param);