[pve-devel] [PATCH v2 pve-common 2/2] Added PVE::Network::tcp_ping to replace Net::Ping

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Aug 25 15:05:23 CEST 2015


We use Net::Ping twice in pve-storage (once for ISCSIPlugin
and once in GlusterfsPlugin, both with the 'tcp' variant.),
but Net::Ping doesn't support IPv6.
---
 src/PVE/Network.pm | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm
index ff4a89c..ca24b16 100644
--- a/src/PVE/Network.pm
+++ b/src/PVE/Network.pm
@@ -6,6 +6,7 @@ use PVE::Tools qw(run_command);
 use PVE::ProcFSTools;
 use PVE::INotify;
 use File::Basename;
+use IO::Socket::IP;
 
 # host network related utility functions
 
@@ -437,4 +438,21 @@ sub activate_bridge_vlan {
     return $bridgevlan;
 }
 
+sub tcp_ping {
+    my ($host, $port, $timeout) = @_;
+
+    $timeout = 3 if !$timeout; # sane default
+    $port = 7 if !$port; # echo port
+
+    my $sock;
+    my $result = PVE::Tools::run_with_timeout($timeout, sub {
+	$sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM);
+    });
+    if ($sock) {
+	$sock->close();
+	$result = 1;
+    }
+    return $result;
+}
+
 1;
-- 
2.1.4





More information about the pve-devel mailing list