简体   繁体   English

Python3线程,尝试同时ping通多个IP /测试端口

[英]Python3 threading, trying to ping multiple IPs/test port simultaineously

Full (non-working) code below 下面的完整(无效)代码

Full (working, w/o threading) code here: http://pastebin.com/KUYzNtT2 完整的(有效的,无线程的)代码在这里: http : //pastebin.com/KUYzNtT2

I've written a small script that does the following: 我编写了一个小脚本,该脚本执行以下操作:

  1. Pull network information from a database 从数据库中提取网络信息
  2. Ping each IP in a cidr (ie - 192.168.0.0/24); ping cidr中的每个IP(即-192.168.0.0/24); if it's up, test to see if a certain port is open 如果启动,请测试以查看某个端口是否打开
  3. Display the results 显示结果

This is working fine, but I'd like to implement threading to make the script run faster; 这工作正常,但是我想实现线程化以使脚本运行得更快。 as is I have thousands of IPs to scan and it takes forever. 因为我要扫描的IP数以千计,所以要花很多时间。

I've played around with threading tutorials but can't seem to grasp how to implement it in my script. 我玩过线程教程,但是似乎无法掌握如何在脚本中实现它。

Any thoughts or suggestions are appreciated. 任何想法或建议,表示赞赏。

EDIT: I went in a different direction based on this guide: http://chriskiehl.com/article/parallelism-in-one-line/ 编辑:根据本指南,我朝另一个方向前进: http : //chriskiehl.com/article/parallelism-in-one-line/

Now I run the program and get: File "port_test.py", line 39, in display_results for (client, location, cidr) in results: ValueError: too many values to unpack (expected 3) and I don't understand why. 现在,我运行程序,并得到: File "port_test.py", line 39, in display_results for (client, location, cidr) in results: ValueError: too many values to unpack (expected 3) ,我不明白为什么。 Thoughts? 有什么想法吗?

**EDIT: I think I figured out why it failed, looks like pool.map expects only one data point. **编辑:我想我弄清楚了它为什么失败,看起来像pool.map期望只有一个数据点。 If I only query the DB for CIDRs instead of the other two columns, the program starts spitting out data (MUCH faster than before). 如果仅查询DB而不是其他两列,则该程序将开始吐出数据(比以前快得多)。 So now I need to figure out how to add the other two columns to the results, then sort the results to they make sense (there's no order to the results, which I suppose makes sense) 因此,现在我需要弄清楚如何在结果中添加其他两列,然后对结果进行排序以使它们有意义(我认为这是没有道理的)

#! /usr/bin/python
# Import modules
import socket
import subprocess
import ipaddress
import mysql.connector
import configparser
import logging
import coloredlogs
from multiprocessing.dummy import Pool as ThreadPool

#logging.basicConfig(format='%(levelname)s:%(message)s',level=logging.INFO)


coloredlogs.install(level='DEBUG')
coloredlogs.DEFAULT_LOG_FORMAT = '%(asctime)s -- %(message)s'
# read from the config file
config = configparser.ConfigParser()
config.read('config.ini')
db=config['mysql']
net=config['network']
port = int(net['port'])

# 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 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)
results = cursor.fetchall()

def display_results(results):
# execute and display the results
    for (cidr) in results:
            logging.info("{} --> ".format(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(.25)
                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")

# make a pool of workers
pool = ThreadPool(4)

# ping the cidrs in their own thread
pool.map(display_results, results)
pool.close()
pool.join()

# close the database connection
cursor.close()
cnx.close()

Grab all the data initially and store it in a Queue . 最初获取所有数据并将其存储在Queue

Create a function that runs continuously until the Queue is empty (ie while my_queue.empty() is False . 创建一个连续运行直到Queue为空的函数(即, while my_queue.empty() is False

Grab the first object in the Queue with Queue 's get() method. 抓住在所述第一对象QueueQueueget()方法。

Then process it. 然后处理它。

Initialize as many threads as you want, they will execute until the Queue is empty. 初始化任意数量的线程,它们将一直执行到Queue为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM