| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # system::netstat Brik | ||
| 5 | # | ||
| 6 | package Metabrik::System::Netstat; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Shell::Command); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable listen) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | commands => { | ||
| 19 | udp_listen => [ ], | ||
| 20 | tcp_listen => [ ], | ||
| 21 | }, | ||
| 22 | require_binaries => { | ||
| 23 | 'netstat', => [ ], | ||
| 24 | }, | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub udp_listen { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | $self->as_array(0); | ||
| 32 | $self->as_matrix(1); | ||
| 33 | my $lines = $self->capture("netstat -an"); | ||
| 34 | |||
| 35 | my $listen = { }; | ||
| 36 | for my $line (@$lines) { | ||
| 37 | my $proto = $line->[0]; | ||
| 38 | if ($proto eq 'udp') { | ||
| 39 | $proto = 'udp4'; # Rewrite for FreeBSD and uniformity | ||
| 40 | } | ||
| 41 | if ($proto eq 'udp4' || $line->[0] eq 'udp6') { | ||
| 42 | my $ip_port = $line->[3]; | ||
| 43 | my ($ip, $port) = $ip_port =~ /^(.*)[:\.](\d+)$/; # : is Linux separator, . is FreeBSD one | ||
| 44 | $listen->{$proto}->{$ip_port} = { ip => $ip, port => $port }; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | return $listen; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub tcp_listen { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | $self->as_array(0); | ||
| 55 | $self->as_matrix(1); | ||
| 56 | my $lines = $self->capture("netstat -an"); | ||
| 57 | |||
| 58 | my $listen = { }; | ||
| 59 | for my $line (@$lines) { | ||
| 60 | my $proto = $line->[0]; | ||
| 61 | if ($proto eq 'tcp') { | ||
| 62 | $proto = 'tcp4'; # Rewrite for FreeBSD and uniformity | ||
| 63 | } | ||
| 64 | if ($proto eq 'tcp4' || $proto eq 'tcp6') { | ||
| 65 | my $ip_port = $line->[3]; | ||
| 66 | my ($ip, $port) = $ip_port =~ /^(.*)[:\.](\d+)$/; # : is Linux separator, . is FreeBSD one | ||
| 67 | $listen->{$proto}->{$ip_port} = { ip => $ip, port => $port }; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | return $listen; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | |||
| 76 | __END__ | ||