Python实现多线程C段Ping扫描与并发存活检测

小岛来信
小岛来信
发布时间:2026-10-12| 2人浏览过
原创

本文介绍如何使用Python实现针对C段IP地址的多线程Ping扫描脚本,以检测网络中存活的主机。通过多线程并发执行ping命令,利用Queue管理IP地址队列,并处理不同操作系统下的ping命令差异(Windows与Linux),从而高效完成网络扫描任务。

多线程C段扫描脚本实现

在Python中实现C段扫描,核心在于利用多线程并发执行ping命令,并通过队列管理IP地址。以下是一个基于Windows系统的实现示例:

import subprocess as p
import time
import threading
from queue import Queue

def check_ip(ip):
    w=p.Popen('ping -n 2 '+ip,shell=True,stdout=p.PIPE,stderr=p.PIPE,encoding='gbk')
    result=w.stdout.read()
    # print(result)
    if 'TTL' in result:
        print(ip,'is Up')

def main():
    q=Queue()
    threads=[]
    threads_count=255
    ips = '39.156.69.'
    for i in range(1,255):
        q.put(ips+str(i))
        # print(q.get())
    for i in range(threads_count):
        t=threading.Thread(target=check_ip,args=(q.get(),))
        t.start()
        threads.append(t)
        time.sleep(0.2)
    for i in threads:
        i.join()
    print('all done')

if __name__ == '__main__':
    main()

该脚本通过Queue对象管理IP地址,多线程并发执行check_ip函数。每个线程从队列中获取一个IP地址,执行ping命令,并根据返回结果判断主机是否存活。需要注意的是,该脚本针对Windows系统,使用了ping -n 2命令。

Python检测Ping扫描脚本实现

对于Linux系统,ping命令的参数有所不同。以下是一个基于Linux系统的Ping扫描脚本示例:

import subprocess
import os
import time
 
def ping(host):
    rc=subprocess.call('ping -c2 %s &> /dev/null' % host,shell=True)
    if rc == 0:
        print('%s:up'% host)
    else:
        print('%s:down'% host)

if __name__ == '__main__':
    ips=['176.130.10.%s' % i for i in range(1,255)]
    for i in ips:
        pid=os.fork()
        if pid==0:
            ping(i)
            exit(0)

该脚本利用os.fork()创建子进程,每个子进程执行一次ping命令。通过检查返回值rc判断主机是否存活。需要注意的是,该脚本针对Linux系统,使用了ping -c2命令。

Python并发扫描存活主机代码示例

结合上述两种方法,可以编写一个跨平台的并发扫描脚本。以下是一个示例:

import subprocess
import threading
from queue import Queue
import platform

def check_ip(ip):
    system = platform.system()
    if system == 'Windows':
        cmd = f'ping -n 2 {ip}'
    else:
        cmd = f'ping -c2 {ip}'
    
    result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if result.returncode == 0:
        print(f'{ip}: up')
    else:
        print(f'{ip}: down')

def main():
    q = Queue()
    threads = []
    ips_prefix = '192.168.1.'  # 替换为实际的IP前缀
    
    for i in range(1, 255):
        q.put(f'{ips_prefix}{i}')
    
    for _ in range(255):
        t = threading.Thread(target=check_ip, args=(q.get(),))
        t.start()
        threads.append(t)
    
    for t in threads:
        t.join()
    print('Scan completed.')

if __name__ == '__main__':
    main()

该脚本通过platform.system()判断操作系统,并执行相应的ping命令。通过多线程并发执行扫描任务,提高了扫描效率。

总结

本文介绍了如何使用Python实现多线程C段Ping扫描脚本,包括Windows和Linux系统的实现方法。通过多线程并发执行ping命令,可以高效地检测网络中存活的主机。在实际应用中,可以根据具体需求调整脚本参数和逻辑。

以上就是Python多线程C段扫描与Ping检测脚本实现的详细内容,更多关于Python并发编程的资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。