
Problemas con el buffer de telnetlib de Python.
Publicado por Elizabeth (1 intervención) el 10/04/2016 19:21:25
Estoy desarrollando un código que hace conexiones telnet para enviar secuencias de comandos a cada de router. Necesito guardar los registros de cada router en archivos.txt, pero al final los registros guardados de las salidas de los shows commands están incompletos, especialmente los comandos que tienen una salida más larga como un : "show run". Debido a que es una conexión remota, configure en los equipos las líneas vty, el comando (length 512), no obstante siguen guardándose incompletos estos comandos en los archivos.
¿Hay una manera de configurar el máximo del buffer de la librería telnetlib o podría ser otro problema?
Espero contar con sus aportaciones, gracias.
¿Hay una manera de configurar el máximo del buffer de la librería telnetlib o podría ser otro problema?
Espero contar con sus aportaciones, gracias.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import telnetlib
import time
import re
from os import listdir
import glob
import os.path
import os
global ip
ip = []
global cmd_file
cmd_file= []
global bandera
bandera = 0
global lista
def agregardispositivos():
numero = input("Inserte la cantidad de equipos a conectar: ")
return numero
def agregarpuertos(numero):
ip = []
for i in range(numero):
direcciones =raw_input("Ingrese la direccion IP: ")
ip += [direcciones]
return ip
def asociarpuerto_router(numero):
router=[]
for i in range(numero):
nombre= "R"+ str(i+1)
router.append(nombre)
print router
return router
def agregararchivo(numero, pregunta):
#bandera=0
cmd_file = []
for root, dirs, files in os.walk("."):
path = root.split('/')
if os.path.basename(root) == pregunta:
for lista in files:
archivos=lista
cmd_file += [archivos]
return cmd_file
def realizar_prueba(direccion, TELNET_PORT, TELNET_TIMEOUT, READ_TIMEOUT, respuesta, pregunta):
try:
connection = telnetlib.Telnet(direccion, TELNET_PORT, TELNET_TIMEOUT)
output = connection.read_until("name:", READ_TIMEOUT)
connection.write('root' + "\n")
output = connection.read_until("word:", READ_TIMEOUT)
connection.write('admin123'+ "\n")
time.sleep(0.2)
selected_cmd_file= open(respuesta, 'r')
print selected_cmd_file
#Starting from the beginning of the file
selected_cmd_file.seek(0)
for each_line in selected_cmd_file.readlines():
connection.write(each_line + '\n')
time.sleep(0.2)
#Closing the file
selected_cmd_file.close()
#Test for reading command output
output = connection.read_very_eager()
band=1
guardar_salida(output, pregunta,band)
realizar_show(connection,pregunta)
#Closing the connection
connection.close()
time.sleep(0.2)
except IOError:
print "Input parameter error! Please check username, password and file name."
def guardar_salida(output, pregunta,band):
archive = "Corrida_"+pregunta+".txt"
f =open(archive, 'a')
if (band == 1):
f.write ("***** Configuracion de Router *****\n")
if (band == 2):
f.write ("\n\n###### Aplicacion de shows #####\n")
f.write(output)
f.close()
def realizar_show(connection,pregunta):
archivo_show= open('VERIFICATION_STEPS.txt', 'r')
#Starting from the beginning of the file
archivo_show.seek(0)
while True:
auxar = archivo_show.readline()
if (re.search("#STEP "+pregunta[5:]+"#", auxar, re.IGNORECASE)):
auxar = archivo_show.readline()
while auxar != '\n':
connection.write(auxar + '\n')
connection.write(' ')
time.sleep(0.2)
auxar = archivo_show.readline()
break
else:
archivo_show.readline()
archivo_show.close()
output = connection.read_very_eager()
band=2
guardar_salida(output, pregunta,band)
#Open telnet connection to devices
def open_telnet_conn(cmd_file, ip):
j=0
numero=agregardispositivos()
ip=agregarpuertos(numero)
pregunta = raw_input("Dime la carpeta: ")
cmd_file=agregararchivo(numero, pregunta)
print cmd_file
TELNET_PORT= 23
TELNET_TIMEOUT = 5
READ_TIMEOUT = 5
#EL CICLO QUE RECORRE LAS DIRECCIONES IP
for direccion in ip:
#PREGUNTA CONTIENE EL DIRECTORIO (STEP_1 , 2 , 3 , ETC)
cadena =cmd_file [j]
respuesta= "./"+pregunta+"/"+cadena+""
print respuesta
realizar_prueba(direccion, TELNET_PORT, TELNET_TIMEOUT, READ_TIMEOUT, respuesta, pregunta)
j=j+1
while True:
print "1. Ejecutar scripts"
print "2. Salir"
opcion = input("Escribir tu opcion: ")
if opcion ==1:
#Calling the Telnet function
open_telnet_conn(cmd_file, ip)
elif opcion ==2:
break
else:
print "Tu opcion no es valida"
Valora esta pregunta


0