Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. """ Прошиватель v3. """
  2.  
  3. import os
  4. import sys
  5. import multiprocessing
  6.  
  7. from telnetlib import Telnet
  8. from datetime import time
  9.  
  10. """ Определяем глобальные переменные. """
  11.  
  12. USER = 'admin'
  13. PASSWROD = 'admin'
  14.  
  15.  
  16. class Helper():
  17.     pass
  18.  
  19.  
  20. class ConnectionMaker():
  21.     """ Создаем телнет соединения"""
  22.  
  23.  
  24.     def __init__(self):
  25.         """ Конструктор """
  26.         pass
  27.  
  28.  
  29.     def get_switches():
  30.         """ Получаем список прошиваемых свитчей """
  31.         switches = []
  32.         with open('switches.txt', 'r') as f:
  33.             for switch in f:
  34.                 switches.append(switch.rstrip())
  35.         return switches
  36.  
  37.  
  38.     def create_connection(switches, port=23):
  39.     """ Создаем телнет соединения. """
  40.  
  41.         connections = multiprocessing.Queue()
  42.         tn = Telnet()
  43.  
  44.         tn.open(ip, port)
  45.         tn.read_until(b'login:')
  46.         tn.write(USER.encode('ascii') + b'\n')
  47.         tn.read_until(b'Password:')
  48.         tn.write(PASSWORD.encode('ascii') + b'\n')
  49.         buff = tn.read_until(b'QSW-2850-28T-AC#')
  50.         if b'QSW-2850-28T-AC#' in buff:
  51.             print ('Connection established')
  52.         else:
  53.             print ('ХУЙ')
  54.  
  55.         connections.put(tn)
  56.         #print (connections) #для дебага
  57.  
  58.         return connections
  59.  
  60.  
  61.  
  62.  
  63. class Downloader():
  64.     """ Загрузчик прошивок. """
  65.  
  66.  
  67.     def __init__(self):
  68.         """ Конструктор. """
  69.         self.connections = connections
  70.  
  71.  
  72.     def bootrom_downloader(self.connections):
  73.     """ Меняем загрузчик коммутатора. """
  74.  
  75.  
  76.         connection.write(b'fcopy tftp://10.111.31.11/QSW-2850-28(52)T_2011.12.12_boot.rom boot.rom' + b'\n')
  77.         buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
  78.         print(buff.decode('ascii'))
  79.         if b'[Y/N]:' in buff: connection.write(b'y' + b'\n')
  80.        
  81.         while True:
  82.             buff = connection.read_until(b'QSW-2850-28T-AC#', 3)
  83.             print (buff.decode('ascii'))           
  84.             if b'QSW-2850-28T-AC#' in buff:
  85.                 print('boot rom downloaded succesfully')
  86.                 break
  87.             else:
  88.                 print('download in progress...')
  89.                 continue
  90.  
  91.  
  92.  
  93.     def downloader_for_7035 (self.connections):
  94.     """Загружаем промежуточную прошивку """
  95.  
  96.    
  97.         connection.write(b'fcopy tftp://10.111.31.11/QSW-X-For-Upgrade_7.0.3.5(R0000.0000)_nos.img QSW-X-For-Upgrade_7.0.3.5(R0000.0000)_nos.img' + b'\n')
  98.    
  99.         while True:
  100.             buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
  101.             if b'QSW-2850-28T-AC#' in buff:
  102.                 connection.write(b'boot img QSW-X-For-Upgrade_7.0.3.5(R0000.0000)_nos.img primary' + b'\n')
  103.                 connection.write(b'reload' + b'\n')
  104.                 confirm = connection.read_until(b'QSW-2850-28T-AC#', 5)
  105.                 if b'Process with reboot? [Y/N] ' in confirm:
  106.                     connection.write(b'y' + b'\n')
  107.                     print ('done, reloading device...')
  108.                     break
  109.                 else:
  110.                     print(buff.decode('ascii'))
  111.                     print('firmware isnt download, process continue...')
  112.                     continue
  113.  
  114.  
  115.  
  116.     def nos_deleter (self.connections):
  117.     """ Удаляем дефолтную прошивку"""
  118.    
  119.  
  120.         connection.write(b'delete nos.img' + b'\n')
  121.         buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
  122.         if b'Delete file, Are you sure? (Y/N)?[N]' in buff: connection.write(b'y' + b'\n')
  123.         buff1 = connection.read_until(b'QSW-2850-28T-AC#', 5)
  124.         if b'QSW-2850-28T-AC#' in buff1: print ('nos.img deleted succesfully')
  125.  
  126.  
  127.     def downloader_for_821 (self.connection):
  128.     """ Загружаем концептуальную прошивку """
  129.  
  130.  
  131.         connection.write(b'copy tftp://10.111.31.11/QSW-2850(3470)-X_8.2.1.54_nos.img nos.img' + b'\n')
  132.  
  133.         while True:
  134.             buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
  135.             if b'QSW-2850-28T-AC#' in buff:            
  136.                 connection.write(b'boot img nos.img primary' + b'\n')
  137.                 connection.read_until(b'QSW-2850-28T-AC#', 1)              
  138.                 connection.write(b'reload' + b'\n')
  139.                 confirm = connection.read_until(b'QSW-2850-28T-AC#', 5)
  140.                 if b'Process with reboot? [Y/N]' in confirm:
  141.                     connection.write(b'y' + b'\n')
  142.                     print ('done, reloading device...')
  143.                     break
  144.                 else:
  145.                     print(buff.decode('ascii'))
  146.                     print('firmware isnt download, process continue...')
  147.                     continue
  148.  
  149.  
  150. if __name__ == '__main__':
  151.     switches = ConnectionMaker.get_switches()
  152.  
  153.     for switch in switches:
  154.         connections = ConnectionMaker.create_connection(switches)
  155.  
  156.     """ Спавним пул воркер процессов """
  157.  
  158.     with multiprocessing.Pool(connections.qsize()) as p:
  159.         p.map(Downloader.bootrom_downloader, connections)
  160.         p.map(Downloader.downloader_for_7035, connections)
  161.         time.sleep(60)
  162.         p.map(Downloader.nos_deleter, connections)
  163.         p.map(Downloader.downloader_for_821, connections)
  164.         p.close()
  165.         p.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement