CSV to Address External Datagroup File
Code is community submitted, community supported, and recognized as ‘Use At Your Own Risk’.
Short Description
I was today years old when I found out you could upload external data-group files to the GUI.
I couldn't figure out the format, however, until John_Alam pointed me to the K73862425 knowledge article.
Problem solved by this Code Snippet
This script takes a CSV file of IPs and/or networks keys, with or without values, and transforms it into an acceptable format for upload to the GUI.
How to use this Code Snippet
python dg_format.py
Code Snippet Meta Information
- Version: .1
- Coding Language: python 3.11
Full Code Snippet
"""
Source File CSV Contents:
10.10.10.10,
10.10.10.11,hi there
10.10.11.0/24,
10.10.12.0/24,/url/path/here
10.10.13.0/24,
Output File Txt Contents:
host 10.10.10.10,
host 10.10.10.11 := hi there,
network 10.10.11.0/24,
network 10.10.12.0/24 := /url/path/here,
network 10.10.13.0/24,
"""
import csv
with open('dg_ipaddrs.csv', mode='r', encoding='utf-8-sig') as f:
reader = csv.reader(f)
ipaddrs = list(reader)
f_out = open('dg_formatted.txt', 'w')
for addr in ipaddrs:
print(addr)
if "/" in addr[0] or "mask" in addr[0] or "prefexlen" in addr[0]:
if addr[1] == '':
f_out.write(f'network {addr[0]},\n')
else:
value = addr[1].split('\n')[0]
f_out.write(f'network {addr[0]} := {value},\n')
else:
if addr[1] == '':
f_out.write(f'host {addr[0]},\n')
else:
value = addr[1].split('\n')[0]
f_out.write(f'host {addr[0]} := {value},\n')
f_out.close()
Published Mar 18, 2023
Version 1.0