Python根据IP地址获取MAC地址
生活随笔
收集整理的這篇文章主要介紹了
Python根据IP地址获取MAC地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python3根據IP地址獲取MAC地址(不能獲取本機IP,可以獲取與本機同局域網設備IP的MAC)
main.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import platform import reclass IP2MAC:"""Python3根據IP地址獲取MAC地址(不能獲取本機IP,可以獲取與本機同局域網設備IP的MAC)"""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 = Nonereturn 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 = Nonereturn macaddrdef __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.2.105'))關注公眾號,獲取更多資料
總結
以上是生活随笔為你收集整理的Python根据IP地址获取MAC地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MITK医学Python开发入门详细版
- 下一篇: 操作系统(王道笔记第二章)