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

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.xdnf.cn/news/11901.html

相关文章:

  • Redis初入门
  • python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)
  • MySQL 8.0 窗口函数全面解析与实例
  • 力提示(force prompting)的新方法
  • leetcode1443. 收集树上所有苹果的最少时间-medium
  • Oracle数据库笔记
  • Windows下运行Redis并设置为开机自启的服务
  • @Prometheus动态配置管理-ConsulConfd
  • ArcGIS Pro 3.4 二次开发 - 地图探索
  • unix/linux,sudo,其基本概念、定义、性质、定理
  • 705SJBH超市库存管理系统文献综述
  • 关于面试找工作的总结(四)
  • 【无标题】图着色问题的革命性解决方案:拓扑膨胀-收缩对偶理论
  • 【黄金评论】美元走强压制金价:基于NLP政策因子与ARIMA-GARCH的联动效应解析
  • react+taro 开发第五个小程序,解决拼音的学习
  • 如果安装并使用RustDesk
  • TDengine 在电力行业如何使用 AI ?
  • win32com.client模块 —— Python实现COM自动化控制与数据交互
  • Linux系统iptables防火墙实验拓补
  • 77、完全植入式脑机接口神经数据编码解码数据处理等问题答疑[嘿!快看,馒头老师在这里蹲着!]
  • 详解Jenkins Pipeline 中git 命令的使用方法
  • Kubernetes 网络方案:Flannel 插件全解析
  • 常用的录音芯片型号有哪些?
  • 高并发区块链系统实战:从架构设计到性能优化
  • NFS的基本配置
  • Java中的多态
  • Java SpringBoot 调用大模型 AI 构建智能应用实战指南
  • 在树莓派上添加音频输入设备的几种方法
  • Rust学习(1)
  • 采用 Docker GPU 部署的 Ubuntu 或者 windows 桌面环境