forked from maaaaz/fgpoliciestocsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fgpoliciestocsv.pl
57 lines (49 loc) · 1.47 KB
/
fgpoliciestocsv.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
# This script has been originally developed by Sebastian Knoop-Troullier aka 'firewallguru' and published on http://firewallguru.blogspot.fr/2014/04/exporting-firewall-rules-to-csv.html
# Kudos to firewallguru !
my $output = "policies-out.csv";
my $policyid = 0;
my $setting = "";
my %policies;
my %seen;
my $in_policy_block = 0;
my @order_keys;
my $order_key = 0;
open(OUTFILE,">$output") || die "Can't open file $output: $!\n";
while (<>) {
if ($in_policy_block) {
if (/^\s*edit\s+(\d+)/i) {
# start of new policy
$policyid = $1;
} elsif (/^\s*set\s+(\S+)\s+(.*)$/i) {
# it's a setting
my ($key,$value) = ($1,$2);
$value =~ tr/\"\015\012\n\r//d;
$order_keys[$order_key++] = $key unless $seen{$key}++;
$policies{$policyid}{$key} = $value;
} elsif (/^\s*end/i) {
$in_policy_block = 0;
}
} elsif (/^\s*config firewall policy/i) {
$in_policy_block = 1;
}
}
# print out our header
print OUTFILE "id";
foreach my $key (@order_keys) {
print OUTFILE ";$key";
}
print OUTFILE "\n";
# now print out each record
foreach my $policy (sort keys %policies) {
print OUTFILE "$policy";
foreach my $key (@order_keys) {
if (defined($policies{$policy}{$key})) {
print OUTFILE ";$policies{$policy}{$key}";
} else {
print OUTFILE ";";
}
}
print OUTFILE "\n";
}
close(OUTFILE);