Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paramiko
- import threading
- import os.path
- import subprocess
- import time
- import sys
- import re
- def validate_ip():
- valided_ip_list =[]
- ip_file = open("routers.txt", 'r')
- ip_file.seek(0)
- ip_list = ip_file.readlines()
- ip_file.close()
- for p in ip_list:
- ref = p.strip('\n')
- reply = subprocess.run("ping -n 1 " + ref, 5, shell=False, capture_output=True)
- repstr = str(reply)
- if repstr.__contains__('TTL=') != False:
- valided_ip_list.append(p)
- return valided_ip_list
- def openssh(ip):
- try:
- o = paramiko.SSHClient()
- pol = paramiko.client.AutoAddPolicy()
- o.set_missing_host_key_policy(pol)
- o.connect(ip, 22, username='admin', password='admin',)
- conn = o.invoke_shell()
- t = o.get_transport()
- time.sleep(1)
- conn.send(b'en')
- conn.send(b'\n')
- time.sleep(1)
- conn.send(b'admin')
- conn.send(b'\n')
- time.sleep(1)
- conn.send(b'terminal length 0\n')
- conn.send(b'config t\n')
- time.sleep(1)
- try:
- cmds = open("cmds.txt",'r')
- lines = cmds.readlines()
- for l in lines:
- print("entering line {0} to router {1}".format(l,ip))
- comm = str.encode(l)
- conn.send(comm)
- conn.send(b'\n')
- time.sleep(2)
- cmds.close()
- except:
- print ('bad file read')
- except paramiko.AuthenticationException:
- print("failure authenticating")
- except:
- print("something went wrong {0}".format(ip))
- finally:
- out = conn.recv(65535)
- c = out.decode('ASCII')
- print(c)
- cmds.close()
- o.close()
- def appthread():
- threads =[]
- valided_ip_list = validate_ip()
- num_of_ips = valided_ip_list.__len__()
- if (num_of_ips > 0):
- for ip in valided_ip_list:
- clnip = ip.strip('\n')
- th = threading.Thread(target=openssh,args= (clnip,))
- th.start()
- threads.append(th)
- for th in threads:
- th.join()
- else:
- print("no valid IPs")
- appthread()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement