Exporting Firewall policies into CSV

337
Exporting Firewall policies into CSV

Here, I am going to export FortiGate firewall policies into CSV.

Here is the step by step process with perl script that takes firewall policies from a text file and performs the CSV conversion.

Step 1- Install Perl Software– https://www.perl.org/get.html

Step 2- Copy below perl script in notepad and save with (Any Name).pl

[ Reff- http://firewallguru.blogspot.com/2014/04/exporting-firewall-rules-to-csv.html ]

#!/usr/bin/perl
#

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”;

Step 3- Login Fortigate Firewall via cli & type below command

#Show firewall policy

copy firewall policy into notepad and save it with (Any name).txt

Step 4- next, go to command prompt and type below command.

Export Firewall Policy
Export Firewall Policy

Where- conf2.pl is perl script, as mentioned and conf1.txt is firewall policy.

after run its export firewall policies into csv at same location…

LEAVE A REPLY

Please enter your comment!
Please enter your name here