Fontion d'upgrade des switchs.

Amlioration base du systme de connexion ssh

darcs-hash:20041205011212-41617-14e3db86fdd511a9e96c57f37bf877b0c4e99fb3.gz
This commit is contained in:
pauget 2004-12-05 02:12:12 +01:00
parent 4e225747ce
commit 13e162fcd6

View file

@ -50,6 +50,8 @@ class ssh :
def __init__(self,host) : def __init__(self,host) :
""" Ouverture d'une connexion ssh vers le switch choisi """ """ Ouverture d'une connexion ssh vers le switch choisi """
self.switch = host self.switch = host
self.log=open('/tmp/test_ssh','w')
if self.__debug : self.__logDest.write("SSH DEBUG : __init__(host=%s)\n" % host) if self.__debug : self.__logDest.write("SSH DEBUG : __init__(host=%s)\n" % host)
self.__host = host self.__host = host
self.__retour, self.__input, self.__err = popen3("/usr/bin/ssh -tt %s" % host) self.__retour, self.__input, self.__err = popen3("/usr/bin/ssh -tt %s" % host)
@ -60,7 +62,6 @@ class ssh :
r.start() r.start()
e = threading.Thread(target=self.__read_err,args=(threading.currentThread(),)) e = threading.Thread(target=self.__read_err,args=(threading.currentThread(),))
e.start() e.start()
self.log=open('/tmp/test_ssh','w')
# On passe l'intro, passe manager et en environnement de configuration # On passe l'intro, passe manager et en environnement de configuration
self.send_cmd('\nenable\nconfigure') self.send_cmd('\nenable\nconfigure')
@ -101,7 +102,6 @@ class ssh :
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd(%s)\n" % cmd.strip() ) if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd(%s)\n" % cmd.strip() )
self.__ssh_out = '' # oubli de ce qu'il y a avant self.__ssh_out = '' # oubli de ce qu'il y a avant
self.log.write('*****\n')
self.log.flush() self.log.flush()
# Envoi de la commande # Envoi de la commande
@ -110,30 +110,35 @@ class ssh :
# Premier retour # Premier retour
count=0 count=0
t = ''
while 1: while 1:
sleep(1)
out = self.__ssh_out
# On a récupéré un prompt ? # On a récupéré un prompt ?
try : while_break = 0
print "|%s|" % self.__ssh_out[-45:-38] for n in range(1,200) :
if self.__ssh_out[-45:-38] == ' [y/n]?' : try : t = out[-n-7:-n]
except : continue
if t == ' [y/n]?' :
self.__input.write('y') self.__input.write('y')
self.__input.flush() self.__input.flush()
sleep(1) sleep(1)
continue elif t == 'onfig)#' :
elif self.__ssh_out[-46:-36].lower() == '(config)# ' :
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> OK\n") if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> OK\n")
break while_break=1
elif self.__ssh_out[-70:-7] == '-- MORE --, next page: Space, next line: Enter, quit: Control-C' : elif t == 'ntrol-C' :
# Faut appuyer sur une touche # Faut appuyer sur une touche
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> MORE\n") if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> MORE\n")
self.__input.write(' ') self.__input.write(' ')
self.__input.flush() self.__input.flush()
sleep(1) sleep(1)
else :
continue continue
except: pass break
if while_break : break
# Rien de bien, le switch es un peu lent, on attend # Rien de bien, le switch es un peu lent, on attend
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> WAIT\n") if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> WAIT (%s) \n" % t)
sleep(1)
count += 1 count += 1
if count > timeout : if count > timeout :
# Il y a un problème # Il y a un problème
@ -205,6 +210,7 @@ class hpswitch :
# Quelques paramètres # Quelques paramètres
IP_tftp = '138.231.136.7' IP_tftp = '138.231.136.7'
firmware_file = 'firmware26xx'
# Variables internes # Variables internes
switch = None # nom du switch switch = None # nom du switch
@ -219,11 +225,11 @@ class hpswitch :
# Config snmp # Config snmp
self.get, self.set, self.walk = config_snmp_secrete(snmp,switch) self.get, self.set, self.walk = config_snmp_secrete(snmp,switch)
def __ssh(self,cmd) : def __ssh(self,cmd,timout=15) :
if not self.__conn_ssh : if not self.__conn_ssh :
self.__conn_ssh = ssh(self.switch) self.__conn_ssh = ssh(self.switch)
return self.__conn_ssh.send_cmd(cmd) return self.__conn_ssh.send_cmd(cmd,timout)
def set_prise(self,prise,action) : def set_prise(self,prise,action) :
""" """
@ -335,8 +341,8 @@ class hpswitch :
def upgrade(self) : def upgrade(self) :
""" Changement de firmware, le firmware est sur la machine """ Changement de firmware, le firmware est sur la machine
ayant le serveur tftfp et a pour nom firmware26xx """ ayant le serveur tftfp et a pour nom firmware26xx """
if self.__debug : self.__logDest.write("HP DEBUG : update()\n") if self.__debug : self.__logDest.write("HP DEBUG : upgrate()\n")
self.__ssh("copy tftp flash %s firmware26xx" % self.IP_tftp,70) self.__ssh("copy tftp flash %s %s" % (self.IP_tftp, self.firmware_file) , 120) # Long timout
def reboot(self) : def reboot(self) :
""" Reboote le switch """ """ Reboote le switch """