-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-lsof.pl
executable file
·61 lines (50 loc) · 1.7 KB
/
format-lsof.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl
use warnings;
use strict;
use List::Util 'max';
# Field indices in lsof output, split on [\s:]+
use constant COMMAND => 0;
use constant HOST => 8;
use constant PORT => 9;
# Sample input that this command parses
# COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
# Code\x20Helper 1985 ccherlin 43u IPv4 0x355daa078b4355ff 0t0 TCP localhost:31760 (LISTEN)
# Code\x20Helper 1988 ccherlin 43u IPv4 0x355daa078ab76f7f 0t0 TCP localhost:8687 (LISTEN)
# Note that Code Helper gets mangled to Code\x20Helper
my @lines = `lsof +c0 -iTCP -sTCP:LISTEN -P`;
shift @lines; # Remove header row
chomp @lines;
if (@lines == 0) {
STDERR->print("No output received from lsof\n");
exit($? >> 8);
}
my %results;
my $hostWidth = -1;
foreach my $line (@lines) {
my @fields = split(/[\s:]+/, $line); # split on whitespace OR colon
my ($command, $host, $port) = @fields[COMMAND, HOST, PORT];
$command =~ s/\\x20/ /; # unmangle spaces
$hostWidth = max($hostWidth, length($host));
$results{$command}{$host}{$port}++;
};
for my $command (sort { lc $a cmp lc $b or $a cmp $b } keys %results) {
print($command, $/);
for my $host (sort keys %{$results{$command}}) {
printf(qq(\t\%-${hostWidth}s ), $host);
my @ports = sort {$a <=> $b } keys %{$results{$command}{$host}};
print(join(' ', mergeRuns(@ports)), $/);
}
}
# Input to mergeRuns must be sorted in ascending order.
sub mergeRuns {
if (@_ == 0) {
return ();
}
my $runStart = shift;
my $current = $runStart;
while (@_ > 0 and $_[0] == ($current + 1)) {
$current = shift;
}
my $result = $runStart == $current ? $runStart : "$runStart-$current";
return ($result, mergeRuns(@_));
}