Skip to content

Instantly share code, notes, and snippets.

@wakaba
Created June 18, 2012 09:32
Show Gist options
  • Save wakaba/2947640 to your computer and use it in GitHub Desktop.
Save wakaba/2947640 to your computer and use it in GitHub Desktop.
require Parse::Netstat;
my $timer; $timer = AnyEvent->timer(
interval => 0.6,
cb => sub {
my $netstat;
AnyEvent::Util::run_cmd(
'LANG=C netstat --inet --inet6 -n -p -l',
'>' => \$netstat,
'2>' => \(my $dummy),
)->cb(sub {
my $stat = Parse::Netstat::parse_netstat(output => $netstat);
for (@{$stat->[2]->{active_conns} or []}) {
next unless defined $_->{pid};
if ($_->{pid} == $pid and
$_->{local_port} == $port and
$_->{state} eq 'LISTEN') {
$cv_start->send;
undef $timer;
last;
}
}
});
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment