Skip to content

Instantly share code, notes, and snippets.

@f5-rahm
Last active March 17, 2023 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f5-rahm/9117c688f7961ad210197bd54ab0bc1f to your computer and use it in GitHub Desktop.
Save f5-rahm/9117c688f7961ad210197bd54ab0bc1f to your computer and use it in GitHub Desktop.
"""
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:
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment