#!/usr/bin/python3
import socket
import netifaces
import tkinter as tk
from tkinter import messagebox
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80)) #这是DNS服务器的 UDP 80端口
local_ip = s.getsockname()[0]
s.close()
return local_ip
local_ip = get_local_ip()
# 获取所有网络接口
interfaces = netifaces.interfaces()
info = ""
for interface in interfaces:
addrs = netifaces.ifaddresses(interface)
# print(addrs)
if netifaces.AF_INET in addrs: # 检查IPv4地址
ip = addrs[netifaces.AF_INET][0]['addr']
if(ip == local_ip):
# print("相等")
mac = addrs[netifaces.AF_LINK][0]['addr']
mac = mac.replace(":","-")
mac = mac.upper()
info=f"IP地址: {ip} MAC地址: {mac}"
if info == "":
messagebox.showinfo("计算机IP和MAC地址", f"未连网,未获取到")
else:
messagebox.showinfo("计算机IP和MAC地址", f" {info}")