吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1430|回复: 16
收起左侧

[学习记录] Python一键提取局域网在线IP和对应Mac地址

[复制链接]
执_念 发表于 2023-7-20 15:37
第一种代码
[Python] 纯文本查看 复制代码
第一种代码

import os
import re


# 获取局域网在线IP和对应Mac地址
def get_ip_mac():
    ip_mac = os.popen('arp -a')
    ip_mac = ip_mac.read()
    ip_mac = ip_mac.split('\n')
    ip_mac = ip_mac[3:-1]
    ip_mac_list = []
    for i in ip_mac:
        ip_mac_dict = {'ip': re.findall(r'(\d+\.){3}\d+', i)[0],
                       'mac': re.findall(r'(([0-9a-fA-F]{2}-){5}[0-9a-fA-F]{2})', i)[0][0]}
        ip_mac_list.append(ip_mac_dict)
    return ip_mac_list


# 获取局域网在线IP
def get_ip():
    ip_mac_list = get_ip_mac()
    ip_list = []
    for i in ip_mac_list:
        ip_list.append(i['ip'])
    return ip_list


# 获取局域网在线Mac地址
def get_mac():
    ip_mac_list = get_ip_mac()
    mac_list = []
    for i in ip_mac_list:
        mac_list.append(i['mac'])
    return mac_list


if __name__ == '__main__':
    print(get_ip())
    print(get_mac())

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
xtstour + 1 + 1 我很赞同!
ldxxhcjs + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

土鸡炖蘑菇 发表于 2023-7-20 16:16
C:\Python38\python.exe D:/python/测试/不保存222.py
Traceback (most recent call last):
  File "D:/python/测试/不保存222.py", line 38, in <module>
    print(get_ip())
  File "D:/python/测试/不保存222.py", line 21, in get_ip
    ip_mac_list = get_ip_mac()
  File "D:/python/测试/不保存222.py", line 13, in get_ip_mac
    ip_mac_dict = {'ip': re.findall(r'(\d+\.){3}\d+', i)[0],
IndexError: list index out of range

进程已结束,退出代码为 1
bdzwater 发表于 2023-7-20 16:25
自从有了chatgpt,写代码方便了好多,直接说需求就行了
gun008 发表于 2023-7-20 16:25
PS D:\Documents\mine> python .\Ip.py
['100.', '100.', '100.', '100.', '100.', '100.', '100.', '0.', '0.', '0.', '255.', '255.']
['74-05-a5-53-7c-34', 'd4-5d-64-06-3a-d2', '3c-7c-3f-2b-db-4d', '78-24-af-d9-2a-83', '40-b0-76-46-cc-ca', 'd4-5d-64-06-3c-5b', 'ff-ff-ff-ff-ff-ff', '01-00-5e-00-00-16', '01-00-5e-00-00-fb', '01-00-5e-00-00-fc', '01-00-5e-7f-ff-fa', 'ff-ff-ff-ff-ff-ff']
PS D:\Documents\mine>
斯文败类 发表于 2023-7-20 16:27
import subprocess
import re

def get_ip_mac():
    result = subprocess.run(['arp', '-a'], capture_output=True, text=True)
    arp_output = result.stdout.splitlines()
    ip_mac_list = []

    for line in arp_output[3:-1]:
        ip = re.findall(r'(\d+\.\d+\.\d+\.\d+)', line)[0]
        mac = re.findall(r'([0-9a-fA-F]{2}-){5}[0-9a-fA-F]{2}', line)[0]
        ip_mac_list.append({'ip': ip, 'mac': mac})

    return ip_mac_list

def get_ip():
    ip_mac_list = get_ip_mac()
    ip_list = [entry['ip'] for entry in ip_mac_list]
    return ip_list

def get_mac():
    ip_mac_list = get_ip_mac()
    mac_list = [entry['mac'] for entry in ip_mac_list]
    return mac_list

if __name__ == '__main__':
    print(get_ip())
    print(get_mac())
anwen 发表于 2023-7-20 16:31

似乎有点儿问题?不太对,另外楼主开头的第一行的第一种代码注释一下或者删掉~哈哈

执行结果

执行结果好像没一个对的QAQ

['31.', '31.', '31.', '31.', '31.', '0.', '0.', '0.', '255.', '255.']
['64-64-4a-ad-c8-98', '90-78-41-8d-77-dc', '7c-fd-6b-02-c6-6a', '64-9e-31-bc-e8-c5', 'ff-ff-ff-ff-ff-ff', '01-00-5e-00-00-16', '01-00-5e-00-00-fb', '01-00-5e-00-00-fc', '01-00-5e-7f-ff-fa', 'ff-ff-ff-ff-ff-ff']
wkdxz 发表于 2023-7-20 16:31
如果不列出本机的IP和MAC,还代码可以简单一些
[Python] 纯文本查看 复制代码
import os


def lan_ip_mac():
    output = os.popen('arp -a')
    return [[ip, mac] for i in output if '动态' in i for ip, mac, _ in [i.strip().split()]]


[print(f'{ip}\t{mac}') for ip, mac in lan_ip_mac()]
anwen 发表于 2023-7-20 16:36
本帖最后由 anwen 于 2023-7-20 16:37 编辑
wkdxz 发表于 2023-7-20 16:31
如果不列出本机的IP和MAC,还代码可以简单一些[mw_shl_code=python,true]import os

能获取本机ipv6的么?这个好像只有ipv4~
我把v6开通了,但是v6是会变化的,可以设置一个脚本定时推送到各种接收消息的软件上面
定时执行,这样妈妈再也不用担心在外面的时候访问不了家里啦~

192.168.31.1        64-64-4a-ad-c8-98
192.168.31.14        90-78-41-8d-77-dc
192.168.31.65        7c-fd-6b-02-c6-6a
192.168.31.159        64-9e-31-bc-e8-c5
law.liu 发表于 2023-7-20 16:45
[Python] 纯文本查看 复制代码
import os
import re

# 获取局域网在线IP和对应Mac地址
def get_ip_mac():
    ip_mac = os.popen('arp -a')
    ip_mac = ip_mac.read()
    ip_mac = ip_mac.split('\n')
    ip_mac = ip_mac[3:-1]
    ip_mac_list = []
    for i in ip_mac:
        ip = re.findall(r'(\d+\.\d+\.\d+\.\d+)', i)
        mac = re.findall(r'([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}', i)
        if ip and mac:
            ip_mac_dict = {'ip': ip[0], 'mac': mac[0]}
            ip_mac_list.append(ip_mac_dict)
    return ip_mac_list

# 保存IP和对应MAC地址到txt文件
def save_ip_mac_to_txt(filename):
    ip_mac_list = get_ip_mac()
    with open(filename, 'w') as file:
        for entry in ip_mac_list:
            file.write(f"IP: {entry['ip']} - MAC: {entry['mac']}\n")

if __name__ == '__main__':
    save_ip_mac_to_txt('ip_mac_addresses.txt')
li231475 发表于 2023-7-20 16:45
arp -a的命令我记得并不能显示全部在线的ip。
我还以为是用ping之类的命令去遍历ip
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 21:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表