Convert iptables-save output to shell script

File download from: http://danieldegraaf.afraid.org/info/iptables/mkscript

To use:

iptables-save | mkscript.pl > firewall.sh
mkscript.pl
#!/usr/bin/perl 
use strict;

if (@ARGV && $ARGV[0] =~ /^-/) {
	print 'converts an iptables-save file to a shell script',
	"\nUse: $0 [filename]\n";
	exit 0;
}

open I, shift || '-' or die $!;

my($table,$ipt);

print "#!/bin/sh
#iptables script, generated from iptables-save file
IPT='/sbin/iptables'
";

while (<I>) {
	if (/^\s*(#|$)/) {
		print;
		next;
	}
	if (/^\*(.*)/) {
		$table = $1;
		$ipt = $table eq 'filter' ? '$IPT' : '$IPT -t '.$table;
		print "$ipt -F\n";
		print "$ipt -X\n";
		next;
	} elsif (/^COMMIT/) {
		$table = 0;
		next;
	}
	die unless $table;
	if (/^:(\S+) +([^- ]\S*)/) {
		print "$ipt -P $1 $2\n";
		next;
	} elsif (/^:(\S+)/) {
		print "$ipt -N $1\n";
		next;
	}
	s/^\[[0-9:]+\]\s*//;
	die unless /^-A/;
	print "$ipt $_";
}
tips/perl_script_to_convert_iptables-save_output_to_shell_script.txt · Last modified: 2011-11-03 15:50 by admin
Back to top
GNU Free Documentation License 1.3
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0