daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Aug 12th, 2018 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import paramiko
  2. import threading
  3. import os.path
  4. import subprocess
  5. import time
  6. import sys
  7. import re
  8.  
  9.  
  10. def validate_ip():
  11.     valided_ip_list =[]
  12.     ip_file = open("routers.txt", 'r')
  13.     ip_file.seek(0)
  14.     ip_list = ip_file.readlines()
  15.     ip_file.close()
  16.     for p in ip_list:
  17.         ref = p.strip('\n')
  18.         reply = subprocess.run("ping -n 1 " + ref, 5, shell=False, capture_output=True)
  19.         repstr = str(reply)
  20.         if repstr.__contains__('TTL=') != False:
  21.             valided_ip_list.append(p)
  22.     return valided_ip_list
  23.  
  24. def openssh(ip):
  25.     try:
  26.         o = paramiko.SSHClient()
  27.         pol = paramiko.client.AutoAddPolicy()
  28.         o.set_missing_host_key_policy(pol)
  29.         o.connect(ip, 22, username='admin', password='admin',)
  30.         conn = o.invoke_shell()
  31.         t = o.get_transport()
  32.         time.sleep(1)
  33.         conn.send(b'en')
  34.         conn.send(b'\n')
  35.         time.sleep(1)
  36.         conn.send(b'admin')
  37.         conn.send(b'\n')
  38.         time.sleep(1)
  39.         conn.send(b'terminal length 0\n')
  40.         conn.send(b'config t\n')
  41.         time.sleep(1)
  42.         try:
  43.             cmds = open("cmds.txt",'r')
  44.             lines = cmds.readlines()
  45.            
  46.             for l in lines:
  47.                 print("entering line {0} to router {1}".format(l,ip))
  48.                 comm = str.encode(l)
  49.                 conn.send(comm)
  50.                 conn.send(b'\n')
  51.                 time.sleep(2)
  52.                 cmds.close()
  53.         except:
  54.             print ('bad file read')
  55.     except paramiko.AuthenticationException:
  56.         print("failure authenticating")
  57.     except:
  58.         print("something went wrong {0}".format(ip))
  59.     finally:
  60.         out = conn.recv(65535)
  61.         c = out.decode('ASCII')
  62.         print(c)
  63.         cmds.close()
  64.         o.close()
  65.    
  66.  
  67. def appthread():
  68.     threads =[]
  69.     valided_ip_list = validate_ip()
  70.     num_of_ips = valided_ip_list.__len__()
  71.     if (num_of_ips > 0):
  72.         for ip in valided_ip_list:
  73.             clnip = ip.strip('\n')
  74.             th = threading.Thread(target=openssh,args= (clnip,))
  75.             th.start()
  76.             threads.append(th)
  77.         for th in threads:
  78.             th.join()
  79.     else:
  80.         print("no valid IPs")
  81.  
  82. appthread()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top