# 获取网关以及其MAC地址
import netifaces
import time

# 获取默认网关的IP地址
def get_default_gateway():
    gws = netifaces.gateways()
    return gws['default'][netifaces.AF_INET][0]

# 获取默认网关地址
body+="\r\n"
gateway_ip = get_default_gateway()
print(f"网关地址是:{gateway_ip}")
body+=f"网关地址是:{gateway_ip}"
body+="\r\n"

try:
    from scapy.all import ARP, Ether, srp
    def get_mac(ip):
        arp_request = ARP(pdst=ip)
        broadcast = Ether(dst="ff:ff:ff:ff:ff:ff")
        arp_request_broadcast = broadcast / arp_request
        answered_list = srp(arp_request_broadcast, timeout=2, verbose=False)[0]
        return answered_list[0][1].hwsrc if answered_list else None

    # 获取网关mac地址
    ip = gateway_ip  # 替换为目标IP
    mac = get_mac(ip)
    if mac:
        print(f"网关MAC地址是: {mac}")
        body += f"网关MAC地址是: {mac}"
        body += "\r\n"
    else:
        print(f"无法找到网关的MAC地址")
        body += f"无法找到网关的MAC地址"
        body += "\r\n"
except:
    print("进行此项测试,需要操作系统管理员权限。可能当前并没有权限。")
    body+="进行此项测试,需要操作系统管理员权限。可能当前并没有权限。"
    body+="\r\n"
最后修改:2024 年 05 月 11 日
如果觉得我的文章对你有用,请随意赞赏