Advertisement
jimmytbud

Untitled

Jan 4th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. import os
  2. import paramiko
  3. import time
  4. import re
  5. import xlsxwriter
  6. import xlrd
  7.  
  8.  
  9.  
  10.  
  11.  
  12. def cmd (command):
  13. cmdoutput = ""
  14. ssh.send(command + "\n")
  15. time.sleep(1)
  16. cmdoutput = ssh.recv(5000)
  17. return cmdoutput
  18.  
  19.  
  20. #a text file where the show output will go
  21. output_file = open(r'X:\\MC40\output.txt', 'w')
  22.  
  23.  
  24.  
  25. # Opens the spreadsheet for input
  26. workbook = xlrd.open_workbook(r"X:\MC40\deviceinfo.xls")
  27.  
  28. firsttab = workbook.sheet_by_index(0)
  29.  
  30.  
  31. # removes the header row fro being counted
  32. xlsrows = firsttab.nrows - 1
  33.  
  34.  
  35. storelist = []
  36. storeiplist = []
  37.  
  38. while input != "":
  39.  
  40. ### Begin - part where we turn a store number into an IP --------
  41. #Get the store number from the user, and pad it with zeros
  42. input = input ("Enter a number: ")
  43. formatinput = input.zfill(4)
  44.  
  45. #pull the individual digits from the four digit number
  46. st_dig_0 = formatinput[0:1]
  47. st_dig_1 = formatinput[1:2]
  48. st_dig_2 = formatinput[2:3]
  49. st_dig_3 = formatinput[3:4]
  50.  
  51. #create the second and third octets
  52. int_st_dig_1 = int(formatinput[0:2])
  53. int_st_dig_2 = int(formatinput[2:4])
  54.  
  55. #define variables that will make up and IP address
  56. oc0 = "10"
  57. oc1 = str(int_st_dig_1)
  58. oc2 = str(int_st_dig_2)
  59. oc3 = "0"
  60. dot = "."
  61. rtr = "160"
  62.  
  63. # makes a list out of all of the compoents that make up an IP
  64. ip_addr_base = [oc0, oc1, oc2, rtr]
  65.  
  66. # joins the list together with dots to make a proper IP
  67. dev_ip = dot.join(ip_addr_base)
  68.  
  69. #append the store number to the store number list
  70. storelist.append(formatinput)
  71.  
  72. #append the store IP to the store IP list
  73. storeiplist.append(dev_ip)
  74. print("The IP address for Store#" + input + " is " + dev_ip)
  75. print("\n")
  76. ### END - part where we turn a store number into an IP --------
  77.  
  78.  
  79.  
  80. #creates a counter to keep the store list and the ip list in sync
  81. stcounter = 0
  82.  
  83. #Loop that iterates through each store IP that was generated
  84. for st in storelist:
  85. print("-------------------------------CURRENTLY WORKING ON STORE# " + st)
  86.  
  87. #the ierator for crawling the spreadsheet
  88. xlscounter = 2
  89.  
  90.  
  91. #create a list that will be populated with the mac addresses of the MC40s
  92. maclist = []
  93.  
  94. #this is the loop that runs through the spreadsheet
  95. while xlscounter <= xlsrows:
  96. #get the mac address
  97. devicemac = firsttab.cell_value(xlscounter,13)
  98. formattedmac = devicemac[:4] + "." + devicemac[4:8] + "." + devicemac[8:]
  99.  
  100. ### BEGIN - part where we turn the IP into a store number--------
  101. #grab the IP addres
  102. ipaddr = firsttab.cell_value(xlscounter,10)
  103. #split the ip based on the periods
  104. octets = ipaddr.split(".")
  105.  
  106. #convert the second and third octets into two digit numbers
  107. dub_secondoctet = octets[1].zfill(2)
  108. dub_thirdoctet = octets[2].zfill(2)
  109.  
  110. #access the second and third octets, convert them to integers
  111. secondoctet = int(dub_secondoctet)
  112. thirdoctet = int(dub_thirdoctet)
  113.  
  114. #convert the integer into a string so we can test
  115. thirdoctet = str(thirdoctet)
  116.  
  117. #if the third octet is three digits, only use the last two digits
  118. if len(thirdoctet) ==3:
  119. thirdoctet = thirdoctet[1:3]
  120. #if the third octet is 1 digis, prepend a zero
  121. elif len(thirdoctet) ==1:
  122. thirdoctet = thirdoctet.zfill(2)
  123. #anything else should be 2 digits... fine
  124. else:
  125. thirdoctet = thirdoctet
  126.  
  127. #add then together to get the store number
  128. storenum = str(secondoctet) + thirdoctet
  129. #make the store number four digits
  130. four_store = storenum.zfill(4)
  131. ### END - part where we turn the IP into a store number--------
  132.  
  133. output_file.write("2nd = ")
  134. output_file.write(str(dub_secondoctet))
  135. output_file.write("\n")
  136. output_file.write("3rd = ")
  137. output_file.write(str(dub_thirdoctet))
  138. output_file.write("\n")
  139. output_file.write(ipaddr)
  140. output_file.write(" = ")
  141. output_file.write(four_store)
  142. output_file.write("\n")
  143. output_file.write("\n")
  144.  
  145.  
  146. #check to see if the store number we pulled from the spreadsheet matches what the user entered
  147. if four_store == st:
  148. maclist.append(formattedmac)
  149.  
  150. xlscounter += 1
  151.  
  152.  
  153.  
  154. # show the user the list of mac addresses
  155.  
  156. print("\n")
  157. print("*" * 100)
  158. print("*" * 100)
  159. print("These are *** " + str(len(maclist)) + " *** the MACs that SOTI has for Store#" + st)
  160. print(maclist)
  161. print("*" * 100)
  162. print("*" * 100)
  163. print("\n")
  164.  
  165.  
  166. #Quit the program if there are no mac addresses found for the store number that you entered
  167. if str(len(maclist)) == "0":
  168. print("\n")
  169. print("*" * 100)
  170. print("THERE ARE NO MC40s REGISTERED TO THIS STORE - I QUIT")
  171. print("*" * 100)
  172. print("\n")
  173. print()
  174. quit()
  175.  
  176.  
  177.  
  178.  
  179. ###this is wher we connect and do stuff
  180. ip_addr = storeiplist[stcounter]
  181. #ip_addr = "10.18.62.160"
  182. username = 'admin'
  183. password = 'farkfark'
  184.  
  185.  
  186.  
  187. # create the SSH client
  188. ssh_pre = paramiko.SSHClient()
  189.  
  190. # add the server's keys automatically
  191. ssh_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  192.  
  193. # connects to the server
  194. ssh_pre.connect(ip_addr, username=username, password=password)
  195.  
  196. #invoke a shell
  197. ssh = ssh_pre.invoke_shell(term='vt100', width=80, height=24, width_pixels=0, height_pixels=0)
  198.  
  199. #turn off the pager so that the output is not broken apart
  200. ssh.send("term len 0\n")
  201. time.sleep(0.75)
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. ###----------get details about the voice register pool
  213. #the register pool table
  214. regpoolbr = cmd("sh voice register pool all br")
  215. print(regpoolbr)
  216. print("\n")
  217.  
  218. #number of clients in register pools
  219. regpoolcount = cmd("sh voice register pool all br | count REGISTER")
  220. #print regpoolcount
  221. #print "\n"
  222.  
  223. #find the number returned by the show command with count
  224. vpreg = "Number of lines which match regexp = (.+)\r"
  225. vp = re.findall(vpreg, regpoolcount)
  226. vpcount = int(vp[0])
  227. print("Number of Register Pools configured for this store: " + str(vpcount))
  228. #print vpcount
  229. print("\n")
  230.  
  231.  
  232.  
  233.  
  234. ###----------get details about the voice register dns
  235. #number of clients in register dns
  236. regdncount = cmd("sh run | count voice register dn")
  237. #print regdncount
  238. #print "\n"
  239.  
  240.  
  241. #find the number returned by the show command with count
  242. dnreg = "Number of lines which match regexp = (.+)\r"
  243. dn = re.findall(dnreg, regdncount)
  244. dncount = int(dn[0])
  245. #print "Number of Register DNs configured for this store: " + str(dncount)
  246. #print dncount
  247. #print "\n"
  248.  
  249.  
  250.  
  251.  
  252. ###
  253. ###enter global config mode
  254. ###
  255. cmd("conf t")
  256.  
  257.  
  258.  
  259.  
  260. #delete as many voice register pools as were found in the router
  261. counter = 1
  262. while counter <= vpcount:
  263. cmd("no voice register pool " + str(counter))
  264. print("no voice register pool " + str(counter))
  265. counter += 1
  266.  
  267.  
  268.  
  269. #delete as many voice register dns as were found in the router
  270. counter = 1
  271. while counter <= dncount:
  272. cmd("no voice register dn " + str(counter))
  273. print("no voice register dn " + str(counter))
  274. counter += 1
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. #generate the code based on the macs in the list
  282. iterator = 1
  283. for m in maclist:
  284. iteratorx5 = iterator + 500
  285.  
  286. cmd("voice register dn " + str(iterator))
  287. print("voice register dn " + str(iterator))
  288. cmd("number " + str(iteratorx5))
  289. print("number " + str(iteratorx5))
  290. cmd("allow watch")
  291. print("allow watch")
  292. cmd("name MC40 " + str(iterator))
  293. print("name MC40 " + str(iterator))
  294. cmd("label " + str(iteratorx5))
  295. print("label " + str(iteratorx5))
  296.  
  297. cmd("voice register pool " + str(iterator))
  298. print("voice register pool " + str(iterator))
  299. cmd("busy-trigger-per-button 1")
  300. print("busy-trigger-per-button 1")
  301. cmd("id mac " + m)
  302. print("id mac " + m)
  303. cmd("type 9971")
  304. print("type 9971")
  305. cmd("number 1 dn " + str(iterator))
  306. print("number 1 dn " + str(iterator))
  307. cmd("dtmf-relay sip-notify")
  308. print("dtmf-relay sip-notify")
  309. cmd("username " + str(iteratorx5) + " password " + str(iteratorx5))
  310. print("username " + str(iteratorx5) + " password " + str(iteratorx5))
  311. cmd("no call-waiting")
  312. print("no call-waiting")
  313. cmd("codec g711ulaw")
  314. print("codec g711ulaw")
  315. iterator += 1
  316.  
  317. print("\n")
  318. print("*" * 80)
  319. print("Creating Vocie Register profiles")
  320. print("*" * 80)
  321. print("\n")
  322. cmd("voice register global")
  323. cmd("create profile")
  324. cmd("end")
  325. print("\n")
  326. print("*" * 80)
  327. print("Saving Configuration")
  328. print("*" * 80)
  329. print("\n")
  330. cmd("wr")
  331. cmd("")
  332.  
  333. #the register pool table
  334. regpoolbr = cmd("sh voice register pool all br")
  335. print(regpoolbr)
  336.  
  337. #number of clients in register pools
  338. regpoolcount = cmd("sh voice register pool all br | count REGISTER")
  339. print(regpoolcount)
  340.  
  341. #Put everything back and get out
  342. ssh.send("term len 24\n")
  343. time.sleep(0.75)
  344. ssh.close()
  345.  
  346.  
  347.  
  348.  
  349. #increment the coutner, moving on to the next store in the list
  350. stcounter +=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement