Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1.  
  2. def pega_string_procura_onu(host,user,senha):
  3.  
  4. tn = telnetlib.Telnet(host)
  5.  
  6. while True:
  7. if "name" in tn.read_very_eager().decode('ascii'):
  8. break
  9.  
  10. tn.write(user.encode('ascii') + b"\n")
  11.  
  12.  
  13. while True:
  14. if "password" in tn.read_very_eager().decode('ascii'):
  15. break
  16.  
  17.  
  18. tn.write(senha.encode('ascii') + b"\n")
  19.  
  20.  
  21. sleep(0.25)
  22.  
  23. tn.write("enable".encode('ascii') + b"\n")
  24.  
  25. sleep(0.25)
  26.  
  27. tn.write("config".encode('ascii') + b"\n")
  28.  
  29. sleep(0.25)
  30.  
  31. tn.write("scroll 512".encode('ascii') + b"\n")
  32.  
  33. sleep(0.25)
  34.  
  35.  
  36. tn.write(("display ont autofind all").encode('ascii') + b"\n")
  37.  
  38. tn.read_until(("display ont autofind all").encode('ascii'))
  39.  
  40. string_procura = tn.read_until(("(config)#").encode('ascii'),timeout=2).decode('ascii')
  41.  
  42. return(string_procura)
  43.  
  44. def procura_onu(update, context):
  45. print("procura_onu")
  46.  
  47. try:
  48. num_olt = float(context.args[0])
  49. print(num_olt)
  50. except:
  51. if not context.args:
  52. update.message.reply_text("<b>Parâmetro necessário.</b>\n\nUso: /procura [número], sendo [número] a OLT a ser consultada.\nEx: /procura 1 mostrará as ONUs disponíveis para serem provisionadas na OLT 1 (POP 04)",parse_mode='HTML')
  53. else:
  54. update.message.reply_text("<b>O parâmetro deve ser um número.</b>\n\nUso: /procura [número], sendo [número] a OLT a ser consultada.\nEx: /procura 1 mostrará as ONUs disponíveis para serem provisionadas na OLT 1 (POP 04)",parse_mode='HTML')
  55.  
  56. user = "bot_monitora"
  57. senha = "q21395po"
  58.  
  59. if num_olt == 1:
  60. host = "172.20.1.2"
  61.  
  62. elif num_olt == 2:
  63. host = "172.20.1.6"
  64.  
  65. elif num_olt == 3:
  66. host = "172.20.1.10"
  67.  
  68. elif num_olt == 4:
  69. host = "172.20.1.14"
  70.  
  71. else:
  72. update.message.reply_text("<b>OLT não localizada!</b>\n\nVerifique e tente novamente.",parse_mode='HTML')
  73. return
  74.  
  75. string_procura = pega_string_procura_onu(host,user,senha)
  76. string_loop = string_procura
  77. string_reply = ""
  78. if "Failure: The automatically found ONTs do not exist" in string_procura:
  79. string_reply = "Não há ONUs para serem provisionadas \U0001F641"
  80. else:
  81. while "Number : " in string_loop:
  82. numero = string_loop[string_loop.index('Number : ')+22:string_loop.index('Number : ')+23]
  83. slot = string_loop[string_loop.index('F/S/P : ')+24:string_loop.index('F/S/P : ')+25]
  84. pon = string_loop[string_loop.index('F/S/P : ')+26:string_loop.index('F/S/P : ')+27]
  85. vlan = slot + pon
  86. if len(vlan) < 3:
  87. vlan = vlan[0] + "0" + vlan[1]
  88. sn = string_loop[string_loop.index('Ont SN : ')+22:string_loop.index('Ont SN : ')+38]
  89. modelo_onu = string_loop[string_loop.index('Ont EquipmentID : ')+22:string_loop.index('Ont EquipmentID : ')+26]
  90.  
  91. string_reply += "<b>ONU encontrada!\n</b>Número: %s\nSlot: %s\nPon: %s\nVlan: %s\nSerial: %s\nModelo ONU: %s\n"%(numero,slot,pon,vlan,sn,modelo_onu)
  92. string_loop = string_loop[string_loop.index('Ont EquipmentID : '):]
  93.  
  94. update.message.reply_text(string_reply,parse_mode='HTML')
  95.  
  96.  
  97. def provisiona(update, context):
  98.  
  99.  
  100. print("provisiona")
  101.  
  102. try:
  103. sn = context.args[1]
  104. slot = context.args[2]
  105. pon = context.args[3]
  106. desc = context.args[4]
  107.  
  108. slot_line = slot
  109. pon_line = pon
  110.  
  111. if len(slot_line) < 2:
  112. slot_line = "0" + slot
  113. if len(pon_line) < 2:
  114. pon_line = "0" + pon
  115.  
  116. lineprofile = "SLOT-%s_PON-%s"%(slot_line,pon_line)
  117. srvprofile = "FTTH"
  118. in_traffic_table = "INTERNET"
  119. out_traffic_table = "INTERNET"
  120.  
  121. num_olt = float(context.args[0])
  122. print(num_olt)
  123. except:
  124. if not context.args:
  125. update.message.reply_text("<b>Parâmetros necessários.</b>\n\nUso: /provisiona [olt] [serial] [slot] [pon] [descricao]",parse_mode='HTML')
  126. else:
  127. update.message.reply_text("<b>Parâmetros inválidos.</b>\n\nUso: /provisiona [olt] [serial] [slot] [pon] [descricao]",parse_mode='HTML')
  128.  
  129. user = "bot_monitora"
  130. senha = "q21395po"
  131.  
  132. if num_olt == 1:
  133. host = "172.20.1.2"
  134.  
  135. elif num_olt == 2:
  136. host = "172.20.1.6"
  137.  
  138. elif num_olt == 3:
  139. host = "172.20.1.10"
  140.  
  141. elif num_olt == 4:
  142. host = "172.20.1.14"
  143.  
  144. else:
  145. update.message.reply_text("<b>OLT não localizada!</b>\n\nVerifique e tente novamente.",parse_mode='HTML')
  146. return
  147.  
  148.  
  149. tn = telnetlib.Telnet(host)
  150.  
  151. while True:
  152. if "name" in tn.read_very_eager().decode('ascii'):
  153. break
  154.  
  155. tn.write(user.encode('ascii') + b"\n")
  156.  
  157.  
  158. while True:
  159. if "password" in tn.read_very_eager().decode('ascii'):
  160. break
  161.  
  162.  
  163. tn.write(senha.encode('ascii') + b"\n")
  164.  
  165.  
  166. sleep(0.25)
  167.  
  168. tn.write("enable".encode('ascii') + b"\n")
  169.  
  170. sleep(0.25)
  171.  
  172. tn.write("config".encode('ascii') + b"\n")
  173.  
  174. sleep(0.25)
  175.  
  176. tn.write("scroll 512".encode('ascii') + b"\n")
  177.  
  178. sleep(0.25)
  179.  
  180. print("provisiona onu")
  181.  
  182. vlan = slot
  183. if len(pon) < 2:
  184. vlan += "0" + pon
  185. else:
  186. vlan += pon
  187. a1 = "interface gpon 0/%s"%(slot)
  188. a = "(config-if-gpon-0/%s)#"%(slot)
  189.  
  190.  
  191. tn.write(a1.encode('ascii') + b"\n")
  192. tn.read_until(a.encode('ascii'),timeout=2)
  193.  
  194.  
  195.  
  196. b = "ont add %s sn-auth %s omci ont-lineprofile-name %s ont-srvprofile-name %s desc %s"%(pon,sn,lineprofile,srvprofile,desc)
  197. b2 = "desc %s"%(desc)
  198. tn.write(b.encode('ascii') + b"\n")
  199. tn.read_until(b2.encode('ascii'),timeout=2).decode('ascii')
  200. result = tn.read_until(a.encode('ascii'),timeout=2).decode('ascii')
  201. ontid = ""
  202. #print(result)
  203.  
  204. if "Failure: SN already exists" in result:
  205. update.message.reply_text("O serial já existe na OLT! Exclua a ONU antiga antes de adicionar esta!",parse_mode='HTML')
  206. return
  207. elif "Failure: The number of ONT in port already reach upper limit" in result:
  208. update.message.reply_text("A PON está cheia! Exclua alguma ONU sem uso para adicionar a próxima!",parse_mode='HTML')
  209. return
  210. else:
  211. if "ONTID" in result:
  212. ontid = result[result.index("ONTID :")+7:result.index("ONTID :")+10]
  213. ontid = re.sub(r'\W','',ontid) #remove espacos e quebra de linha
  214. print(ontid)
  215. update.message.reply_text("<b>ONU adicionada com sucesso!</b>\n\nVlan: %s\nID: %s\nSerial: %s\nDescrição: %s"%(vlan,ontid,sn,desc),parse_mode='HTML')
  216. else:
  217. update.message.reply_text("Ocorreu um erro! Tente novamente",parse_mode='HTML')
  218. return
  219.  
  220. tn.write("quit".encode('ascii') + b"\n")
  221. tn.read_until("(config)#".encode('ascii'),timeout=2)
  222.  
  223. c = "service-port vlan %s gpon 0/%s/%s ont %s gemport %s multi-service user-vlan %s inbound traffic-table name %s outbound traffic-table name %s"%(vlan,slot,pon,ontid,vlan,vlan,in_traffic_table,out_traffic_table)
  224.  
  225. tn.write(c.encode('ascii') + b"\n")
  226. tn.read_until(c.encode('ascii'),timeout=2)
  227.  
  228. result_associa = tn.read_until(a.encode('ascii'),timeout=2).decode('ascii')
  229.  
  230. if "Failure" in result_associa or "Parameter error" in result_associa:
  231. update.message.reply_text("Ocorreu um erro ao associar :(",parse_mode='HTML')
  232.  
  233. else:
  234. update.message.reply_text("<b>ONU associada com sucesso!</b>",parse_mode='HTML')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement