目标是收集连接的本地NIC的MAC地址, 不是所有本地NIC的列表 :)
使用socket
和connect (to_a_website)
,
我可以使用getsockname()
来获取IP,
用于连接到Internet。
但是从IP中我怎样才能获得本地网卡的MAC地址?
问题的主要原因是是否有多个NIC。
答案 0 :(得分:2)
正如vartec建议netifaces
应该从IP-> iface开始工作得好:
import netifaces as nif
def mac_for_ip(ip):
'Returns a list of MACs for interfaces that have given IP, returns None if not found'
for i in nif.interfaces():
addrs = nif.ifaddresses(i)
try:
if_mac = addrs[nif.AF_LINK][0]['addr']
if_ip = addrs[nif.AF_INET][0]['addr']
except IndexError, KeyError: #ignore ifaces that dont have MAC or IP
if_mac = if_ip = None
if if_ip == ip:
return if_mac
return None
测试:
>>> mac_for_ip('169.254.90.191')
'2c:41:38:0a:94:8b'
答案 1 :(得分:1)
您无法检索外部IP的MAC地址。
请参阅how to get mac address of external IP in C#处的讨论,以获得更多说明。
答案 2 :(得分:1)
答案 3 :(得分:0)
这样做的一种原始方法是使用操作系统上可用的命令行工具。使用subprocess
模块(不是os.system()
!)运行该工具,收集输出并解析它。
在Windows上,您需要的命令是ipconfig /all
。
在大多数Unices上,包括Linux,OSX和BSD,它是ifconfig
。
有一种更好的方法可以在不使用命令行实用程序的情况下执行此操作,但我不知道... 尚未。
Windows XP上ipconfig /all
的示例输出:
D:\Documents and Settings\LAYip>ipconfig /all
Windows IP Configuration
Host Name . . . . . . . . . . . . : <redacted>
Primary Dns Suffix . . . . . . . : <redacted>
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : <redacted>
<redacted>
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : <redacted>
Description . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Con
nection #2
Physical Address. . . . . . . . . : 5C-26-0A-60-8D-C7
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : xxx.xxx.28.29
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : xxx.xxx.28.254
DHCP Server . . . . . . . . . . . : xxx.xxx.23.13
DNS Servers . . . . . . . . . . . : xxx.xxx.23.13
xxx.xxx.23.11
Lease Obtained. . . . . . . . . . : Thursday, 12 April 2012 9:14:41 AM
Lease Expires . . . . . . . . . . : Friday, 20 April 2012 9:14:41 AM
Ethernet adapter VirtualBox Host-Only Network:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
Physical Address. . . . . . . . . : 08-00-27-00-28-E6
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.56.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Linux下ifconfig
的输出:
lws@helios:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:25:22:db:8c:b6
inet addr:10.1.1.2 Bcast:10.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::225:22ff:fedb:8cb6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:322333 errors:0 dropped:0 overruns:0 frame:0
TX packets:296952 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:40005876 (40.0 MB) TX bytes:162343969 (162.3 MB)
Interrupt:40 Base address:0x4000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:362 errors:0 dropped:0 overruns:0 frame:0
TX packets:362 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:31806 (31.8 KB) TX bytes:31806 (31.8 KB)
答案 4 :(得分:0)
你无法从socket中捕获mac地址。你需要一个ethernet frame,它可以在tcp处理链的最低层找到。你需要监视(捕获)你的网络流量,找到一些数据包通过解析数据包的标头。并从中提取所需的信息,如mac地址。
这是useful code span,可以帮助您做到这一点。
答案 5 :(得分:0)
获取系统mac id的另一种迂回方式是使用ping命令ping系统的名称,然后针对被ping的ip地址执行arp -a请求。这样做的垮台你需要将ping响应写入python的内存并执行readline操作来检索ip地址,然后在写入系统名称,ip地址和编写系统名称,ip地址时将相应的arp数据写入内存有问题的机器的mac id到显示器或测试文件。
我尝试做类似系统验证检查的事情,以提高测试程序的自动化程度,目前脚本在python中。