参考
https://mp.weixin.qq.com/s/TQRKJBIrA75lOK4IcPrERQ
import os
import win32api
import win32con
def msgbox(msg, title='提醒'):
win32api.MessageBox(0, msg, title, win32con.MB_OK)
def local_ip_mac():
output = os.popen('ipconfig /all')
for line in output:
if '物理地址.' in line:
mac = line.split(':')[1].strip()
if 'IPV4' in line.upper() and '(' in line:
ip = line.split(':')[1].split('(')[0].strip()
return [ip, mac] if ip and mac else None
def lan_ip_mac():
ip_mac_list = []
output = os.popen('arp -a')
for line in output:
if '动态' in line:
ip, mac, _ = line.strip().split()
ip_mac_list.append([ip, mac])
return ip_mac_list
if __name__ == '__main__':
ip_mac_result = lan_ip_mac()
ip_mac_result.append(local_ip_mac())
print(ip_mac_result)
# 结果写入文本
txt = 'ip_mac.txt'
out_txt = '\n'.join(['\t'.join(i) for i in ip_mac_result])
with open(txt, 'w') as f:
f.write(out_txt)
msgbox(f'提取结果已保存到:{txt}')