Advertisement
Guest User

Untitled

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