Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Прошиватель v3. """
- import os
- import sys
- import multiprocessing
- from telnetlib import Telnet
- from datetime import time
- """ Определяем глобальные переменные. """
- USER = 'admin'
- PASSWROD = 'admin'
- class Helper():
- pass
- class ConnectionMaker():
- """ Создаем телнет соединения"""
- def __init__(self):
- """ Конструктор """
- pass
- def get_switches():
- """ Получаем список прошиваемых свитчей """
- switches = []
- with open('switches.txt', 'r') as f:
- for switch in f:
- switches.append(switch.rstrip())
- return switches
- def create_connection(switches, port=23):
- """ Создаем телнет соединения. """
- connections = multiprocessing.Queue()
- tn = Telnet()
- tn.open(ip, port)
- tn.read_until(b'login:')
- tn.write(USER.encode('ascii') + b'\n')
- tn.read_until(b'Password:')
- tn.write(PASSWORD.encode('ascii') + b'\n')
- buff = tn.read_until(b'QSW-2850-28T-AC#')
- if b'QSW-2850-28T-AC#' in buff:
- print ('Connection established')
- else:
- print ('ХУЙ')
- connections.put(tn)
- #print (connections) #для дебага
- return connections
- class Downloader():
- """ Загрузчик прошивок. """
- def __init__(self):
- """ Конструктор. """
- self.connections = connections
- def bootrom_downloader(self.connections):
- """ Меняем загрузчик коммутатора. """
- connection.write(b'fcopy tftp://10.111.31.11/QSW-2850-28(52)T_2011.12.12_boot.rom boot.rom' + b'\n')
- buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
- print(buff.decode('ascii'))
- if b'[Y/N]:' in buff: connection.write(b'y' + b'\n')
- while True:
- buff = connection.read_until(b'QSW-2850-28T-AC#', 3)
- print (buff.decode('ascii'))
- if b'QSW-2850-28T-AC#' in buff:
- print('boot rom downloaded succesfully')
- break
- else:
- print('download in progress...')
- continue
- def downloader_for_7035 (self.connections):
- """Загружаем промежуточную прошивку """
- 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')
- while True:
- buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
- if b'QSW-2850-28T-AC#' in buff:
- connection.write(b'boot img QSW-X-For-Upgrade_7.0.3.5(R0000.0000)_nos.img primary' + b'\n')
- connection.write(b'reload' + b'\n')
- confirm = connection.read_until(b'QSW-2850-28T-AC#', 5)
- if b'Process with reboot? [Y/N] ' in confirm:
- connection.write(b'y' + b'\n')
- print ('done, reloading device...')
- break
- else:
- print(buff.decode('ascii'))
- print('firmware isnt download, process continue...')
- continue
- def nos_deleter (self.connections):
- """ Удаляем дефолтную прошивку"""
- connection.write(b'delete nos.img' + b'\n')
- buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
- if b'Delete file, Are you sure? (Y/N)?[N]' in buff: connection.write(b'y' + b'\n')
- buff1 = connection.read_until(b'QSW-2850-28T-AC#', 5)
- if b'QSW-2850-28T-AC#' in buff1: print ('nos.img deleted succesfully')
- def downloader_for_821 (self.connection):
- """ Загружаем концептуальную прошивку """
- connection.write(b'copy tftp://10.111.31.11/QSW-2850(3470)-X_8.2.1.54_nos.img nos.img' + b'\n')
- while True:
- buff = connection.read_until(b'QSW-2850-28T-AC#', 5)
- if b'QSW-2850-28T-AC#' in buff:
- connection.write(b'boot img nos.img primary' + b'\n')
- connection.read_until(b'QSW-2850-28T-AC#', 1)
- connection.write(b'reload' + b'\n')
- confirm = connection.read_until(b'QSW-2850-28T-AC#', 5)
- if b'Process with reboot? [Y/N]' in confirm:
- connection.write(b'y' + b'\n')
- print ('done, reloading device...')
- break
- else:
- print(buff.decode('ascii'))
- print('firmware isnt download, process continue...')
- continue
- if __name__ == '__main__':
- switches = ConnectionMaker.get_switches()
- for switch in switches:
- connections = ConnectionMaker.create_connection(switches)
- """ Спавним пул воркер процессов """
- with multiprocessing.Pool(connections.qsize()) as p:
- p.map(Downloader.bootrom_downloader, connections)
- p.map(Downloader.downloader_for_7035, connections)
- time.sleep(60)
- p.map(Downloader.nos_deleter, connections)
- p.map(Downloader.downloader_for_821, connections)
- p.close()
- p.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement