The script should be a single python file that takes an excel file (or csv). Each row represents one switch. For each switch, there will be one or more interfaces. The objective is to output a script of commands to execute to configure each switch, as specified.
For example, Row 1 is below:
switch1 Eth1 trunk 10,20,30 Eth2 Access 10 Y
The script should see that there are two interfaces. For each trunk interface, the script should output the command “swithport mode trunk”, then “switchport trunk allow vlan …” with the list of allowed VLANs. For each access interface, it’s the same, but with the word “access” instead of “trunk”.
The sample output for the above row is as below;
switch1
hostname switch1
!
interface Eth1
switchport mode trunk
switchport trunk allow vlan 10,20,30
!
interface Eth2
switchport mode access
switchport access vlan 10
Each switch’s output should be written as a separate column in a new csv or excel file.
My submitted code as below, please review and guide me for improvements;
#!/usr/bin/python
import csv
import sys
import re
import argparse
def parseCSV(path):
'''
Parses a CSV file and return the rows as a list
'''
rows = []
try:
f = path
except IOError:
print "Cannot open specified file: %s" % path
else:
reader = csv.reader(f)
for row in reader:
rows.append(row)
f.close()
return rows
def outputCSV(destpath, rows):
'''
Process the input rows and provide switch command output.
Output then processed to destination CSV file.
'''
try:
f = destpath
except IOError:
print "Cannot open specified file: %s" % destpath
else:
writer = csv.writer(f)
for i in range(len(rows)):
new_list = []
final_string = ''
if re.match(r'^switch\d$', rows[i][0]):
new_list.append(rows[i][0])
new_list.append('')
new_list.append('hostname '+rows[i][0])
new_list.append('!')
if rows[i][1] is not None:
new_list.append('interface '+rows[i][1].lower())
new_list.append(' switchport mode '+rows[i][2])
new_list.append(' switchport '+rows[i][2]+' allow vlan '+rows[i][3])
new_list.append('!')
if rows[i][4] is not None:
new_list.append('interface '+rows[i][4].lower())
new_list.append(' switchport mode '+rows[i][5].lower())
new_list.append(' switchport '+rows[i][5].lower()+' vlan '+rows[i][6])
final_string = '\n'.join(new_list)
writer.writerow([final_string])
f.close()
def main(args):
'''
Parses the command line arguments
Arguments are:
arg1: Input CSV file to process
arg2: Output CSV file
Sample program execution: python myfile.py arg1 arg2
'''
parser = argparse.ArgumentParser(description="Reads CSV input and provide switch commands in CSV output file")
parser.add_argument('path', help="CSV file to process", type=argparse.FileType('rt'))
parser.add_argument('destpath', help="Output CSV file", type=argparse.FileType('at'))
try:
args = parser.parse_args()
except IOError, msg:
parser.error(str(msg))
return
rows = parseCSV(args.path)
outputCSV(args.destpath, rows)
if __name__ == '__main__':
args = sys.argv[1:]
main(args)