Skip to content

Instantly share code, notes, and snippets.

@TheBigBear
Last active November 5, 2021 10:45
Show Gist options
  • Save TheBigBear/088c6ea30b3a90cb1ffe to your computer and use it in GitHub Desktop.
Save TheBigBear/088c6ea30b3a90cb1ffe to your computer and use it in GitHub Desktop.
first simple python jinja2 writer to control config files of a bunch of openwrt WL APs

I want to write a simple python 2.7 jinja2 writer that writes out text config files for WL AccessPoints

I have the following structure in my directory ~/Documents/PyCharmProjects:

config-data.csv
mk-wl-ap-configs.py
shadow.jinja
network.jinja
wireless,jinja

and I ultimatley I want it to write the jinja2 rendered output to subdirs named after the AccessPoints

ap1:
    shadow
    network
    wireless
    
ap2:
    shadow
    network
    wireless
AP_Name AP_Prefix ULA_PREFIX LAN_IPADDRESS LAN_NETMASK LAN_GATEWAY LAN_BROADCAST LAN_DNS WAN_IPADDRESS WAN_NETMASK WAN_GATEWAY WAN_BROADCAST WAN_DNS password enc_password
Test_AP test_ap fdcd:665c:0c67::/48 10.99.142.160 255.255.240.0 10.99.128.250 10.99.143.255 10.99.100.20 10.99.100.30 10.99.168.160 255.255.248.0 10.99.168.250 10.99.175.255 10.99.168.250 redacted out;-) $1$R49FJxxxxxxxxxxxxxxxxxxxxxx9yD/
#!/usr/bin/env/python
#
# import os to get fundamentals
import os
#
# First things first, we need to import the csv module
import csv
csvFile = 'config-data.csv'
configdata = {}
print('Reading file %s' % csvFile)
configdata=csv.DictReader(open(csvFile, 'rU'), delimiter=',')
print "\nHere is the config-data dictionary "
print configdata
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# some debug lines
#print "\nWe are in directory "
#print THIS_DIR
def print_transform():
# Create the jinja2 environment.
j2_env = Environment(loader=FileSystemLoader(THIS_DIR))
print j2_env.get_template('network.jinja').render()
if __name__ == '__main__':
print_transform()
#!/usr/bin/env/python
#
# import os to get fundamentals
import os
#
# First things first, we need to import the csv module
import csv
# and for debugging , I like my pprint functions
import pprint
# let's initialize my variables
csvFile = 'config-data.csv'
j2_env = {}
configs = {}
config = {}
data = {}
template = {}
item = {}
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
j2_env = Environment(loader=FileSystemLoader(THIS_DIR))
# open my jinja template config files (*.jinja)
configs = ['shadow', 'network', 'wireless']
with open(csvFile, 'rU') as f:
data = csv.DictReader(f, delimiter=',')
# render the templates to output files in their own subdirectory
for config in configs:
template = j2_env.get_template('%s.jinja' % config)
for item in data:
with open('%s-%s' % (config, item['AP_Name'])) as f:
f.write(template.render(item))
#!/usr/bin/env/python
#
# import os to get fundamentals
import os
#
# First things first, we need to import the csv module
import csv
# and for debugging , I like my pprint functions
import pprint
# let's initialize my variables
csvFile = 'config-data.csv'
j2_env = {}
configs = {}
config = {}
data = {}
template = {}
item = {}
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
j2_env = Environment(loader=FileSystemLoader(THIS_DIR))
# open my jinja template config files (*.jinja)
configs = ['shadow', 'network', 'wireless']
with open(csvFile, 'rU') as tf:
data = csv.DictReader(tf, delimiter=',')
# render the templates to output files in their own subdirectory
for config in configs:
template = j2_env.get_template('%s.jinja' % config)
for item in data:
with open('%s-%s' % (config, item['AP_Name'])) as rf:
rf.write(template.render(item))
#!/usr/bin/env/python
#
# import os to get fundamentals
import os
#
# First things first, we need to import the csv module
import csv
# and for debugging , I like my pprint functions
import pprint
# let's initialize my variables
csvFile = 'config-data.csv'
j2_env = {}
configs = {}
config = {}
data = {}
template = {}
item = {}
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
j2_env = Environment(loader=FileSystemLoader(THIS_DIR))
# open my jinja template config files (*.jinja)
configs = ['shadow', 'network', 'wireless']
with open(csvFile, 'rU') as tf:
data = csv.DictReader(tf, delimiter=',')
# render the templates to output files in their own subdirectory
for config in configs:
template = j2_env.get_template('%s.jinja' % config)
for item in data:
with open('%s/etc/config/%s' % (item['AP_Name'], config), 'w') as rf:
rf.write(template.render(item))
#!/usr/bin/env/python
#
# import os to get fundamentals
import os
#
# First things first, we need to import the csv module
import csv
# and for debugging , I like my pprint functions
import pprint
# let's initialize my variables
csvFile = 'config-data.csv'
j2_env = {}
configs = {}
config = {}
data = {}
template = {}
item = {}
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
j2_env = Environment(loader=FileSystemLoader(THIS_DIR))
# open my jinja template config files (*.jinja)
configs = ['shadow', 'network', 'wireless']
with open(csvFile, 'rU') as tf:
data = list(csv.DictReader(tf, delimiter=','))
# render the templates to output files in their own subdirectory
for config in configs:
template = j2_env.get_template('%s.jinja' % config)
for item in data:
with open('%s/etc/config/%s' % (item['AP_Name'], config), 'w') as rf:
rf.write(template.render(item))
#!/usr/bin/env/python
#
# import os to get fundamentals
import os
#
# First things first, we need to import the csv module
import csv
# and for debugging , I like my pprint functions
import pprint
# let's initialize my variables
csvFile = 'config-data.csv'
j2_env = {}
configs = {}
config = {}
data = {}
template = {}
item = {}
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
j2_env = Environment(loader=FileSystemLoader(THIS_DIR))
# open my jinja template config files (*.jinja)
configs = ['shadow', 'network', 'wireless']
with open(csvFile, 'rU') as tf:
data = list(csv.DictReader(tf, delimiter=','))
# render the templates to output files in their own subdirectory
for config in configs:
template = j2_env.get_template('%s.jinja' % config)
for item in data:
if config == "shadow":
with open('%s/etc/%s' % (item['AP_Name'], config), 'w') as rf:
rf.write(template.render(item))
else:
with open('%s/etc/config/%s' % (item['AP_Name'], config), 'w') as rf:
rf.write(template.render(item))
config globals 'globals'
option ula_prefix '{{ AP_Name.ULA_PREFIX }}'
config interface 'lan'
option force_link '1'
option type 'bridge'
option proto 'static'
option ipaddr '{{ AP_Name.LAN_IPADDR }}'
option netmask '{{ AP_Name.LAN_NETMASK }}'
option gateway '{{ AP_Name.LAN_GATEWAY }}'
option broadcast '{{ AP_Name.LAN_BROADCAST }}'
option dns '{{ AP_Name.LAN_DNS }}'
option _orig_ifname 'eth1 wlan0'
option _orig_bridge 'true'
option ifname 'eth0.1 eth0.128 eth1 eth1.1 eth1.128'
config interface 'wan'
option _orig_ifname 'eth0'
option _orig_bridge 'false'
option proto 'static'
option ipaddr '{{ AP_Name.WAN_IPADDR }}'
option netmask '{{ AP_Name.WAN_NETMASK }}'
option gateway '{{ AP_Name.WAN_GATEWAY }}'
option broadcast '{{ AP_Name.WAN_BROADCAST }}'
option dns '{{ AP_Name.WAN_DNS }}'
option type 'bridge'
option ifname 'eth0 eth0.1 eth0.96 eth1.1 eth1.96'
root:{{ enc_password }}:16491:0:99999:7:::
daemon:*:0:0:99999:7:::
ftp:*:0:0:99999:7:::
network:*:0:0:99999:7:::
nobody:*:0:0:99999:7:::
config wifi-iface
option device 'radio0'
option mode 'ap'
option ssid '{{ AP_Name.AP_Prefix }}-office'
option encryption 'psk2+ccmp'
option hidden '1'
option network 'lan'
option key 'redacted out ;-)'
config wifi-iface
option device 'radio0'
option mode 'ap'
option encryption 'psk2+ccmp'
option ssid '{{ AP_Name.AP_Prefix }}-servers'
option hidden '1'
option network 'wan'
option disabled '1'
option key 'redacted out ;-)'
config wifi-iface
option device 'radio0'
option mode 'ap'
option encryption 'psk2+ccmp'
option ssid '{{ AP_Name.AP_Prefix }}-residents'
option key 'redacted out ;-)'
option network 'residents'
config wifi-iface
option device 'radio0'
option mode 'ap'
option encryption 'psk2+ccmp'
option ssid '{{ AP_Name.AP_Prefix }}-office-guests'
option network 'officeguests'
option key 'redacted out ;-)'
config wifi-iface
option device 'radio0'
option mode 'ap'
option encryption 'psk2+ccmp'
option key 'redacted out ;-)'
option network 'accomguests'
option ssid '{{ AP_Name.AP_Prefix }}-accom-guests'
@TheBigBear
Copy link
Author

And most likely final adjustment now.

Latest and greatest is: "mk-wl-ap-configs_v6.py"

It now puts the jinja2 rendered 'shadow' output file into the place openwrt expects and needs it.

The internet is a great place.
I would have never managed to finish this over a weekend without the guys and gals on Freenet IRC #python

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment