in reply to Re^4: How can I test TCP socket status in Perl?
in thread How can I test TCP socket status in Perl?
Hello again beanscake,
Since you are running on MacOS and you have by default grep(1) - Linux man page and all sweet linux network tools nc(1) - Linux man page, why do not implement your own way of checking active tcp/udp ports based on the server port.
Sample of code with output:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $serverPort = 12345; my @ActivePorts = `netstat -a | grep $serverPort`; # Strip white spaces @ActivePorts = map { s/^\s+|\s+$//g; $_; } @ActivePorts; print Dumper \@ActivePorts; __END__ $VAR1 = [ 'udp 0 0 localhost:12345 *:*' ];
I am running a UDP server localy at the moment and I can see the active port that listens to my connections.
So based on the output you can either terminate them or simply use them.
Update: Even faster than nc is ss(8) - Linux man page that provides you also pid without sudo.
Sample of code with output:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $serverPort = 12345; my @ActivePorts = `ss -lp | grep 12345`; # Strip white spaces @ActivePorts = map { s/^\s+|\s+$//g; $_; } @ActivePorts; print Dumper \@ActivePorts; __END__ $VAR1 = [ 'tcp UNCONN 0 0 127.0.0.1:ipproto-123 +45 *:* users:(("perl",8179,3))' ];
Hope this helps.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: How can I test TCP socket status in Perl?
by 1nickt (Canon) on Jul 27, 2015 at 01:59 UTC | |
by thanos1983 (Parson) on Jul 27, 2015 at 02:14 UTC |