Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
In trying to solve a problem setting up iptables on my Linux gateway box, I wrote a Perl script to display netfilter activity in real-time. 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. It's designed to run in an xterm window and uses ANSI escape sequences to control the text. To be effective, the xterm window needs to be at least as high as the output, else scrolling will ruin the visual effect. Also, the code may be iptables version-dependent, as it matches certain keywords for the formatting. Once started, it can be stopped with a ctl-C, which will restore some of the display settings. Herewith is the code:
#!/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 }

In reply to Real-time Iptables Monitor by Dr. Mu

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-12-04 20:07 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (25 votes). Check out past polls.