0% found this document useful (0 votes)
61 views

Multi - Comand - SSH - Telnet .Py

This document defines functions for connecting to remote hosts using telnet or SSH. It reads a list of hosts, usernames, passwords and command types from a file. It then connects to each host using either telnet or SSH depending on the connection type indicated. For each connection, it executes the commands from the specified command file type and saves the output.

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)
61 views

Multi - Comand - SSH - Telnet .Py

This document defines functions for connecting to remote hosts using telnet or SSH. It reads a list of hosts, usernames, passwords and command types from a file. It then connects to each host using either telnet or SSH depending on the connection type indicated. For each connection, it executes the commands from the specified command file type and saves the output.

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

from multiprocessing.

dummy import Pool as ThreadPool


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

def Telnet (HOST,user,password,tipo):


with open("direcciones.txt") as file:
alldata = file.read().splitlines()

for linea in alldata:


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

print (HOST + user + password+ conexion + tipo)


print ("accesando a "+ (HOST))

tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")

if password:
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)

def ssh(HOST,user,password,tipo):
try:
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)

except:
print ("error en " + HOST)

else:
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)
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)
output = remote_conn.recv(65535)
print (output.decode('ascii'))
saveoutput.write (output.decode('ascii'))
time.sleep(10)

saveoutput.close

def RemoteConection(linea):

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

print (HOST + conexion + tipo)


print ("accesando a "+ (HOST))

if conexion == "t":
Telnet(HOST,user,password,tipo)

else:
ssh(HOST,user,password,tipo)
with open("direcciones.txt") as file:
conndata = file.read().splitlines()

"""
for linea in conndata:
RemoteConection(linea)
"""

pool = ThreadPool(len(conndata))

pool.map(RemoteConection,conndata)

pool.close()
pool.join()

You might also like