Hello again
It’s been a while since my last entry and i blame covid 🙂
Jokes aside, I used that 4 month break to do some thinking about where I want to be in a few years’ time. I kept learning python and watching how networks become more devops-oriented. The harsh reality is that most of network engineers will either lose their jobs or become programmers. There’s no denying that at this point. Also, I personally feel that i’m a bit tired of good old network configuration (which is why i never did my ccie) and it’s time to make another step forward, just like 9 years ago when I joined the sonicwall support team. From now on my posts will concentrate more on how to automate operations rather than on operations per se.
Let’s start, then.
Part of what i do at work has to do with configuring new routers and sending them to client sites. I get a telnet connection through a console server to those routers, which isn’t ideal. Part 1 is coming up with the actual config, which is always a pain because you need to replace the config in a number of places. I’ve therefore come up with the following python script which:
- opens a GUI where you can put your config variables
- generates a config and saves the result to a test.txt file on the desktop
- archives the config as a <hostname>.txt file on the desktop
from networkconfgen import NetworkConfGen import sys import os import PySimpleGUI as sg import shutil import socket from tkinter import * from tkinter import messagebox #this creates the GUI layout sg.theme('DarkBlue1') layout = [[sg.Text('My config generator')], [sg.Text('LAN IP', size=(21, 2)),sg.InputText('x.y.z.a'),sg.Text('subnet mask', size=(18, 2)),sg.InputText('255.255.255.248')], [sg.Text('WAN IP', size=(21, 2)),sg.InputText('a.s.d.f'),sg.Text('subnet mask', size=(12, 2)),sg.InputText('255.255.255.248')], [sg.Text('hostname', size=(21, 2)),sg.InputText('testrouter')], [sg.Text('VRRP IP', size=(21, 2)),sg.InputText('x.y.z.b')], [sg.Text('WAN next hop', size=(21, 2)),sg.InputText()], [sg.Text('Serial Number', size=(21, 2)),sg.InputText('BIGBROWNFOX')], [sg.Text('tunnel1 IP', size=(21, 2)),sg.InputText('172.16.0.x'),sg.Text('subnet mask', size=(12, 2)),sg.InputText('255.255.255.0')], [sg.Text('tunnel2 IP', size=(21, 2)),sg.InputText('172.16.1.x'),sg.Text('subnet mask', size=(12, 2)),sg.InputText('255.255.255.0')], [sg.Text('loopback', size=(21, 2)),sg.InputText('172.16.2.x'),sg.Text('subnet mask', size=(12, 2)),sg.InputText('255.255.255.255')], [sg.Text('network address of LAN', size=(21, 2)),sg.InputText('x.y.z.0'),sg.Text('subnet mask in slash format', size=(12, 2)),sg.InputText('/29')], [sg.OK()]] window = sg.Window('Window Title', layout) event, values = window.read() #this reads the variables from the array generated by the GUI. lanip = values[0] lanipsubnetmask = values [1] wanip = values[2] wanipsubnetmask = values [3] hostname = values[4] virtlanip = values[5] nexthop = values[6] serial = values[7] tunnel1 = values[8] tunnel1subnetmask = values[9] tunnel2 = values[10] tunnel2subnetmask = values[11] loopback = values[12] loopbacksubnetmask = values[13] networklan = values[14] networklansubnetmask = values[15] #this checks if values in all fields are valid IP addresses try: socket.inet_aton(lanip) socket.inet_aton(lanipsubnetmask) socket.inet_aton(wanip) socket.inet_aton(wanipsubnetmask) socket.inet_aton(virtlanip) socket.inet_aton(tunnel1) socket.inet_aton(tunnel2) socket.inet_aton(loopback) socket.inet_aton(networklan) # legal except socket.error: messagebox.showerror(title="OH BLIMEY", message="ADDRESSING MISTAKE") window.close() #this creates a confgen object confgen = NetworkConfGen() template = """ #here put any configuration you need with variables in the double brackets, example: hostname {{hostname}} interface Gi0/0 ip address {{WANIP}} {{WANIPSUNETMASK}} """ parameters = { "hostname": hostname, "WANIP": wanip, "WANIPSUBNETMASK": wanipsubnetmask, "REALLANIP": lanip, "REALLANIPSUBNETMASK": lanipsubnetmask, "VIRTLANIP": virtlanip, "NEXTHOPWANIP": nexthop, "SERIAL": serial, "TUNNEL1IP": tunnel1, "TUNNEL1IPSUBNETMASK": tunnel1subnetmask, "TUNNEL2IP": tunnel2, "TUNNEL2SUBNETMASK": tunnel2subnetmask, "LOOPBACKIP": loopback, "LOOPBACKIPSUBNETMASK": loopbacksubnetmask, "NETWORKLAN": networklan, "NETWORKLANSUBNETMASK": networklansubnetmask } result = confgen.render_from_string( template_content=template, parameters=parameters ) if not result.render_error: sys.stdout = open("test.txt", "w") print(result.template_result) sys.stdout.close() else: print("Something went wrong: %s" % result.error_text) oldname = 'test.txt' shutil.copyfile(oldname,hostname)