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

通过python 获取当前局域网内存在的IP和MAC

通过python 获取当前局域网内存在的ip

'''
通过ipconfig /all 命令获取局域网所在的网段
通过arp -d *命令清空当前所有的arp映射表
循环遍历当前网段所有可能的ip与其ping一遍建立arp映射表
for /L %i IN (1,1,254) DO ping -w 1 -n 1 192.168.3.%i
通过arp -a命令读取缓存的映射表获取所有与本机连接的设备的Mac地址。
'''
import os
import re
import time
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
import psutil# 逻辑cpu个数
count = psutil.cpu_count()
print("cpu个数:",str(count))
import pandas as pd
def get_net_segment():with os.popen("arp -a") as res:for line in res:line = line.strip()if line.startswith("接口"):net_segment = re.findall("(\d+\.\d+\.\d+)\.\d+", line)[0]breakreturn net_segment
def ping_net_segment_all(net_segment):# for i in range(1, 255):#     os.system(f"ping -w 1 -n 1 {net_segment}.{i}")# 多线程并发 5个线程时耗时是30秒,8个线程是28秒with ThreadPoolExecutor(max_workers=4) as executor:for i in range(1, 255):executor.submit(os.popen, f"ping -w 1 -n 1 {net_segment}.{i}")
def get_arp_ip_mac():header = Nonelist1 = []#os.system('arp -a > temp11.txt')with os.popen("arp -a") as res:for line in res:line = line.strip()         if not line or line.startswith("接口"):continueif header is None:                header = re.split(" {2,}", line.strip())line1 = re.split(" {2,}", line.strip())list1.append(line1)df = pd.DataFrame(list1,columns=header)return df
def ping_ip_list(ips, max_workers=4):print("正在扫描在线列表")with ThreadPoolExecutor(max_workers=max_workers) as executor:future_tasks = []for ip in ips:future_tasks.append(executor.submit(os.popen, f"ping -w 1 -n 1 {ip}"))wait(future_tasks, return_when=ALL_COMPLETED)
if __name__ == '__main__':# 是否进行初始扫描init_search = True #Falseif init_search:print("正在扫描当前网段所有ip,预计耗时1分钟....")ping_net_segment_all(get_net_segment())last = Nonewhile 1:df = get_arp_ip_mac()df = df.loc[df.类型 == "动态", ["Internet 地址", "物理地址"]]if last is None:print("当前在线的设备:")print(df)else:online = df.loc[~df.物理地址.isin(last.物理地址)]if online.shape[0] > 0:print("新上线设备:")print(online)offline = last[~last.物理地址.isin(df.物理地址)]if offline.shape[0] > 0:print("刚下线设备:")print(offline)time.sleep(5)ping_ip_list(df["Internet 地址"].values)last = df
http://www.lryc.cn/news/149902.html

相关文章:

  • 解决D盘的类型不是基本,而是动态的问题
  • 如何判断自己的qt版本呢?
  • 【文心一言大模型插件制作初体验】制作面试错题本大模型插件
  • ROS 2官方文档(基于humble版本)学习笔记(二)
  • excel中公式结合实际的数据提取出公式计算的分支
  • 3D模型优化实战:LowPoly、纹理烘焙及格式转换
  • genome comparison commend 2 MCMCtree
  • Linux安装JenkinsCLI
  • Midjourney学习(一)prompt的基础
  • 12 权重衰退
  • 简化测试流程,提供卓越服务:TestComplete+Salesforce满足不断发展的企业的需求
  • kafka 命令脚本说明以及在java中使用
  • Qt应用开发(基础篇)——文件选择对话框 QFileDialog
  • 图像OCR转文字,验证码识别技术太疯狂-UI软件自动化
  • Docker:自定义镜像
  • 【Nginx22】Nginx学习:FastCGI模块(四)错误处理及其它
  • 轮毂电机单位换算-米每秒/转每分
  • 博流RISC-V芯片BL616开发环境搭建
  • Weblogic漏洞(三)之 Weblogic 弱口令、任意文件读取漏洞
  • 15 mysql tiny/meidum/long blob/text 的数据存储
  • 【方案】基于视频与AI智能分析技术的城市轨道交通视频监控建设方案
  • mysql8 修改数据存储位置
  • Qt QSlider样式
  • Redis五大数据类型
  • chatGPT训练过程
  • 原神角色数据分析项目说明文档
  • 【Qt】QML-04:自定义变量(属性)property
  • 基于Cadence Allegro无盘设计操作流程
  • 微信小程序 - 2023年最新版手机号快捷登录详细教程
  • Spring_Bean的自动装配