How to Export CSVs from FortiGate

FortiManager comes with a convenient built-in way to extract almost any data from the UI using a simple web based export tool, however this same functionality is not available when accessing the FortiGate UI directly.

I’ve released a simple Python script to connect to a FortiGate web UI using supplied credentials, and then pull any required REST data and convert it automatically to CSV format.


% python -f -u dan -v management -i address -o address.csv
Enter password:
Connecting to (management) as dan
Successfully logged in as dan
Fetching data...
Logging out of firewall
Saving to address.csv
% cat address.csv
hst-1,ipmask,,,v123,enable,disable,description of host 1
hst-2,ipmask,,,v123,enable,disable,description of host 2,fqdn,,,,enable,disable,comment goes here

Extending the script

The code is very simple and hopefully self-explanatory, it also comes with a number of defaults to get you started. If you wish to add another type of object, you will simply need to add the API endpoint, along with a list of required headers, for example:

# SNAT Mapping
elif args.item == 'snat':
    data = f.get(f'{base_url}api/v2/cmdb/firewall/central-snat-map/?vdom={args.vdom}').json()
    headers = ['policyid', 'status', 'orig-addr', 'dst-addr', 'srcintf', 'dstintf', 
        'nat', 'nat-ippool', 'comments']

If a field contains a list, instead of a string/int, the list is converted to a space-separated list of q_origin_keys. There is an override for ipv4_addresses headers to display both the IP and the CIDR netmask in the same field:

if header == 'ipv4_addresses': # parse list, extract ip mask for each item within the field
    for x in row[header]:
else: # parse list, extract q_origin_key for each item within the field
    for x in row[header]:

# join with a space, can't use comma due to csv
row_data.append(' '.join(map(str, subitems))) 

Available on GitHub

The full source code can be found here and is GPL-3.0 licensed for use in your projects ✌️