Está en la página 1de 2

#importación de bibliotecas

from datetime import date


import time
import telnetlib

def sendTelnet(param1,defaultvalue):
if(param1==None):
tn.write(defaultvalue.encode('ascii')) #envio defaultvalue
else:
tn.write(param1.encode('ascii') + b"\r") #envio param1
time.sleep(delay)

#Defino variables por defecto


defaultUsername = "high"
defaultPassword = "high"
data=""
TIMEOUT = 3
delay=0.1

#variables para cambiar parametros a un PXC


ip="1.1.1.1"
usr="usr"
password="pass"

siteName= ""
nodeName= ""
blnName= ""
dnsSuffix= ""
dhcp= ""
ipAddress= ""
netmask= ""
gatewayAddress= ""
dns1= ""
dns2= ""
dns3= ""
dns4= ""
tcpPort1= ""
tcpPort2= ""
tcpPort3= ""
tcpPort4= ""
tcpPort5= ""
tcpPort6= ""
tcpPort7= ""
tcpPort8= ""
multicastAddress1= ""
multicastPort1= ""
multicastAddress2= ""
multicastPort2= ""
multicastAddress3= ""
multicastPort3= ""
multicastAddress4= ""
multicastPort4= ""
telnetEnabled= ""

try:
#inicio el telnet
tn = telnetlib.Telnet(ip,timeout=TIMEOUT)
tn.set_debuglevel(1) #Habilito el modo debug en la consola
time.sleep(TIMEOUT) #Espero un tiempo para asegurar que la conexión se
estableció
tn.write(b"#") #Reinicio la comunicación (feature del PXC)
tn.write(b"h") #Hello
time.sleep(delay)
data = str(data) + tn.read_until(b"Enter user initials : ", timeout =
TIMEOUT).decode('ascii') #Espero el mensaje de bienvenida
sendTelnet(usr,defaultUsername) #ingreso usuario
data = data + tn.read_until(b"Enter user password : ", timeout =
TIMEOUT).decode('ascii') #Espero solicitud de password
sendTelnet(password,defaultPassword) #ingreso password
time.sleep(delay)
tn.write(b"s") #System
tn.write(b"h") #Hardware
tn.write(b"e") #Ethernet
tn.write(b"s") #ipSetings
tn.write(b"m") #Modify
sendTelnet(siteName,"\r")
sendTelnet(nodeName,"\r")
sendTelnet(blnName,"\r")
sendTelnet(dnsSuffix,"\r")
sendTelnet(dhcp,"\r")
sendTelnet(ipAddress,"\r")
sendTelnet(netmask,"\r")
sendTelnet(gatewayAddress,"\r")
sendTelnet(dns1,"\r")
sendTelnet(dns2,"\r")
sendTelnet(dns3,"\r")
sendTelnet(dns4,"\r")
sendTelnet(tcpPort1,"\r")
sendTelnet(tcpPort2,"\r")
sendTelnet(tcpPort3,"\r")
sendTelnet(tcpPort4,"\r")
sendTelnet(tcpPort5,"\r")
sendTelnet(tcpPort6,"\r")
sendTelnet(tcpPort7,"\r")
sendTelnet(tcpPort8,"\r")
sendTelnet(multicastAddress1,"\r")
sendTelnet(multicastPort1,"\r")
sendTelnet(multicastAddress2,"\r")
sendTelnet(multicastPort2,"\r")
sendTelnet(multicastAddress3,"\r")
sendTelnet(multicastPort3,"\r")
sendTelnet(multicastAddress4,"\r")
sendTelnet(multicastPort4,"\r")
sendTelnet(telnetEnabled,"\r")
tn.write(b"y") #OK to coldstart
data = data + tn.read_until(b"Good Bye", timeout = TIMEOUT).decode('ascii')
#Espero a que salude
print("Imprimiendo..." ) #guardo la sesión TELNET en archivo de texto
op=open ("output.txt", "a").write(data)
tn.close() #cierro la sesión TELNET
except Exception as e:
print(str(e))

También podría gustarte