python实现批量ping

用简单的代码实现批量ping,并打印无响应的IP

from scapy.all import *
from multiprocessing import Manager
from concurrent.futures import ProcessPoolExecutor
def packet_create(s):##构造ICMP报文
    pk = IP( 
        dst=s
    )/ICMP(
        type=8
    )/data
    return pk

def packet_send(s, down_list):##发送ICMP报文
    ans = sr1(packet_create(s), timeout=8)
    if ans == None:
        down_list.append(s)


if __name__ == "__main__":
    #需要扫描的存放IP地址的文件路径
    file_path = "test.txt"
    data = "A" * 32

    with open(file_path, "r") as f:
        server_IP = f.readlines()

    pool = ProcessPoolExecutor(max_workers=60)

    manager = Manager()
    down_list = manager.list()

    for s in server_IP:
        s = s.replace("\n", "")
        pool.submit(packet_send, s, down_list)
    pool.shutdown()
    
    print('以下IP地址没有响应')
    print(down_list)

  • 上图是test.txt文件的格式
posted @   Désiré  阅读(972)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示