Skip to content

Commit 9f458d2

Browse files
committed
Initial commit
0 parents  commit 9f458d2

9 files changed

+410
-0
lines changed

.gitignore

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
### macOS ###
2+
# General
3+
.DS_Store
4+
.AppleDouble
5+
.LSOverride
6+
7+
# Icon must end with two \r
8+
Icon
9+
10+
# Thumbnails
11+
._*
12+
13+
# Files that might appear in the root of a volume
14+
.DocumentRevisions-V100
15+
.fseventsd
16+
.Spotlight-V100
17+
.TemporaryItems
18+
.Trashes
19+
.VolumeIcon.icns
20+
.com.apple.timemachine.donotpresent
21+
22+
# Directories potentially created on remote AFP share
23+
.AppleDB
24+
.AppleDesktop
25+
Network Trash Folder
26+
Temporary Items
27+
.apdisk
28+
29+
### VisualStudioCode ###
30+
.vscode/*
31+
32+
### VisualStudioCode Patch ###
33+
# Ignore all local history of files
34+
.history
35+
.ionide
36+
37+
### Windows ###
38+
#Folders
39+
configs/**/*.*
40+
!configs/.gitkeep
41+
42+
# Windows thumbnail cache files
43+
Thumbs.db
44+
Thumbs.db:encryptable
45+
ehthumbs.db
46+
ehthumbs_vista.db
47+
48+
# Dump file
49+
*.stackdump
50+
51+
# Folder config file
52+
[Dd]esktop.ini
53+
54+
# Recycle Bin used on file shares
55+
$RECYCLE.BIN/
56+
57+
# Windows Installer files
58+
*.cab
59+
*.msi
60+
*.msix
61+
*.msm
62+
*.msp
63+
64+
# Windows shortcuts
65+
*.lnk

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Generate Cisco Configuration Template Using Python, Jinja2, and CSV
2+
3+
## Table of Contents
4+
5+
1. [Prerequisites](#prerequisites)
6+
2. [Getting Started](#getting-started)
7+
3. [How it Works?](#how-it-works)
8+
9+
### 1. Prerequisites
10+
11+
1. [Python @3.9.4](https://www.python.org/)
12+
2. [Jinja2 @2.11.3](https://jinja.palletsprojects.com/en/2.11.x/)
13+
3. [CSV @1.0](https://docs.python.org/3/library/csv.html)
14+
4. [Visual Studio Code](https://code.visualstudio.com/) (Optional but recommended)
15+
5. [Cisco IOS Syntax](https://marketplace.visualstudio.com/items?itemName=jamiewoodio.cisco) (Extension for Cisco IOS Syntax Highlighting)
16+
17+
---
18+
19+
### 2. Getting Started
20+
21+
In this repo, you can find four `.csv` files:
22+
23+
1. params.csv
24+
2. vlans.csv
25+
3. etherchannels.csv
26+
4. port_mapping.csv
27+
28+
and
29+
30+
1. config_generator.py
31+
2. switch.j2
32+
33+
---
34+
35+
### 3. How it Works?
36+
37+
1. Open each `.csv file` _(respectively)_, and add the configurations that meet your needs.
38+
2. Open Visual Studio Code.
39+
3. Open Terminal within VSCode (`` Ctrl+` ``).
40+
4. Run `python config_generator.py`
41+
42+
Voila :sparkles:! Your configuration will automagically open in your default text editor
43+
44+
> If the `.ios` file extension is not associated with any text editor on your machine, please associate it with VSCode.

config_generator.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
3+
# -----------------------------------------------------------------------------
4+
# Demonstrates how to generate Cisco configuration using Python, Jinja2 and csv
5+
#
6+
# (C) 2021 Osama Abbas, Cairo, Egypt
7+
# Released under GNU Public License (GPL)
8+
9+
# -----------------------------------------------------------------------------
10+
11+
# Libraries
12+
from os import path, mkdir, stat
13+
from csv import DictReader
14+
from jinja2 import Environment, FileSystemLoader
15+
import json
16+
import webbrowser
17+
from time import sleep
18+
19+
# Global Vars
20+
jinja_template = "switch.j2"
21+
output_dir = "configs"
22+
params_file = "params.csv"
23+
vlans_file = "vlans.csv"
24+
etherchannels_file = "etherchannels.csv"
25+
port_mapping = "port_mapping.csv"
26+
27+
# Handle Jinja template
28+
env = Environment(loader=FileSystemLoader("./"), trim_blocks=True, lstrip_blocks=True)
29+
template = env.get_template(jinja_template)
30+
31+
# Create configs directory if not already created
32+
if not path.exists(output_dir):
33+
mkdir(output_dir)
34+
35+
# Read the "parameters" csv sheet and convert it to a dict
36+
with open(params_file, "r") as parameters:
37+
params = DictReader(parameters)
38+
for param in params:
39+
dict_params = dict(param)
40+
41+
# Read the "vlans" csv sheet and convert it to a dict
42+
with open(vlans_file, "r") as vlans:
43+
vlans = DictReader(vlans)
44+
dict_vlans = {"vlans": []}
45+
for vlan in vlans:
46+
dict_vlans["vlans"].append(dict(vlan))
47+
48+
# Read the "etherchannels" csv sheet and convert it to a dict
49+
with open(etherchannels_file, "r") as etherchannels:
50+
portchannels = DictReader(etherchannels)
51+
dict_portchannels = {"etherchannels": []}
52+
for portchannel in portchannels:
53+
dict_portchannels["etherchannels"].append(dict(portchannel))
54+
55+
# Read the "port mapping" csv sheet and convert it to a dict
56+
with open(port_mapping, "r") as interfaces:
57+
ports = DictReader(interfaces)
58+
dict_ports = {"interfaces": []}
59+
for port in ports:
60+
dict_ports["interfaces"].append(dict(port))
61+
62+
# Combine all four csv sheets
63+
dict_params.update(dict_vlans)
64+
dict_params.update(dict_portchannels)
65+
dict_params.update(dict_ports)
66+
67+
# All dictionaries
68+
dicts = dict_params
69+
70+
# Generate the configuration template file
71+
res = template.render(dicts)
72+
file_name = dicts["hostname"]
73+
file_ext = ".ios"
74+
file_location = path.join(output_dir, file_name + file_ext)
75+
file = open(file_location, "w")
76+
file.write(res)
77+
file.close()
78+
79+
print("Configuration file '%s' is created successfully!" % (file_name + file_ext))
80+
81+
# Open file in default Text Editor for the file extension
82+
sleep(1)
83+
webbrowser.open(file_location)

configs/.gitkeep

Whitespace-only changes.

etherchannels.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id,mode,native_vlan,allowed_vlans,desc,shutdown
2+
1,trunk,100,all,Uplink to Core,no
3+
2,trunk,100,all,,

params.csv

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hostname,timezone,domain_name,stp_mode,vtp_domain,vtp_version,vtp_mode,logging_console,logging_buffer_size,http_server,errdisable,errdisable_recovery_interval,lldp,username,algorithm_type,password,enable_password,ssh_key_size,ssh_version,vty_lines,login_local,timeout,transport_input,transport_output
2+
Edge-1,EET +2,Cisco,rapid-pvst,Cisco,3,transparent,debugging,307200,no,yes,60,yes,cisco,scrypt,cisco,cisco,2048,2,0 4,yes,15 0,ssh,all

port_mapping.csv

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name,mode,native_vlan,allowed_vlans,access_vlan,voice_vlan,portfast,bpduguard,portsecurity,description
2+
GigabitEthernet1/0/1,access,,,200,300,yes,yes,yes,
3+
GigabitEthernet1/0/2,access,,,200,300,yes,yes,no,
4+
GigabitEthernet1/0/3,access,,,200,300,yes,yes,yes,
5+
GigabitEthernet1/0/4,access,,,200,300,yes,yes,no,
6+
GigabitEthernet1/0/5,access,,,200,300,yes,yes,yes,
7+
GigabitEthernet1/0/6,access,,,200,300,yes,yes,no,
8+
GigabitEthernet1/0/7,access,,,200,300,yes,yes,yes,
9+
GigabitEthernet1/0/8,access,,,200,300,yes,yes,no,
10+
GigabitEthernet1/0/9,access,,,200,300,yes,yes,yes,
11+
GigabitEthernet1/0/10,access,,,200,300,yes,yes,no,
12+
GigabitEthernet1/0/11,access,,,200,300,yes,yes,yes,
13+
GigabitEthernet1/0/12,access,,,200,300,yes,yes,no,
14+
GigabitEthernet1/0/13,access,,,200,300,yes,yes,yes,
15+
GigabitEthernet1/0/14,access,,,200,300,yes,yes,no,
16+
GigabitEthernet1/0/15,access,,,200,300,yes,yes,yes,
17+
GigabitEthernet1/0/16,access,,,200,300,yes,yes,no,
18+
GigabitEthernet1/0/17,access,,,200,300,yes,yes,yes,
19+
GigabitEthernet1/0/18,access,,,200,300,yes,yes,no,
20+
GigabitEthernet1/0/19,access,,,200,300,yes,yes,yes,
21+
GigabitEthernet1/0/20,access,,,200,300,yes,yes,no,
22+
GigabitEthernet1/0/21,access,,,200,300,yes,yes,yes,
23+
GigabitEthernet1/0/22,access,,,200,300,yes,yes,no,
24+
GigabitEthernet1/0/23,access,,,200,300,yes,yes,yes,
25+
GigabitEthernet1/0/24,access,,,200,300,yes,yes,no,
26+
TenGigabitEthernet1/1/1,channel-group 1 mode active,,,,,no,,,Uplink to Core
27+
TenGigabitEthernet1/1/2,channel-group 1 mode active,,,,,no,,,Uplink to Core
28+
TenGigabitEthernet1/1/3,trunk,100,all,,,no,,,
29+
TenGigabitEthernet1/1/4,trunk,100,all,,,no,,,
30+
TenGigabitEthernet1/1/5,trunk,100,all,,,no,,,
31+
TenGigabitEthernet1/1/6,trunk,100,all,,,no,,,
32+
TenGigabitEthernet1/1/7,trunk,100,all,,,no,,,
33+
TenGigabitEthernet1/1/8,trunk,100,all,,,no,,,

0 commit comments

Comments
 (0)