-
Notifications
You must be signed in to change notification settings - Fork 11
/
lpdiff
executable file
·115 lines (83 loc) · 2.56 KB
/
lpdiff
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
# netstat command to list listening ip:port combinations
my $netstat = '/bin/netstat -nltu';
GetOptions(
"s|state|statefile=s" => \( my $state = '/tmp/lpdiff.state'),
"u|update" => \( my $update ),
"h|help" => \&usage,
);
my $app = basename($0);
##############################################
open( my $ns_fh, "$netstat |" )
|| die "Failed to use [$netstat]: $!\n";
my @listeners;
while(<$ns_fh>) {
my $local = ( split(/\s+/) )[3];
next unless $local =~ /:/;
push (@listeners, $local);
}
close $ns_fh;
# load state
##############################################
my @previous;
if ( -e $state ) {
open ( my $state_fh, '<', $state )
|| die "Failed to open [$state]: $!\n";
chomp (@previous = <$state_fh>);
close $state_fh;
}
# write state if --update is given
##############################################
if ( $update ) {
open ( my $state_fh, '>', $state )
|| die "Failed to open [$state]: $!\n";
print "$app: updating expected ip/ports\n";
print $state_fh join( "\n", @listeners );
close $state_fh;
exit 0;
}
# compare and print diffs
##############################################
my %current_listeners = map { $_ => 1 } @listeners;
my %previous_listeners = map { $_ => 1 } @previous;
my (@additions, @subtractions);
for my $listener (keys %current_listeners) {
push( @additions, $listener ) unless exists $previous_listeners{$listener};
}
for my $listener (keys %previous_listeners) {
push( @subtractions, $listener ) unless exists $current_listeners{$listener};
}
if ( scalar @additions ){
print "New listeners: \n";
print join("\n", @additions), "\n";
}
if ( scalar @subtractions ){
print "Expected but absent: \n";
print join("\n", @subtractions), "\n";
}
##############################################
sub usage {
print<<EOU;
lpdiff can be used to capture the currently running ports and then provide
a list of differences on future runs. It shows both new ports and old ones
thaqt have closed since its previous run.
$app - Copyright (c) 2008 Dean Wilson. Licensed under the GPL
Usage Examples:
$app # prints a list of ports changed from the last time update was run
$app --update # write the currently listening set of ports to the
# state file
$app -h # shows this information
Options:
-s | --statefile
The state file to use for tracking open ports.
-u | --update
Update the state file. This sets the baseline for fitire runs.
-h
This help and usage information.
EOU
exit 3;
}