当前位置: 首页 > news >正文

Python IP可达性检测脚本解析

目录

  1. 脚本概述

  2. 代码解析

    • 导入模块

    • 核心函数check_ip

    • 主程序逻辑

  3. 使用说明

  4. 脚本优化建议

  5. 完整代码

脚本概述

这是一个使用Python编写的IP地址可达性检测脚本,主要功能是批量检测192.168.124.1到192.168.125.254范围内所有IP地址的可达性,并将可达的IP地址保存到文件中。脚本采用了多线程技术,可以同时检测多个IP地址,大大提高了检测效率。

代码解析

导入模块

import pythonping
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
  • pythonping: 用于发送ICMP ping请求的Python库

  • os: 提供操作系统相关功能,这里用于文件操作

  • ThreadPoolExecutor: 实现线程池功能

  • as_completed: 用于获取已完成的任务

核心函数check_ip

def check_ip(ip):try:response = pythonping.ping(ip, count=2, timeout=1, verbose=False)return ip if response.success(option=1) else Noneexcept:return None

函数功能解析:

  1. 使用pythonping向指定IP发送2个ping包,超时时间为1秒

  2. 如果ping成功,返回IP地址

  3. 如果ping失败或发生异常,返回None

  4. verbose=False参数关闭了ping的详细输出

主程序逻辑

if os.path.exists('reachable_ip.txt'):os.remove('reachable_ip.txt')ip_list = [f'192.168.{ip3}.{ip4}' for ip3 in range(124,126) for ip4 in range(1,255)]
with ThreadPoolExecutor(max_workers=50) as executor, \open('reachable_ip.txt', 'a') as ip_file:futures = {executor.submit(check_ip, ip): ip for ip in ip_list}for future in as_completed(futures):ip = futures[future]result = future.result()if result:print(f'{ip} is reachable')ip_file.write(f'{ip}\n')else:print(f'{ip} is not reachable')

执行流程:

  1. 如果存在reachable_ip.txt文件则删除

  2. 生成IP地址列表:192.168.124.1到192.168.125.254

  3. 创建最大50个线程的线程池

  4. 为每个IP提交一个检测任务到线程池

  5. 使用as_completed获取已完成的任务

  6. 将可达的IP地址写入reachable_ip.txt文件

  7. 打印每个IP的检测结果

使用说明

  1. 安装依赖库:

    pip install pythonping
  2. 运行脚本:

    python ip_checker.py
  3. 查看结果:

    • 控制台会输出每个IP的检测结果

    • 可达的IP会保存在reachable_ip.txt文件中

完整代码

import pythonping
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
def check_ip(ip):try:response = pythonping.ping(ip, count=2, timeout=1, verbose=False)return ip if response.success(option=1) else Noneexcept:return None
if os.path.exists('reachable_ip.txt'):os.remove('reachable_ip.txt')ip_list = [f'192.168.{ip3}.{ip4}' for ip3 in range(124,126) for ip4 in range(1,255)]
with ThreadPoolExecutor(max_workers=50) as executor, \open('reachable_ip.txt', 'a') as ip_file:futures = {executor.submit(check_ip, ip): ip for ip in ip_list}for future in as_completed(futures):ip = futures[future]result = future.result()if result:print(f'{ip} is reachable')ip_file.write(f'{ip}\n')else:print(f'{ip} is not reachable')

http://www.lqws.cn/news/125569.html

相关文章:

  • 蓝桥杯17114 残缺的数字
  • NPOI操作EXCEL文件 ——CAD C# 二次开发
  • 【Linux】Linux 环境变量
  • [3-02-01].第13节:三方整合 - Jedis客户端操作Redis
  • 【游戏科学】游戏开发中数学算法的核心与应用
  • 四款主流物联网操作系统(FreeRTOS、LiteOS、RT-Thread、AliOS)的综合对比分析
  • 依赖注入的注解
  • IDEA中微服务指定端口启动
  • 每日Prompt:每天上班的状态
  • 【android bluetooth 协议分析 12】【A2DP详解 2】【开启ble扫描-蓝牙音乐卡顿分析】
  • 在 Android 框架中,接口的可见性规则
  • 解决Java项目NoProviderFoundException报错
  • 代码随想录 算法训练 Day22:回溯算法part01
  • 07 APP 自动化- appium+pytest+allure框架封装
  • java31
  • Vue.js教学第十九章:Vue 工具与调试,Vue DevTools 的使用与 VS Code 插件辅助开发
  • 匀速旋转动画的终极对决:requestAnimationFrame vs CSS Animation
  • AI在网络安全领域的应用现状和实践
  • unix/linux,sudo,其发展历程详细时间线、由来、历史背景
  • 《PyTorch:开启深度学习新世界的魔法之门》
  • 使用 React Native 开发鸿蒙(HarmonyOS)运动健康类应用的系统化准备工作
  • DrissionPage调试工具:网页自动化与数据采集的革新利器
  • AI自动化任务执行工具OpenManus一键启动整合包
  • unix/linux,sudo,其历史争议、兼容性、生态、未来展望
  • @Prometheus 监控-MySQL (Mysqld Exporter)
  • 第四十二天打卡
  • 深度学习之路——CNN卷积神经网络详解
  • Asp.net Core 通过依赖注入的方式获取用户
  • Facebook接入说明
  • CentOS 7 修改为静态 IP 地址完整指南