Skip to content

Instantly share code, notes, and snippets.

@ruped24
Created August 1, 2015 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruped24/3c647ecd58d7033e1eff to your computer and use it in GitHub Desktop.
Save ruped24/3c647ecd58d7033e1eff to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
my @types = qw/nat mangle filter/;
$SIG{INT} = sub{print "\e[?25h\e[u"; exit};
print "\e[40;37m\e[2J\e[?25l";
while (1) {
print "\e[0;0H";
my %output = map {$_ => scalar `iptables -t $_ -L -v -Z`} @types;
foreach my $type (@types) {
print "\e[01;34m------", uc($type), '-' x (73 - length($type)), "\n";
$output{$type} =~ s/ pkts[^\n]*\n(\n|Zeroing)/$1/gs;
foreach my $line (split /\n/, $output{$type}) {
next if $line =~ m/^Zeroing/ || $line eq '';
print $line =~ m/^\s*(\d+)/ || $line =~ m/(\d+) packets/
? ($1 > 0
? ($line =~ m/DROP|DENY|REJECT/
? "\e[01;40;31m"
: "\e[01;40;32m")
: "\e[00;40;37m")
: "\e[00;40;33m";
print "\e[K$line\e[01;40;37m\n"
}
}
print "\e[s";
sleep 1
}
@ruped24
Copy link
Author

ruped24 commented Aug 1, 2015

Original script can be found here: http://perlmonks.org/?node_id=513732

Every second, it grabs output from the iptables list utility, including counters (which are then zeroed).
It displays this data with extraneous lines removed, highlighting the lines with non-zero packet counts in color.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment