Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, warnings, re
- from ftplib import FTP
- class Group():
- btn = str()
- class User():
- first = str()
- last = str()
- tn = str()
- authid = str()
- pw = str()
- class Device():
- mac = str()
- model = str()
- d = Device()
- u = User()
- g = Group()
- def strInput(question):
- return input(question).strip('\r')
- run = "1"
- while run != "0":
- ## This section defines all necessary variables.
- g.btn = strInput("Enter Group BTN: ")
- btnmatch = re.match('^[0-9]{10}$',g.btn)
- while btnmatch == None:
- g.btn = strInput("Invalid BTN.\nEnter Group BTN: ")
- btnmatch = re.match('^[0-9]{10}$',g.btn)
- d.model = strInput("Enter device model number (335, 450, 550, etc.): ")
- modelmatch = re.match('^301$|^320$|^330$|^331$|^335$|^430$|^450$|^4000$|^500$|^501$|^550$|^560$|^600$|^601$|^650$|^670$|^6000$|^7000$|^HDX$|^VVX$',d.model)
- while modelmatch == None:
- d.model = strInput("Invalid model number.\nEnter device model number (335, 450, 550, etc.): ")
- modelmatch = re.match('^301$|^320$|^330$|^331$|^335$|^430$|^450$|^4000$|^500$|^501$|^550$|^560$|^600$|^601$|^650$|^670$|^6000$|^7000$|^HDX$|^VVX$',d.model)
- d.mac = strInput("Enter device MAC Address: ")
- macmatch = re.match('^[0-9A-Fa-f]{12}$',d.mac)
- while macmatch == None:
- d.mac=strInput("Invalid MAC Address.\nEnter device MAC Address: ")
- macmatch = re.match('^[0-9A-Fa-f]{12}$',d.mac)
- u.first = strInput("Enter User First Name: ")
- u.last = strInput("Enter User Last Name: ")
- u.tn = strInput("Enter User TN or Extension: ")
- tnmatch = re.match('^[0-9]{2,4}$|^[0-9]{10}$', u.tn)
- while tnmatch == None:
- u.tn = strInput("Invalid TN or Extension.\nEnter User TN or Extension: ")
- tnmatch = re.match('^[0-9]{2,4}$|^[0-9]{10}$', u.tn)
- u.authid = strInput("Enter User Auth ID: ")
- u.pw = strInput("Enter User Auth Password: ")
- name = "POLY"+d.model+" "+str(d.mac.upper()[6:12])
- fnmac = d.mac.lower()
- ## Reads template file (BWDEVICE_template.cfg) to memory
- bwtf = open ("Z:/NOC/MacTool/BWDEVICE_template.cfg")
- var = bwtf.readlines()
- bwtf.close()
- ## Reads template file (template.cfg) to memory
- tf = open ("Z:/NOC/MacTool/template.cfg")
- var2 = tf.readlines()
- tf.close()
- ## Creates new file named BWDEVICE_%mac%.cfg
- bwnf = open("Z:/NOC/MacTool/BWDEVICE_"+fnmac+".cfg",'w')
- ## Writes to new file line by line, replacing variables in template file with values
- for line in range(406):
- var[line] = var[line].replace("%First%", u.first)
- var[line] = var[line].replace("%Last%", u.last)
- var[line] = var[line].replace("%Address%", u.tn)
- var[line] = var[line].replace("%Label%", u.tn)
- var[line] = var[line].replace("%type%", "private")
- var[line] = var[line].replace("%authid%", u.authid)
- var[line] = var[line].replace("%authpw%", u.pw)
- bwnf.write(var[line])
- ## Creates new file named %mac%.cfg
- nf = open("Z:/NOC/MacTool/"+fnmac+".cfg",'w')
- ## Writes to new file line by line, replacing variables in template file with values
- for line2 in range(6):
- if d.model == "320":
- d.model = "330"
- if d.model == "331":
- d.model = "330"
- if d.model == "335":
- d.model = "330"
- if d.model == "301":
- d.model = "300"
- if d.model == "501":
- d.model = "500"
- if d.model == "601":
- d.model = "600"
- var2[line2] = var2[line2].replace("%devicename%", name)
- var2[line2] = var2[line2].replace("%mac%", d.mac.lower())
- var2[line2] = var2[line2].replace("%model%", d.model)
- nf.write(var2[line2])
- bwnf.close()
- nf.close()
- ## Opens both newly created files as readable binaries
- bwf = open("Z:/NOC/MacTool/BWDEVICE_"+fnmac+".cfg",'rb')
- devf = open("Z:/NOC/MacTool/"+fnmac+".cfg",'rb')
- ## Connects to FTP
- ftp = FTP('204.11.148.130')
- ftp.debug(2)
- ftp.connect(timeout=100000)
- ftp.login(user=g.btn, passwd=g.btn)
- ## Stores both files to FTP
- ftp.storbinary('STOR BWDEVICE_'+fnmac+'.cfg', bwf)
- ftp.storbinary('STOR '+fnmac+'.cfg', devf)
- ## Closes files
- bwf.close()
- devf.close()
- ## Disconnects from FTP Server
- ftp.close()
- run = strInput("Enter 0 to quit. Any other key to run again. ")
Add Comment
Please, Sign In to add comment