AnyEvent::Ping::DGRAM - Ping as Non-Root
version 0.1
use AnyEvent;
use AnyEvent::Ping::DGRAM;
my $host = shift || '4.2.2.2';
my $times = shift || 4;
my $timeout = shift || 5;
my $package_s = shift || 56;
my $c = AnyEvent->condvar;
my $ping = AnyEvent::Ping::DGRAM->new;
print "PING $host $package_s(@{[$package_s+8]}) bytes of data\n";
$ping->ping($host, $times, $timeout, sub {
my $results = shift;
foreach my $result (@$results) {
my $status = $result->[0];
my $time = $result->[1];
printf "%s from %s: time=%f ms\n",
$status, $host, $time * 1000;
};
$c->send;
});
$c->recv;
$ping->end;
AnyEvent::Ping::DGRAM is an asynchronous AnyEvent pinger.
It is a subclass of AnyEvent::Ping that does not require the code to run under root.
This class depends on the DGRAM style icmp ping socket that linux provides. Doing so allows your code to run as a non-root user. However, you will need to whitelist a group that the code's user is in. The relevent sysctl takes a range of groups:
echo '0 2000' > /proc/sys/net/ipv4/ping_group_range
or to make it last between reboots:
echo "net.ipv4.ping_group_range=0 2000" > /etc/sysctl.d/60-icmp-ping.conf
/etc/init.d/procps restart