Guest User

Untitled

a guest
Jan 22nd, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. import sys, warnings, re
  2. from ftplib import FTP
  3.  
  4. class Group():
  5. btn = str()
  6.  
  7. class User():
  8. first = str()
  9. last = str()
  10. tn = str()
  11. authid = str()
  12. pw = str()
  13.  
  14. class Device():
  15. mac = str()
  16. model = str()
  17.  
  18.  
  19.  
  20. d = Device()
  21. u = User()
  22. g = Group()
  23.  
  24. def strInput(question):
  25. return input(question).strip('\r')
  26.  
  27. run = "1"
  28. while run != "0":
  29.  
  30. ## This section defines all necessary variables.
  31. g.btn = strInput("Enter Group BTN: ")
  32. btnmatch = re.match('^[0-9]{10}$',g.btn)
  33. while btnmatch == None:
  34. g.btn = strInput("Invalid BTN.\nEnter Group BTN: ")
  35. btnmatch = re.match('^[0-9]{10}$',g.btn)
  36. d.model = strInput("Enter device model number (335, 450, 550, etc.): ")
  37. 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)
  38. while modelmatch == None:
  39. d.model = strInput("Invalid model number.\nEnter device model number (335, 450, 550, etc.): ")
  40. 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)
  41. d.mac = strInput("Enter device MAC Address: ")
  42. macmatch = re.match('^[0-9A-Fa-f]{12}$',d.mac)
  43. while macmatch == None:
  44. d.mac=strInput("Invalid MAC Address.\nEnter device MAC Address: ")
  45. macmatch = re.match('^[0-9A-Fa-f]{12}$',d.mac)
  46. u.first = strInput("Enter User First Name: ")
  47. u.last = strInput("Enter User Last Name: ")
  48. u.tn = strInput("Enter User TN or Extension: ")
  49. tnmatch = re.match('^[0-9]{2,4}$|^[0-9]{10}$', u.tn)
  50. while tnmatch == None:
  51. u.tn = strInput("Invalid TN or Extension.\nEnter User TN or Extension: ")
  52. tnmatch = re.match('^[0-9]{2,4}$|^[0-9]{10}$', u.tn)
  53. u.authid = strInput("Enter User Auth ID: ")
  54. u.pw = strInput("Enter User Auth Password: ")
  55. name = "POLY"+d.model+" "+str(d.mac.upper()[6:12])
  56.  
  57. fnmac = d.mac.lower()
  58.  
  59. ## Reads template file (BWDEVICE_template.cfg) to memory
  60. bwtf = open ("Z:/NOC/MacTool/BWDEVICE_template.cfg")
  61. var = bwtf.readlines()
  62. bwtf.close()
  63.  
  64. ## Reads template file (template.cfg) to memory
  65. tf = open ("Z:/NOC/MacTool/template.cfg")
  66. var2 = tf.readlines()
  67. tf.close()
  68.  
  69. ## Creates new file named BWDEVICE_%mac%.cfg
  70. bwnf = open("Z:/NOC/MacTool/BWDEVICE_"+fnmac+".cfg",'w')
  71.  
  72. ## Writes to new file line by line, replacing variables in template file with values
  73. for line in range(406):
  74. var[line] = var[line].replace("%First%", u.first)
  75. var[line] = var[line].replace("%Last%", u.last)
  76. var[line] = var[line].replace("%Address%", u.tn)
  77. var[line] = var[line].replace("%Label%", u.tn)
  78. var[line] = var[line].replace("%type%", "private")
  79. var[line] = var[line].replace("%authid%", u.authid)
  80. var[line] = var[line].replace("%authpw%", u.pw)
  81. bwnf.write(var[line])
  82.  
  83. ## Creates new file named %mac%.cfg
  84. nf = open("Z:/NOC/MacTool/"+fnmac+".cfg",'w')
  85.  
  86. ## Writes to new file line by line, replacing variables in template file with values
  87. for line2 in range(6):
  88. if d.model == "320":
  89. d.model = "330"
  90. if d.model == "331":
  91. d.model = "330"
  92. if d.model == "335":
  93. d.model = "330"
  94. if d.model == "301":
  95. d.model = "300"
  96. if d.model == "501":
  97. d.model = "500"
  98. if d.model == "601":
  99. d.model = "600"
  100. var2[line2] = var2[line2].replace("%devicename%", name)
  101. var2[line2] = var2[line2].replace("%mac%", d.mac.lower())
  102. var2[line2] = var2[line2].replace("%model%", d.model)
  103. nf.write(var2[line2])
  104.  
  105. bwnf.close()
  106. nf.close()
  107.  
  108.  
  109. ## Opens both newly created files as readable binaries
  110. bwf = open("Z:/NOC/MacTool/BWDEVICE_"+fnmac+".cfg",'rb')
  111. devf = open("Z:/NOC/MacTool/"+fnmac+".cfg",'rb')
  112.  
  113. ## Connects to FTP
  114. ftp = FTP('204.11.148.130')
  115. ftp.debug(2)
  116. ftp.connect(timeout=100000)
  117. ftp.login(user=g.btn, passwd=g.btn)
  118.  
  119. ## Stores both files to FTP
  120. ftp.storbinary('STOR BWDEVICE_'+fnmac+'.cfg', bwf)
  121. ftp.storbinary('STOR '+fnmac+'.cfg', devf)
  122.  
  123. ## Closes files
  124. bwf.close()
  125. devf.close()
  126.  
  127. ## Disconnects from FTP Server
  128. ftp.close()
  129.  
  130.  
  131. run = strInput("Enter 0 to quit. Any other key to run again. ")
Add Comment
Please, Sign In to add comment