Arm*_*her 144
Python 2.5包含一个uuid实现(至少在一个版本中)需要mac地址.您可以轻松地将mac查找功能导入到您自己的代码中:
from uuid import getnode as get_mac
mac = get_mac()
Run Code Online (Sandbox Code Playgroud)
返回值是mac地址为48位整数.
syn*_*tel 80
Linux下的这个问题的纯python解决方案,用于获取特定本地接口的MAC,最初由vishnubob发布为评论,并在此活动状态配方中由Ben Mackey改进
#!/usr/bin/python
import fcntl, socket, struct
def getHwAddr(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ':'.join(['%02x' % ord(char) for char in info[18:24]])
print getHwAddr('eth0')
Run Code Online (Sandbox Code Playgroud)
cam*_*lan 30
netifaces是一个很好的模块,用于获取mac地址(和其他地址).它是跨平台的,比使用socket或uuid更有意义.
>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0', 'tun2']
>>> netifaces.ifaddresses('eth0')[netifaces.AF_LINK]
[{'addr': '08:00:27:50:f2:51', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]
Run Code Online (Sandbox Code Playgroud)
mha*_*wke 21
另外一件事你应该注意的是,uuid.getnode()可以通过返回一个随机的48位数来伪造MAC地址,这可能不是你所期望的.此外,没有明确指示MAC地址已伪造,但您可以通过调用getnode()两次并查看结果是否变化来检测它.如果两个呼叫都返回相同的值,则您拥有MAC地址,否则您将获得伪造的地址.
>>> print uuid.getnode.__doc__
Get the hardware address as a 48-bit positive integer.
The first time this runs, it may launch a separate program, which could
be quite slow. If all attempts to obtain the hardware address fail, we
choose a random 48-bit number with its eighth bit set to 1 as recommended
in RFC 4122.
Run Code Online (Sandbox Code Playgroud)
Jul*_*urt 17
有时我们有多个网络接口.
查找特定接口的mac地址的简单方法是:
def getmac(interface):
try:
mac = open('/sys/class/net/'+interface+'/address').readline()
except:
mac = "00:00:00:00:00:00"
return mac[0:17]
Run Code Online (Sandbox Code Playgroud)
调用方法很简单
myMAC = getmac("wlan0")
Run Code Online (Sandbox Code Playgroud)
使用我的答案:https://stackoverflow.com/a/18031868/2362361
重要的是要知道你想要MAC的iface,因为许多可以存在(蓝牙,几个nics等).
当您使用netifaces(在PyPI中提供)知道您需要MAC的iface的IP时,这可以完成这项工作:
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
Run Code Online (Sandbox Code Playgroud)
测试:
>>> mac_for_ip('169.254.90.191')
'2c:41:38:0a:94:8b'
Run Code Online (Sandbox Code Playgroud)
您可以使用跨平台的psutil进行此操作:
import psutil
nics = psutil.net_if_addrs()
print [j.address for j in nics[i] for i in nics if i!="lo" and j.family==17]
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您不介意依赖的话,跨平台getmac包将适用于此。它适用于 Python 2.7+ 和 3.4+。它将尝试许多不同的方法,直到获得地址或返回 None。
from getmac import get_mac_address
eth_mac = get_mac_address(interface="eth0")
win_mac = get_mac_address(interface="Ethernet 3")
ip_mac = get_mac_address(ip="192.168.0.1")
ip6_mac = get_mac_address(ip6="::1")
host_mac = get_mac_address(hostname="localhost")
updated_mac = get_mac_address(ip="10.0.0.1", network_request=True)
Run Code Online (Sandbox Code Playgroud)
免责声明:我是该包的作者。
更新(2019 年 1 月 14 日):该软件包现在仅支持 Python 2.7+ 和 3.4+。如果您需要使用较旧的 Python(2.5、2.6、3.2、3.3),您仍然可以使用旧版本的包。
| 归档时间: |
|
| 查看次数: |
134285 次 |
| 最近记录: |