-
Notifications
You must be signed in to change notification settings - Fork 11
/
showcidrs
executable file
·59 lines (43 loc) · 1.17 KB
/
showcidrs
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
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use Net::IPv4Addr qw ( :all );
use Regexp::Common qw ( net );
use Term::ANSIColor qw ( :constants );
# highlight any IPs found that are in the given cidr range.
# written to help locate internal traffic in logs.
GetOptions(
"c|color|colour" => \( my $coloured = 0 ),
"h|help" => \&usage,
);
my $cidr_range = shift || usage();
while ( <> ) {
s/\b$RE{net}{IPv4}{-keep}\b/check_network($1)/eg;
print;
}
#-----------------------------------------#
sub check_network {
my $ip = shift;
my $matched;
if ( ipv4_in_network( $cidr_range, $ip ) ) {
$matched = $coloured ? GREEN . $ip . RESET : " !!$ip!! ";
} else {
$matched = $coloured ? RED . $ip . RESET : " --$ip-- ";
}
return $matched;
}
#-----------------------------------------#
sub usage {
my $appname = basename($0);
print<<EOH;
$appname: <cidrrange> [-c] [filename]
-c\tTurn on coloured output.
showcidrs -c 60.11.55.0/24 /var/log/syslog
This script can either be passed a filename or be invoked in a pipe, in
which case it operated on STDIN.
EOH
exit 0;
}
#-----------------------------------------#