跨网络IP的MAC地址 [英] MAC Address from IP across network

查看:182
本文介绍了跨网络IP的MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你一切都好.

我想知道您是否可以帮助我或为我指明正确的方向.我目前正在从事一个围绕网络管理的项目.由于时间限制,我正在使用开源代码.我遇到的问题是该项目的一部分要求我能够捕获连接到网络的所有设备的MAC地址.

I'm wondering if you could help me or point me in the right direction. I'm currently working on a project that centers around network management. Due to severe time constraints where possible I'm using opensource code. The issue I'm having is that part of the project requires me to be able to capture the MAC addresses of all of the devices that are connected to the network.

由于我在过去4年中一直在软件工程的其他领域工作,因此我对面向网络的编程的了解有限.我采用的方法是使用nmap作为获取IP地址和我需要的其他信息的基础. MAC地址不包含在nmap输出中,从我阅读的内容来看,它似乎有些不稳定. (我可能是错的.)

My knowledge of network orientated programming is limited as I have been working in other areas of software engineering for the past 4 years. The approach I have taken is to use nmap as a basis to get the ip address and other information I need. The MAC address is not included in the nmap out put and from what I have read it seems to be a bit flakey. (i could be wrong).

因此,我尝试采用两阶段方法进行此操作,首先,我从nmap获得包括ip地址在内的数据,效果很好.我的下一步和我遇到的困难是,我ping了有效的ip地址(从我的python程序中).但是,如何从IP地址获取MAC地址?我最初以为是ping IP并从ARP抓取MAC,但我认为只有在IP地址位于同一子网中时,该方法才有效.使部署问题更加复杂的是,网络上可能需要记录多达5000台计算机.为了向您展示我的python ping方法,这是代码:

Therefore I have tried to do this in a two stage approach, firstly I get the data including ip address from nmap which works fine. My next step and the bit I'm having difficulty with is I ping the ip address (from within my python program) which works. But how do I get the MAC Address from the IP address? I initially thought ping the ip and grab the MAC from the ARP but I think that will only work if the IP address is on the same subnet. to compound the problem on deployment there could be up to 5000 computers on the network that needs to be logged. To show you my python ping approach this is the code:

import pdb, os
import subprocess
import re
from subprocess import Popen, PIPE

# This will only work within the netmask of the machine the program is running on cross router MACs will be lost
ip ="192.168.0.4"

#PING to place target into system's ARP cache 
process = subprocess.Popen(["ping", "-c","4", ip], stdout=subprocess.PIPE)
process.wait()

result = process.stdout.read()
print(result)

#MAC address from IP
pid = Popen(["arp", "-n", ip], stdout=PIPE)
s = pid.communicate()[0]

# [a-fA-F0-9] = find any character A-F, upper and lower case, as well as any number
# [a-fA-F0-9]{2} = find that twice in a row
# [a-fA-F0-9]{2}[:|\-] = followed by either a ?:? or a ?-? character (the backslash escapes the hyphen, since the  # hyphen itself is a valid metacharacter for that type of expression; this tells the regex to look for the hyphen character, and ignore its role as an operator in this piece of the expression)
# [a-fA-F0-9]{2}[:|\-]? = make that final ?:? or ?-? character optional; since the last pair of characters won't be followed by anything, and we want them to be included, too; that's a chunk of 2 or 3 characters, so far
# ([a-fA-F0-9]{2}[:|\-]?){6} = find this type of chunk 6 times in a row

mac = re.search(r"([a-fA-F0-9]{2}[:|\-]?){6}", s).groups()[0] #LINUX VERSION ARP
mac = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s).groups()[0] #MAC VERSION ARP
print(mac)

我一直在寻找一些信息,但发现的内容似乎有点含糊.如果您知道任何可能对我有帮助的想法或研究途径,我将非常感激

I have looked for some information but what I have found seems a bit vague. If you know of any ideas or avenues of research that may help me I would be greatful

欢呼

克里斯

推荐答案

您无法直接获取子网外部计算机的MAC地址.

You can't directly get the MAC address of a machine outside your subnet.

网络管理应用程序的常见策略是使用

A common strategy for network management applications is to query machines that do have this information, such as the routers and switches connecting the machines, using SNMP. Routers have arp tables for the subnets to which they are directly connected (as they need this to do their job), and this information can be acquired from the router.

这个问题的答案可能有助于查找python库代码可帮助实现这一目标.

The answers to this question might help with finding python library code to help in this endeavor.

这篇关于跨网络IP的MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆