Nouvelle facon de faire des connexions ssh
darcs-hash:20050305162613-d1718-ecb2cba8e230d986dd659b8d847cc86b2fb4a269.gz
This commit is contained in:
parent
8966da15c2
commit
5ac75ce6f0
1 changed files with 39 additions and 93 deletions
|
@ -14,7 +14,6 @@ from popen2 import popen3
|
||||||
from sys import stderr, path
|
from sys import stderr, path
|
||||||
from commands import getstatusoutput
|
from commands import getstatusoutput
|
||||||
from annuaires import chbre_prises, all_switchs, uplink_prises
|
from annuaires import chbre_prises, all_switchs, uplink_prises
|
||||||
import threading
|
|
||||||
|
|
||||||
try :
|
try :
|
||||||
from secrets import config_snmp_secrete
|
from secrets import config_snmp_secrete
|
||||||
|
@ -40,112 +39,59 @@ class ConversationError(Exception) :
|
||||||
|
|
||||||
# Classes de base pour comminiquer (ssh et snmp)
|
# Classes de base pour comminiquer (ssh et snmp)
|
||||||
|
|
||||||
|
import pexpect
|
||||||
class ssh :
|
class ssh :
|
||||||
""" Ouverture d'une connexion ssh, envoi de commandes et récupération du résultat """
|
""" Ouverture d'une connexion ssh, envoi de commandes et récupération du résultat """
|
||||||
__debug = 0
|
|
||||||
__ssh_log = '/dev/null'
|
|
||||||
__logDest = stderr
|
|
||||||
|
|
||||||
__ssh_out = '' # Retour de la connexion 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(self.__ssh_log,'w')
|
self.__sshout = ''
|
||||||
|
self.ssh = pexpect.spawn("ssh %s" % host)
|
||||||
if self.__debug : self.__logDest.write("SSH DEBUG : __init__(host=%s)\n" % host)
|
self.ssh.sendline("")
|
||||||
self.__host = host
|
self.ssh.sendline("configure")
|
||||||
self.__retour, self.__input, self.__err = popen3("/usr/bin/ssh -tt %s" % host)
|
self.ssh.expect("%s\(config\)# " % self.switch, timeout=10)
|
||||||
sleep(1)
|
|
||||||
|
|
||||||
# Création de threads de lecture de la connexion
|
|
||||||
r = threading.Thread(target=self.__read_retour,args=(threading.currentThread(),))
|
|
||||||
r.start()
|
|
||||||
e = threading.Thread(target=self.__read_err,args=(threading.currentThread(),))
|
|
||||||
e.start()
|
|
||||||
|
|
||||||
# On passe l'intro, passe manager et en environnement de configuration
|
|
||||||
self.send_cmd('\nenable\nconfigure')
|
|
||||||
|
|
||||||
def __read_retour(self,parent) :
|
|
||||||
while parent.isAlive() :
|
|
||||||
c = self.__retour.read(1)
|
|
||||||
self.__ssh_out += c
|
|
||||||
self.log.write(c)
|
|
||||||
self.log.flush()
|
|
||||||
|
|
||||||
def __read_err(self,parent) :
|
|
||||||
err = ''
|
|
||||||
while parent.isAlive :
|
|
||||||
char = self.__err.read(1)
|
|
||||||
if char == '\n' :
|
|
||||||
# Il y a eu une erreur
|
|
||||||
if err.lower() in [ 'connection to %s closed by remote host.' % self.switch ,
|
|
||||||
'connection to %s closed.' % self.switch ] :
|
|
||||||
raise ConnectionClosed
|
|
||||||
else :
|
|
||||||
print err
|
|
||||||
err = ''
|
|
||||||
else :
|
|
||||||
err += char
|
|
||||||
|
|
||||||
def __del__(self) :
|
def __del__(self) :
|
||||||
"""Ferme la connexion : envoi logout et attend """
|
"""Ferme la connexion : envoi logout et attend """
|
||||||
if self.__debug : self.__logDest.write("SSH DEBUG : __del__()\n")
|
self.ssh.sendline("logout")
|
||||||
try :
|
self.ssh.send("y")
|
||||||
self.send_cmd('\nlogout')
|
self.ssh.send("y")
|
||||||
except ConnectionClosed :
|
self.ssh.close()
|
||||||
# C'est le but
|
|
||||||
pass
|
|
||||||
|
|
||||||
def send_cmd(self,cmd,timeout=15):
|
def send_cmd(self,cmd,timeout=15):
|
||||||
""" Envoi une commande, attend le prompt et retourne la réponse """
|
""" Envoi une commande, attend le prompt et retourne la réponse """
|
||||||
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.log.flush()
|
|
||||||
|
|
||||||
# Envoi de la commande
|
# Envoi de la commande
|
||||||
self.__input.write(cmd+'\n')
|
self.ssh.sendline(cmd)
|
||||||
self.__input.flush()
|
self.__sshout = ''
|
||||||
|
|
||||||
# Premier retour
|
try:
|
||||||
count=0
|
# Attente de la réponse
|
||||||
t = ''
|
|
||||||
while 1:
|
while 1:
|
||||||
sleep(1)
|
index = self.ssh.expect([' \[y/n\]\? ',
|
||||||
out = self.__ssh_out
|
'%s\(config\)# ' % self.switch,
|
||||||
# On a récupéré un prompt ?
|
'quit: Control-C'],
|
||||||
while_break = 0
|
timeout=timeout)
|
||||||
for n in range(1,200) :
|
|
||||||
try : t = out[-n-7:-n]
|
self.__sshout = self.__sshout + self.ssh.before
|
||||||
except : continue
|
if index == 0:
|
||||||
if t == ' [y/n]?' :
|
# On répond oui
|
||||||
self.__input.write('y')
|
self.ssh.send("y")
|
||||||
self.__input.flush()
|
elif index == 1:
|
||||||
sleep(1)
|
# On est revenu au prompt
|
||||||
elif t == 'onfig)#' :
|
|
||||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> OK\n")
|
|
||||||
while_break=1
|
|
||||||
elif t == 'ntrol-C' :
|
|
||||||
# Faut appuyer sur une touche
|
|
||||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> MORE\n")
|
|
||||||
self.__input.write(' ')
|
|
||||||
self.__input.flush()
|
|
||||||
sleep(1)
|
|
||||||
else :
|
|
||||||
continue
|
|
||||||
break
|
break
|
||||||
|
elif index == 2:
|
||||||
|
# On doit continuer, on envoie espace
|
||||||
|
self.ssh.send(" ")
|
||||||
|
|
||||||
if while_break : break
|
return self.__sshout
|
||||||
# Rien de bien, le switch es un peu lent, on attend
|
except pexpect.TIMEOUT:
|
||||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> WAIT (%s) \n" % t)
|
print "Timeout !"
|
||||||
count += 1
|
import traceback
|
||||||
if count > timeout :
|
traceback.print_exc()
|
||||||
# Il y a un problème
|
print "Contenu du buffer :"
|
||||||
raise ConnectionTimout
|
print self.ssh.before
|
||||||
|
|
||||||
return self.__ssh_out
|
|
||||||
|
|
||||||
class snmp :
|
class snmp :
|
||||||
""" Classe de communication SNMP """
|
""" Classe de communication SNMP """
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue