[pve-devel] [PATCH common v2] add get_local_ip_from_cidr

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Oct 28 11:53:24 CEST 2016


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 at proxmox.com>
---
 src/PVE/Network.pm | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm
index 3a0d778..1c03770 100644
--- a/src/PVE/Network.pm
+++ b/src/PVE/Network.pm
@@ -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);
-- 
2.1.4





More information about the pve-devel mailing list