Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Monks,

I would like to thank you for your help to develop my code:

#!/usr/bin/perl #-------------------------------------------------------------- # Script to write iptables-save syntaxed file, add rules to # relating table files mangle, nat, filter. example table file # syntax: # INPUT DROP (Chain Policy) # -p tcp -s 192.168.0.10 -d 80.45.76.34 --dport 143 -j ACCEPT (Rule) #-------------------------------------------------------------- use strict; use warnings; use Data::Dumper; use Tie::IxHash; #-------------------------------------------------------------- # Declare Global Vars #-------------------------------------------------------------- my $filter = "filter"; my $mangle = "mangle"; my $nat = "nat"; my @tables = ($mangle, $nat, $filter); my $date = qx(date +%d%m%y%T|sed 's/\://g'); chomp $date; my $outfile = "firewall-$date"; #-------------------------------------------------------------- # Tie to keep hash order no memory optimization #-------------------------------------------------------------- tie (my %rules, 'Tie::IxHash'); #-------------------------------------------------------------- # Open each table file, match ^uppper case, split (chain policy) # , chain as hash key, push remaining lines (rules) on array ref # to key value. Print keys:policy then values, COMMIT #-------------------------------------------------------------- sub main { open IPTSAVE, ">> $outfile" or die "Failed to open $outfile : $!"; for my $file (@tables) { %rules = (); my $r = \my @uncategorised; my ($chain,$policy); print IPTSAVE "\*$file\n"; local *_; open (FILE, "<$file") or die "Failed to open $file: $!"; while(<FILE>) { chomp; next if ( /^#|^\s^/ ); if ( /^[[:upper:]]/ ) { ($chain,$policy) = split(/ /); $r = \@{$rules{$chain}}; } else { push @$r => $_; } } close FILE; for my $key ( keys %rules ) { print IPTSAVE ":$key $policy [0:0]\n"; } for my $key ( keys %rules ) { foreach(@{$rules{$key}}) { print IPTSAVE "-A $key $_\n"; } } print IPTSAVE "COMMIT\n"; } { close IPTSAVE if eof }; } #-------------------------------------------------------------- # Run Main() #-------------------------------------------------------------- main();
Regards,

k_grdn


In reply to Re^4: Multiple Array Loops by k_grdn
in thread Multiple Array Loops by k_grdn

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-20 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found