Python3根据IP地址获取MAC地址,python3ip获取mac,这是一种笨方法,是根据系
Python3根据IP地址获取MAC地址,python3ip获取mac,这是一种笨方法,是根据系
这是一种笨方法,是根据系统内置命令去获取的。我只是抛砖引玉,希望有人给我分享更好的代码,比如通过ARP协议去获取MAC地址?
#!/usr/bin/env python3# -*- coding: utf-8 -*-import osimport platformimport reclass IP2MAC: def __init__(self): self.patt_mac = re.compile('([a-f0-9]{2}[-:]){5}[a-f0-9]{2}', re.I) def getMac(self, ip): sysstr = platform.system() if sysstr == 'Windows': macaddr = self.__forWin(ip) elif sysstr == 'Linux': macaddr = self.__forLinux(ip) else: macaddr = None return macaddr or '00-00-00-00-00-00' def __forWin(self, ip): os.popen('ping -n 1 -w 500 {} > nul'.format(ip)) macaddr = os.popen('arp -a {}'.format(ip)) macaddr = self.patt_mac.search(macaddr.read()) if macaddr: macaddr = macaddr.group() else: macaddr = None return macaddr def __forLinux(self, ip): os.popen('ping -nq -c 1 -W 500 {} > /dev/null'.format(ip)) result = os.popen('arp -an {}'.format(ip)) result = self.patt_mac.search(result.read()) return result.group() if result else Noneif __name__ =='__main__': g = IP2MAC() print(g.getMac('192.168.112.80'), g.getMac('192.168.1.254'), sep='\\n')#该片段来自于http://byrx.net
相关内容
- wxpython不规则窗体,wxpython窗体,#!/usr/bin/e
- 微信服务端模拟,信服务端模拟,最近开发微信接口,配
- 删除列表中重复的元素,删除列表重复元素,lists=[20,1
- python中随机数的查找,python随机数查找,1. random.ra
- wxpython与logging模块结合显示实时日志,wxpythonlogging,#!
- open phone's app,openphoneapp,def open_app
- 装饰器 decorator,装饰decorator,装饰器的简单应用方法A和
- wxpython tab标签式 gui实现方法,wxpythongui,import wxcla
- quick sort,quick,class QuickS
- python实现的简单LR(1)状态表,pythonlr,只是四则混合运算
评论关闭