Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- # Import modules
- import socket
- import subprocess
- import ipaddress
- import mysql.connector
- import configparser
- import logging
- import threading
- from queue import Queue
- # lock to serialize console output
- lock = threading.Lock()
- # create queue and thread pool
- q = Queue()
- # configure logging options
- logging.basicConfig(format='%(levelname)s:%(message)s',level=logging.INFO)
- # read from the config file
- config = configparser.ConfigParser()
- config.read('config.ini')
- db=config['mysql']
- net=config['network']
- port = int(net['port'])
- def display_results(cursor):
- # execute and display the results
- for (client, location, cidr) in cursor:
- logging.info("{} - {}: {} --> ".format(client, location, cidr))
- try:
- # Prompt the user to input a network address
- net_addr = str(cidr)
- # Create the network
- ip_net = ipaddress.ip_network(net_addr)
- # Get all hosts on that network
- all_hosts = list(ip_net.hosts())
- except ValueError as e:
- logging.warning(e)
- continue
- # For each IP address in the subnet, test to see if port 3389 is open
- for i in range(len(all_hosts)):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(1)
- result = sock.connect_ex((str(all_hosts[i]),port))
- if result == 0:
- logging.info(str(all_hosts[i]) + ": " + net['port'] + " is open")
- else:
- logging.debug(str(all_hosts[i]) + ": " + net['port'] + " is not open")
- # create the connection, connect, and setup the query
- cnx = mysql.connector.connect(user=db['user'], database=db['database'], password=db['password'])
- cursor = cnx.cursor()
- query = ("select c.client_name as client, l.location_name as location, fw.net_cidr as cidr "
- "from firewalls fw "
- "left join clients c on c.id = fw.client_id "
- "left join locations l on l.id = fw.location_id "
- "where fw.net_cidr <> '' and c.active = '1' and fw.active = '1'")
- cursor.execute(query)
- for i in range(4):
- t = threading.Thread(target=display_results)
- t.daemon = True
- t.start()
- for item in range(cursor.rowcount):
- q.put(cursor)
- # close the database connection
- cursor.close()
- cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement