Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Прошиватель """
- from telnetlib import Telnet
- from datetime import time
- """ Определяем глобальные переменные """
- USER = 'admin'
- PASSWORD = 'admin'
- """ Читаем ip из файла """
- def get_switches():
- switches = []
- with open('switches.txt', 'r') as f:
- for switch in f:
- switches.append(switch.rstrip())
- return switches
- def create_telnet_connections(switches, port=23):
- """ Создаем телнет соединения. """
- connections = []
- 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.append(tn)
- print (connections) #для дебага
- return connections
- def bootrom_downloader(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 nos_deleter (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_7035 (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 downloader_for_821 (connections):
- """ Загружаем концептуальную прошивку """
- 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
- def handler():
- """ Обработчик """
- switches = get_switch()
- for ip in switches:
- connection = create_telnet_connections(ip) #устанавливаем телнет соединение для одного ip
- bootrom_downloader(connection) #меняем загрузчик
- downloader_for_7035(connection) # загружаем промежуточную прошивку
- time.sleep(60) # скрипт спит 60 сек, пока свитч перезагружается
- connection = create_telnet_connections(ip) # создаем телнет соединение после рестарта свитча
- nos_deleter(connection) # удаляем дефолтную прошивку, чтобы освободить место в flash
- downloader_for_821(connection) # загружаем концептуальную прошивку
- print (str(connection.host) + ' is done, go next...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement