Advertisement
Guest User

port_test.py

a guest
Apr 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #! /usr/bin/python
  2. # Import modules
  3. import socket
  4. import subprocess
  5. import ipaddress
  6. import mysql.connector
  7. import configparser
  8. import logging
  9. import threading
  10. from queue import Queue
  11.  
  12. # lock to serialize console output
  13. lock = threading.Lock()
  14.  
  15. # create queue and thread pool
  16. q = Queue()
  17.  
  18. # configure logging options
  19. logging.basicConfig(format='%(levelname)s:%(message)s',level=logging.INFO)
  20.  
  21. # read from the config file
  22. config = configparser.ConfigParser()
  23. config.read('config.ini')
  24. db=config['mysql']
  25. net=config['network']
  26. port = int(net['port'])
  27.  
  28. def display_results(cursor):
  29. # execute and display the results
  30.     for (client, location, cidr) in cursor:
  31.             logging.info("{} - {}: {} --> ".format(client, location, cidr))
  32.             try:
  33.                 # Prompt the user to input a network address
  34.                 net_addr = str(cidr)
  35.  
  36.                 # Create the network
  37.                 ip_net = ipaddress.ip_network(net_addr)
  38.  
  39.                  # Get all hosts on that network
  40.                 all_hosts = list(ip_net.hosts())
  41.             except ValueError as e:
  42.                 logging.warning(e)
  43.                 continue
  44.  
  45.             # For each IP address in the subnet, test to see if port 3389 is open
  46.             for i in range(len(all_hosts)):
  47.                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  48.                 sock.settimeout(1)
  49.                 result = sock.connect_ex((str(all_hosts[i]),port))
  50.                 if result == 0:
  51.                         logging.info(str(all_hosts[i]) + ": " + net['port'] + " is open")
  52.             else:
  53.                     logging.debug(str(all_hosts[i]) + ": " + net['port'] + " is not open")
  54.  
  55. # create the connection, connect, and setup the query
  56. cnx = mysql.connector.connect(user=db['user'], database=db['database'], password=db['password'])
  57. cursor = cnx.cursor()
  58. query = ("select c.client_name as client, l.location_name as location, fw.net_cidr as cidr "
  59.         "from firewalls fw "
  60.             "left join clients c on c.id = fw.client_id "
  61.             "left join locations l on l.id = fw.location_id "
  62.                 "where fw.net_cidr <> '' and c.active = '1' and fw.active = '1'")
  63.  
  64. cursor.execute(query)
  65.  
  66. for i in range(4):
  67.     t = threading.Thread(target=display_results)
  68.     t.daemon = True
  69.     t.start()
  70.  
  71. for item in range(cursor.rowcount):
  72.     q.put(cursor)
  73.  
  74. # close the database connection
  75. cursor.close()
  76. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement