Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # |
| 3 | # |
| 4 | # A script that generates text output of the ebtables rules. |
| 5 | # Similar to iptables-save. |
| 6 | # |
| 7 | # It can be used to store active configuration to /etc/sysconfig/ebtables |
| 8 | |
| 9 | use strict; |
| 10 | my $table; |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 11 | my $ebtables = "__EXEC_PATH__/ebtables"; |
| 12 | my $cnt = ""; |
| 13 | my $version = "1.0"; |
| 14 | my $table_name; |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 15 | |
| 16 | # ======================================================== |
| 17 | # Process filter table |
| 18 | # ======================================================== |
| 19 | sub process_table { |
| 20 | my $chain = ""; |
| 21 | my $rules = ""; |
| 22 | my $chains = ""; |
| 23 | my $line = ""; |
| 24 | |
| 25 | foreach $line (split("\n",$_[0])) { |
| 26 | if ($line =~ m/Bridge table: (.*)/) { |
| 27 | print "*$1\n"; |
| 28 | next; |
| 29 | } |
| 30 | if ($line =~ m/Bridge chain: (.*?), entries:.* policy: (.*)/) { |
| 31 | $chains = $chains . ":$1 $2\n"; |
| 32 | $chain = $1; |
| 33 | next; |
| 34 | } |
| 35 | if ($line =~ m/^$/) { |
| 36 | next; |
| 37 | } |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 38 | if ($cnt eq "--Lc") { |
| 39 | $line =~ s/, pcnt = (.*) -- bcnt = (.*)/-c $1 $2/; |
| 40 | } |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 41 | $rules = $rules . "-A $chain $line\n"; |
| 42 | } |
| 43 | |
| 44 | print $chains; |
| 45 | print $rules; |
| 46 | print "\n"; |
| 47 | } |
| 48 | # ======================================================== |
| 49 | |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 50 | unless (-x $ebtables) { exit -1 }; |
| 51 | print "# Generated by ebtables-save v$version on " . `date`; |
| 52 | if (defined($ENV{'EBTABLES_SAVE_COUNTER'}) && $ENV{'EBTABLES_SAVE_COUNTER'} eq "yes") { |
| 53 | $cnt = "--Lc"; |
| 54 | } |
| 55 | foreach $table_name (split("\n", `grep -E '^ebtable_' /proc/modules | cut -f1 -d' ' | sed s/ebtable_//`)) { |
| 56 | $table =`$ebtables -t $table_name -L $cnt`; |
| 57 | unless ($? == 0) { print $table; exit -1 }; |
| 58 | &process_table($table); |
| 59 | } |