Guest User

Untitled

a guest
Jun 28th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. #!/usr/bin/python3
  2. #22/06/2017 17:17
  3. import telnetlib
  4. import time
  5. import os
  6. import sys
  7. import getpass
  8. import pexpect
  9. from telnetlib import Telnet
  10. import datetime
  11. import subprocess
  12. def who_is(): # проверяем точно ли тот вендор, который мы предположили на начальном этапе.
  13. global ver_status
  14. global sab_versus
  15. global versus
  16. sab_versus=''
  17. ver_status=''
  18. versus=''
  19. a = 'Serial No.:1405' # #qtech rev1
  20. #b = 'Serial No.:3922' # #qtech rev2
  21. #c = 'Serial No.:5436' # #qtech rev2
  22. #l = 'Serial No.:16101' # #qtech rev2
  23. d = 'Command: show switch' # #d-link
  24. f = 'Raisecom Operating System Software' # #raisecom
  25. h = 'QOS' # #raisecom-qtech
  26. j = 'MES1124M' # #eltex
  27. k = 'eltex' # #eltex
  28. n = 'Build245' #qtech2.0
  29. parser=open('servers_&_log/ver.txt', 'r')
  30. for line in parser.readlines():
  31. line1 = line.find(a)
  32. #line2 = line.find(b)
  33. #line3 = line.find(c)
  34. line4 = line.find(d)
  35. line5 = line.find(f)
  36. line6 = line.find(h)
  37. line7 = line.find(j)
  38. line8 = line.find(k)
  39. #line9 = line.find(l)
  40. line10 = line.find(n)
  41. if line1 != -1:
  42. ver_status='qtech_rev_1.0'
  43. #if line2 != -1 or line3 != -1 or line9 !=-1:
  44. #ver_status='qtech_rev_2.0'
  45. if line4 != -1:
  46. ver_status='d-link'
  47. if line5 != -1:
  48. ver_status='raisecom'
  49. if line6 != -1:
  50. ver_status='raisecom-qtech'
  51. sab_versus=1
  52. if line7 !=-1:
  53. ver_status='eltex'
  54. if line8 !=-1:
  55. ver_status='eltex'
  56. if line10 != -1:
  57. ver_status = 'qtech_rev_2.0'
  58. time.sleep(0.1)
  59. parser.close()
  60. os.remove('servers_&_log/ver.txt')
  61. return ver_status,sab_versus
  62. user='user'
  63. password='password'
  64. komm=open('servers_&_log/komm.txt')
  65. log=open('servers_&_log/log.txt','a')
  66. counter_komm=0
  67. counter_dlink=0
  68. counter_qtech=0
  69. counter_eltex=0
  70. counter_raisecom=0
  71. counter_ROS=0 #гибрид
  72. counter_qtech1_0=0
  73. counter_qtech2_0=0
  74. for host in komm.readlines():
  75. print('connect....',host)
  76. vend = ''
  77. #tn = Telnet(host.replace('\n', ''), 23, 20)
  78. response = os.system('ping -c 1 ' + host)
  79. verinfo = open('servers_&_log/ver.txt', 'w')
  80. ver_status = ''
  81. sab_versus = ''
  82. if response == 0:
  83. telnet = pexpect.spawn('telnet ' + host,timeout=40)
  84. vend = telnet.expect(['login:', 'Login:', 'User Name:', 'Username'])
  85. telnet.close()
  86. tn = Telnet(host.replace('\n', ''), 23,30)
  87. try:
  88. print('Ok'+'\n')
  89. tn.read_until(b':')
  90. tn.write((user +'\n').encode('ascii'))
  91. tn.read_until(b':')
  92. tn.write((password + '\n').encode('ascii'))
  93. time.sleep(3)
  94. tn.read_until(b'#',timeout=20)
  95. except:
  96. print('connection refused' + '\n')
  97. f = open('servers_&_log/log.txt', 'a')
  98. print(host, 'connection refused', file=log)
  99. print('#' * 100, host, file=log)
  100. ###БЛОК ОПРЕДЕЛЕНИЯ ВЕНДОРА
  101. ################################################################################
  102. if vend == 0:# предположительно что это Qtech
  103. tn.write(('show ver' + '\n').encode('ascii'))
  104. print((tn.read_until(b'#').decode('ascii')), file=verinfo)
  105. verinfo.close()
  106. who_is()
  107. ################################################################################
  108. if vend == 1: #предположительно что это Raisecom,Raisecom-Qtech
  109. tn.write(('show ver' + '\n').encode('ascii'))
  110. print((tn.read_until(b'#').decode('ascii')), file=verinfo)
  111. verinfo.close()
  112. who_is()
  113. ################################################################################
  114. if vend == 2:#предположительно что это Eltex
  115. tn.write(('show system' + '\n').encode('ascii'))
  116. tn.write(('show ver' + '\n').encode('ascii'))
  117. print((tn.read_until(b'#').decode('ascii')), file=verinfo)
  118. verinfo.close()
  119. who_is()
  120. ################################################################################
  121. if vend == 3:#предположительно что это D-link
  122. tn.write(('show sw' + '\n').encode('ascii'))
  123. tn.write(('a' + '\n').encode('ascii'))
  124. print((tn.read_until(b'#').decode('ascii')), file=verinfo)
  125. verinfo.close()
  126. who_is()
  127. #
  128. ###БЛОГ КОНФИГУРИРОВАНИЯ
  129. #
  130. ################################################################################
  131. if ver_status == 'd-link':
  132. tn.read_until(b'#', timeout=20)
  133. tn.write(('show sw' + '\n').encode('ascii'))
  134. tn.write(('a' + '\n').encode('ascii'))
  135. print((tn.read_until(b'#').decode('ascii')), file=log)
  136. print('D-link ' + host,file=log)
  137. print('#'*100,file=log)
  138. counter_dlink+=1
  139. ################################################################################
  140. elif ver_status == 'qtech_rev_1.0':
  141. tn.write(('show ver' + '\n').encode('ascii'))
  142. print((tn.read_until(b'#').decode('ascii')), file=log)
  143. counter_qtech1_0+=1
  144. komand_qtech1_0=open('servers_&_log/komand_qtech_ver1.0.txt')
  145. for kommand in komand_qtech1_0.readlines():
  146. tn.write((kommand.replace('\n', '') + '\n').encode('ascii'))
  147. #print((tn.read_until(b'#').decode('ascii')), file=log)
  148. tn.write(('exit' + '\n').encode('ascii'))
  149. print(tn.read_all().decode('ascii'), file=log)
  150. print('Qtech rev1.0 ' + host, file=log)
  151. print('#' * 100, file=log)
  152. ################################################################################
  153. elif ver_status == 'qtech_rev_2.0':
  154. tn.write(('show ver' + '\n').encode('ascii'))
  155. print((tn.read_until(b'#').decode('ascii')), file=log)
  156. counter_qtech2_0+=1
  157. komand_qtech2_0=open('servers_&_log/komand_qtech_ver2.0.txt')
  158. for kommand in komand_qtech2_0.readlines():
  159. tn.write((kommand.replace('\n', '') + '\n').encode('ascii'))
  160. print((tn.read_until(b'#').decode('ascii')), file=log)
  161. time.sleep(1)
  162. print('Qtech rev2.0 ' + host, file=log)
  163. print('#' * 100, file=log)
  164. ################################################################################
  165. elif ver_status == 'raisecom-qtech':
  166. tn.write(('show ver' + '\n').encode('ascii'))
  167. print((tn.read_until(b'#').decode('ascii')), file=log)
  168. raisecom_command = open('servers_&_log/komand_raisecom.txt')
  169. for komand in raisecom_command.readlines():
  170. tn.write((komand.replace('\n', '') + '\n').encode('ascii'))
  171. print((tn.read_until(b'#').decode('ascii')), file=log)
  172. print('ROS '+ host,file=log)
  173. print('#'*100,file=log)
  174. counter_ROS+=1
  175. ################################################################################
  176. elif ver_status == 'raisecom':
  177. tn.write(('show ver' + '\n').encode('ascii'))
  178. print((tn.read_until(b'#').decode('ascii')), file=log)
  179. counter_raisecom+=1
  180. raisecom_command = open('servers_&_log/komand_raisecom.txt')
  181. for komand in raisecom_command.readlines():
  182. tn.write((komand.replace('\n', '') + '\n').encode('ascii'))
  183. print((tn.read_until(b'#').decode('ascii')), file=log)
  184. print(host, ':', 'RAISECOM', file=log)
  185. print('#' * 100, host, file=log)
  186. ################################################################################
  187. elif ver_status == 'eltex':
  188. tn.write(('show ver' + '\n').encode('ascii'))
  189. print((tn.read_until(b'#').decode('ascii')), file=log)
  190. eltex_command=open('servers_&_log/komand_eltex.txt')
  191. for komand in eltex_command.readlines():
  192. tn.write((komand.replace('\n', '') + '\n').encode('ascii'))
  193. print((tn.read_until(b'#').decode('ascii')), file=log)
  194. print('Eltex ' + host, file=log)
  195. print('#' * 100, file=log)
  196. counter_eltex+=1
  197. else:
  198. print('no ping')
  199. counter_komm+=1
  200. print('\n\n\nИтого обработано: ', counter_komm,file=log)
  201. print('\n\nD-link:', counter_dlink,'\nQtech ver1.0:', counter_qtech1_0,'\nROS:', counter_ROS,'\nRaisecom:',counter_raisecom,'\nEltex:', counter_eltex,'\nQtech ver2.0 :', counter_qtech2_0,file=log)
  202. print('\nДата: ', datetime.datetime.now().isoformat(),'\n', '#'*100,'\n\n\n\n\n',file=log)
  203. verinfo.close()
  204. log.close()
Add Comment
Please, Sign In to add comment