0% found this document useful (0 votes)
46 views3 pages

Respaldos Py

This Python script connects to network devices using either telnet or SSH, executes a list of commands from a file, and saves the output to individual text files. It loops through a text file containing host, username, password, and connection type details. For telnet connections, it authenticates and sends commands, writing the output. For SSH, it establishes a connection, sends commands, and writes that output to a timestamped file.

Uploaded by

Drake Lord
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Respaldos Py

This Python script connects to network devices using either telnet or SSH, executes a list of commands from a file, and saves the output to individual text files. It loops through a text file containing host, username, password, and connection type details. For telnet connections, it authenticates and sends commands, writing the output. For SSH, it establishes a connection, sends commands, and writes that output to a timestamped file.

Uploaded by

Drake Lord
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#/usr/bin/python

import sys
import telnetlib
import getpass
import datetime
import paramiko
import time

with open("direcciones.txt") as file:


alldata = file.read().splitlines()

for linea in alldata:


HOST,user,password,conexion,tipo = linea.split("|")

print (HOST + conexion + tipo)


print ("accesando a "+ (HOST))

if conexion == "t":

tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")

tn.write(user.encode('ascii') + b"\n")

if password:
#time.sleep(5)
tn.read_until(b"password: ")

tn.write(password.encode('ascii') + b"\n")
with open(str(tipo)+".txt") as file:
alldata = file.read().splitlines()
for comando in alldata:
print (comando)
#
#tn.write(b"terminal length 0\n")
tn.write(bytes(comando + "\n", 'utf-8'))

tn.write(b"exit\n")
#
output = tn.read_all().decode('ascii')
print (output)

t = datetime.datetime.now()

timestamp = str(t.year) + str(t.month) + str(t.day) + str(t.hour) +


str(t.minute) + str(t.second)

with open ( timestamp + " Respaldo " + HOST + ".txt", 'w') as


saveoutputtel:
saveoutputtel.write (output)
saveoutputtel.close
time.sleep(5)
else:
remote_conn_pre=paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(HOST, port=22,
username=user,password=password,look_for_keys=False, allow_agent=False)
remote_conn = remote_conn_pre.invoke_shell()

t = datetime.datetime.now()

timestamp = str(t.year) + str(t.month) + str(t.day) + str(t.hour) +


str(t.minute) + str(t.second)

with open (timestamp + " respaldo " + HOST + ".txt", 'w') as saveoutput:

output = remote_conn.recv(65535)
print (output.decode('ascii'))
saveoutput.write (output.decode('ascii'))

#time.sleep(5)
#remote_conn.send("terminal length 0 \n")
#time.sleep(5)
#remote_conn.send("show running-config\n")

time.sleep(5)
print (output.decode('ascii'))
saveoutput.write (output.decode('ascii'))

with open(str(tipo)+".txt") as file:


alldata = file.read().splitlines()
for comando in alldata:
print (comando)

time.sleep(5)
remote_conn.send(bytes(comando + "\n", 'utf-8'))
time.sleep(5)
# remote_conn.send("show running-config\n")
#time.sleep(5)
output = remote_conn.recv(65535)
print (output.decode('ascii'))
saveoutput.write (output.decode('ascii'))

# remote_conn.send("show version\n")
# time.sleep(5)
#output = remote_conn.recv(65535)
#print (output.decode('ascii'))
#saveoutput.write (output.decode('ascii'))

#time.sleep(15)
#print ("Done")
time.sleep(10)
saveoutput.close

You might also like