Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2. import colorama
  3. import paramiko
  4. import time
  5. import ipaddr
  6. import os
  7. import socket
  8. import getpass
  9. import thread
  10. #import csv
  11. from colorama import Fore, Back, Style
  12. from threading import *
  13. def disable_paging(remote_conn):
  14.     '''Disable paging on a Cisco router'''
  15.     remote_conn.send("terminal length 0\n")
  16.     time.sleep(1)
  17.  
  18.     # Clear the buffer on the screen
  19.     output = remote_conn.recv(1000)
  20.  
  21.     return output
  22. def payload (file,docnoconnect,username,password,enable,cmd,cmd2):
  23. #acquire screenclock
  24. # Static variable for loop
  25.     count = 1
  26.  
  27. #begin output for user
  28.     screenLock.acquire()
  29.     print "[+]  Current IP Address: %s" % ip
  30.     screenLock.release()
  31. # Create instance of SSHClient object
  32.     remote_conn_pre = paramiko.SSHClient()
  33.  
  34. # Automatically add untrusted hosts (make sure okay for security policy in your environment)
  35.     remote_conn_pre.set_missing_host_key_policy(
  36.     paramiko.AutoAddPolicy())
  37.  
  38. # initiate SSH connectioni
  39.     screenLock.acquire()
  40.     print "[+]  Establishing SSH Connection to %s" % ip
  41.     screenLock.release()
  42.  
  43. # exectute the ssh connection with exception management
  44.     try:
  45.         remote_conn_pre.connect(ip, username=username, password=password, timeout=2)
  46.     except paramiko.SSHException, e:
  47.                 screenLock.acquire()
  48.                 print (Fore.RED + "[-]  Password is not valid!"),
  49.                 print(Fore.RESET + Back.RESET + Style.RESET_ALL)
  50.                 screenLock.release()
  51.                 return
  52.     except paramiko.AuthenticationException:
  53.                 screenLock.acquire()
  54.                 print (Fore.RED + "[-]  Authentication failed for some reason!"),
  55.                 print(Fore.RESET + Back.RESET + Style.RESET_ALL)
  56.                 screeLock.Release()
  57.                 return
  58.     except socket.error, e:
  59.                 screenLock.acquire()
  60.                 print (Fore.RED + "[-]  Socket connection failed on %s:" % ip),
  61.                 print(Fore.RESET + Back.RESET + Style.RESET_ALL)
  62.                 screenLock.release()
  63.                 count = count + 1
  64.                 if docnoconnect in ['y', 'Y' , "yes" , "Yes" , "YES"]:
  65.                         f.write("\n")
  66.                         f.write("***************")
  67.                         f.write("Unable to contact host @ ")
  68.                         f.write(ip)
  69.                         f.write("***************")
  70.                         f.write("\n")
  71.                 return
  72. # SSH established
  73.     screenLock.acquire()
  74.     print "[+]  SSH connection established to %s" % ip
  75.     screenLock.release()
  76.  
  77. # Use invoke_shell to establish an 'interactive session'
  78.     remote_conn = remote_conn_pre.invoke_shell()
  79.     screenLock.acquire()
  80.     print "[+]  Interactive SSH session established"
  81.     screenLock.release()
  82.  
  83. # Strip the initial router prompt
  84.     output = remote_conn.recv(1000)
  85.  
  86. # Turn off paging
  87.     disable_paging(remote_conn)
  88.  
  89. # Send enable command and wait a bit
  90.     remote_conn.send("\n")
  91.     remote_conn.send("enable\n")
  92.     time.sleep(.25)
  93.     output = remote_conn.recv(5000)
  94.  
  95. # send enable password
  96.     remote_conn.send(enable)
  97.     remote_conn.send("\n")
  98.     output = remote_conn.recv(5000)
  99.     time.sleep(.25)
  100.  
  101. # send the first command line
  102.     screenLock.acquire()
  103.     print "[+]  Transmitting command: # %s" % cmd,
  104.     print "to target:%s" % ip
  105.     screenLock.release()
  106.     remote_conn.send(cmd)
  107.     remote_conn.send("\n")
  108.     output = remote_conn.recv(1000)
  109.  
  110. # send the second command line
  111.     time.sleep(.25)
  112.     screenLock.acquire()
  113.     print "[+]  Transmitting command: # %s" % cmd2,
  114.     print "to target:%s" % ip
  115.     screenLock.release()
  116.     remote_conn.send(cmd2)
  117.     remote_conn.send("\n")
  118.  
  119. # Wait for the command to complete
  120.  
  121.     time.sleep(1)
  122.     output = remote_conn.recv(5000)
  123.  
  124. # output and convert to strings for file writing
  125.  #   print output
  126.     outputs = str(output)
  127.     ipconvs = str(ip)
  128.     f.write("\n")
  129.     f.write(ipconvs)
  130.     f.write("\n")
  131.     f.write(outputs)
  132.     f.write("\n")
  133.     f.write("\n")
  134.     f.write("**************************************************************\n")
  135. # sleep just for display remove when only concerned with file writing
  136. #             time.sleep(2)
  137.  
  138. def fireatwill():
  139.         for line in fileIN:
  140.             ip =  str(line.strip())
  141.             payload (ip,docnoconnect,username,password,enable,cmd,cmd2)
  142.  
  143. # Increment counter and increment ip address for scanning
  144.  
  145. #count = count + 1
  146. #    ipconv = ipconv + incri
  147.  
  148. if __name__ == '__main__':
  149. #   VARIABLES
  150.     os.system("clear")
  151.     inputfile = raw_input("Enter the input file name [Press enter to use input.txt]: ")
  152.     outputfile = raw_input("Enter the output file name [Press enter to use output.txt]: ")
  153.     docnoconnect = raw_input("Do you want to document hosts that don't conenct? ")
  154.     username = raw_input("Username? ")
  155.     password = getpass.getpass("Password? ")
  156.     enable = getpass.getpass("Enable Password? ")
  157.     cmd = raw_input("Enter the first cisco command to execute: #")
  158.     cmd2 = raw_input("Enter the second cisco command to execute: #")
  159.  
  160. # semaphore for threading output
  161.     screenLock = Semaphore(value=1)
  162.     if len (inputfile) < 1:
  163.          inputfile = "inputfile.txt"
  164.     if len (outputfile) < 1:
  165.          outputfile = "outputfile.txt"
  166.     f = open(outputfile, 'w')
  167.  
  168. #open the input file
  169.     fileIN = open(inputfile, "r")
  170.  
  171. #Start the loop based on input file
  172.     for line in fileIN:
  173.         thread.start_new_thread(fireatwill,())
  174. #       thread.start_new_thread(fireatwill,())
  175.  
  176.     fileIN.close()
  177.  
  178.  
  179.  
  180. Enter the second cisco command to execute: #
  181. Unhandled exception in thread started by <function fireatwill at 0x7f577956e398>Unhandled exception in thread started by <function fireatwill at 0x7f577956e398>Unhandled exception in thread started by <function fireatwill at 0x7f577956e398>Unhandled exception in thread started by <function fireatwill at 0x7f577956e398>Unhandled exception in thread started by <function fireatwill at 0x7f577956e398>
  182. Traceback (most recent call last):
  183.  
  184. Traceback (most recent call last):
  185.  
  186. Traceback (most recent call last):
  187.  
  188. Traceback (most recent call last):
  189.   File "./ciscorev30.py", line 139, in fireatwill
  190.       File "./ciscorev30.py", line 139, in fireatwill
  191.  
  192. Traceback (most recent call last):
  193.   File "./ciscorev30.py", line 139, in fireatwill
  194.     for line in fileIN:
  195.   File "./ciscorev30.py", line 139, in fireatwill
  196.   File "./ciscorev30.py", line 139, in fireatwill
  197. for line in fileIN:
  198.     TypeError: 'NoneType' object is not iterable
  199. for line in fileIN:
  200. TypeError: 'NoneType' object is not iterable
  201.     for line in fileIN:
  202. TypeError: 'NoneType' object is not iterable
  203.     for line in fileIN:
  204. TypeError: 'NoneType' object is not iterable
  205. TypeError: 'NoneType' object is not iterable
  206. valdezd@valdezdub:~/Development$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement