Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- ##----------------------------------------------------------------------
- ## Firmware update
- ##----------------------------------------------------------------------
- ## Copyright (C) 2007-2011 The NOC Project
- ## See LICENSE for details
- ##----------------------------------------------------------------------
- import os, psycopg2, sys, time
- ########################## GET FROM NOC ###########################################
- ### Внимание не все коммутаторы можно обновлять безопасно! В некоторых после обновления возможна установка настроек по умолчанию!
- ### Смотреть возможность обновления без изменения настроек согласно версии прошивки и оборудования на сайте производителя:
- ### Известные проблемы:
- ### http://zyxel.ru/content/support/knowledgebase/KB-1457 при обновлении к прошивки до версии 3.80 и выше!!!
- #Выбираем Profile:
- #DLink.DxS
- # Выбераем обновляемую платформу:
- #platform='DES-3200-10'
- #platform='DES-3200-26'
- #platform='DES-3200-28'
- #hardware='A1'
- # Обновляемая версия прошивки
- #firmware_version='1.10.B015'
- # Путь к прошивке на TFTP сервере....
- #firmware_url='192.168.4.19 /DLink/DES-3200/DES-3200R_1.21.B006.had'
- platform='DES-3028'
- hardware='A1'
- # Путь к прошивке на TFTP сервере....
- firmware_url='192.168.4.19 /DLink/DES-3028/des3028_3052_series_firmware_200b27.had'
- # Обновляемая версия прошивки
- firmware_version='1.00-B32'
- check='1533156'
- #platform='DES-3526'
- #platform='DES-3010G'
- #platform='DGS-3100'
- #hardware='04'
- # Обновляемая версия прошивки
- #firmware_version='1.10.B015'
- # Путь к прошивке на TFTP сервере....
- #firmware_url='192.168.4.19 /DLink/DGS-3100_B1/DGS-3100_series_FW_3.60.28.ros'
- #firmware_url='192.168.4.19 /DLink/DGS-3100_A/DGS-3100_series_A1A2_FW_v3.00.43.ros'
- # Необходимая версия Boot PROM
- #boot_version='1.01.05'
- # Путь к загрузчику прошивки на TFTP сервере....
- #boot_url='192.168.4.19 /DLink/DGS-3100_B1/DGS-3100_series_Boot_1.01.05.rfb'
- #boot_url='192.168.4.19 /DLink/DGS-3100_A/DGS-3100_series_Boot_1.01.05.rfb'
- ####################################################################
- conn = psycopg2.connect("host=localhost dbname=noc user=noc")
- cur = conn.cursor()
- # Пробегаем по всем id объектов только данной платформы и оборудования
- cur.execute( "SELECT managed_object_id FROM sa_managedobjectattribute WHERE key='platform' and value='"+platform+"';" )
- device = cur.fetchall()
- for i in range(len(device)):
- id = str(device[i][0])
- cur.execute( "SELECT address FROM sa_managedobject WHERE id="+id+";" )
- IP = str(cur.fetchone()[0])
- # Проверяем доступность коммутатора.
- if os.popen('ping -c 3 '+IP+"|grep ', 0% packet loss,'").read() <> '':
- #{'platform': 'DGS-3426G', 'vendor': 'DLink', 'version': '2.61.B06'}
- dev_firmware_version = os.popen( 'cd /opt/noc; python manage.py debug-script get_version '+id+' 2>&1 |grep "{'+"'platform': '"+'" |grep -v '+"'Script returns with result: ' |awk -F"+'"'+"'"+'" '+"'"+'{ORS="'+'"; print $12}'+"'" ).read()
- # dev_firmware_version = firmware_version
- if dev_firmware_version == '':
- print ( platform+", id="+id+", IP address="+IP+": Error get_version!..." )
- sys.exit(1)
- elif firmware_version == dev_firmware_version:
- sys.stdout.write ( platform+", id="+id+", IP address="+IP+": Firmware update from "+dev_firmware_version+"..." )
- sys.stdout.flush()
- # Сохраняем настройки свича:
- if os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='"+'["save config"]'+"' 2>&1 |grep 'Command: save config' |grep 'Success.'" ).read() <> '':
- print ( platform+", id="+id+", IP address="+IP+": Error save config!..." )
- sys.exit(1)
- # commands for DES-3200
- # Так делать плохо:
- # os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"download firmware_fromTFTP '+firmware_url+'", "config firmware image_id 1 boot_up", "reboot"]'+"'" ).read()
- # os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"download firmware_fromTFTP '+firmware_url+'", "reboot"]'+"'" ).read()
- # Надо делать вот так:
- #
- # Если надо проверить версию Boot PROM и если нужно обновить...
- # TODO... какой коммандой обновлять бутпром?
- #
- # Проверить image_id и определить свободный image_id=image_id (1 или 2)
- fir_tab=os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"show firmware information"]'+"' 2>&1 |grep -m 1 'Command: show firmware information' |awk -F'Command: show firmware information' '{print $2}'" ).read().split('\\n\\r')
- image_1 = fir_tab[4].split(' ')
- image_2 = fir_tab[5].split(' ')
- image_del = '0'
- # Определить номер загрузочного образа image_id=image_id (1 или 2)
- if image_1[0] == '*1':
- image_id='1'
- if image_2[1] <> '':
- image_del = '2'
- elif image_2[0] == '*2':
- image_id='2'
- if image_1[1] <> '':
- image_del = '1'
- else:
- print ( platform+", id="+id+", IP address="+IP+": Error no boot image..." )
- sys.exit(1)
- #
- # Проверить объем свободного места, на старых прошивках нет
- # os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"show utilization flash"]'+"'" ).read()
- #
- # Если надо освободить место удалив небутабельное firmware.
- if image_del <> '0':
- # os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"config firmware image_id '+image_del+' delete"]'+"'" ).read()
- sys.stdout.write ( ' del='+image_del )
- #
- # Загружаем ружное firmware в image_id
- # os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"download firmware_fromTFTP '+firmware_url+' image_id '+image_id+'"]'+"'" ).read()
- sys.stdout.write ( ', download '+image_id )
- #
- # Проверить размер (хеш) image_id и определить успешность загрузки firmware с TFTP
- fir_tab=os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"show firmware information"]'+"' 2>&1 |grep -m 1 'Command: show firmware information' |awk -F'Command: show firmware information' '{print $2}'" ).read().split('\\n\\r')
- i=3+int(image_id)
- image = fir_tab[i].split(' ')
- if image[0] == '*'+image_id and image[2] == check:
- # Перегружаем свич
- # os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"reboot"]'+"'" ).read()
- sys.stdout.write ( ', reboot ...' )
- else:
- print ( platform+", id="+id+", IP address="+IP+": Error firmware checksum faild..." )
- sys.exit(1)
- quit()
- time.sleep(1)
- # Проверяем доступность коммутатора после перезагрузки. Если недоступен длительное время, выходим...
- i = 0
- while os.popen('ping -c 3 '+IP+"|grep ', 0% packet loss,'").read() == '':
- sys.stdout.write ( "." )
- sys.stdout.flush()
- time.sleep(1)
- i = i + 1
- if i > 50:
- print ( "!" )
- print ( platform+", id="+id+", IP address="+IP+" is faild!!!" )
- sys.exit(1)
- print ( " Updated! :)" )
- else:
- print ( platform+", id="+id+", IP address="+IP+": Firmware "+dev_firmware_version+" skip update! :)" )
- else:
- print ( platform+", id="+id+", IP address="+IP+" is down..." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement