Search for host with MAC-address using Python

2020-07-09 06:36发布

I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?

8条回答
男人必须洒脱
2楼-- · 2020-07-09 06:37

Depends on your platform. If you're using *nix, you can use the 'arp' command to look up the mac address for a given IP (assuming IPv4) address. If that doesn't work, you could ping the address and then look, or if you have access to the raw network (using BPF or some other mechanism), you could send your own ARP packets (but that is probably overkill).

查看更多
叼着烟拽天下
3楼-- · 2020-07-09 06:44

You would want to parse the output of 'arp', but the kernel ARP cache will only contain those IP address(es) if those hosts have communicated with the host where the Python script is running.

ifconfig can be used to display the MAC addresses of local interfaces, but not those on the LAN.

查看更多
混吃等死
4楼-- · 2020-07-09 06:45

If you want a pure Python solution, you can take a look at Scapy to craft packets (you need to send ARP request, and inspect replies). Or if you don't mind invoking external program, you can use arping (on Un*x systems, I don't know of a Windows equivalent).

查看更多
神经病院院长
5楼-- · 2020-07-09 06:46

You need ARP. Python's standard library doesn't include any code for that, so you either need to call an external program (your OS may have an 'arp' utility) or you need to build the packets yourself (possibly with a tool like Scapy.

查看更多
放荡不羁爱自由
6楼-- · 2020-07-09 06:48

It seems that there is not a native way of doing this with Python. Your best bet would be to parse the output of "ipconfig /all" on Windows, or "ifconfig" on Linux. Consider using os.popen() with some regexps.

查看更多
乱世女痞
7楼-- · 2020-07-09 06:53

I don't think there is a built in way to get it from Python itself.

My question is, how are you getting the IP information from your network?

To get it from your local machine you could parse ifconfig (unix) or ipconfig (windows) with little difficulty.

查看更多
登录 后发表回答