Python3修改电脑mac地址

Python3修改电脑mac地址说明 仅供学习使用 请勿用于非法用途 若有侵权 请联系博主删除作者 zhu 博客 https blog csdn net zhu 一 查看电脑 mac 方式 1 ipconfig all 查看本地连接物理地址 注意 是本地连接的物理地址 操作如下 方式 2 依次进入控制面板网络和 Internet 网络连接 注意 是本地连接的物理地址 右键本地连接属性配置高级本地管理地址 输入要修改的 mac

说明:仅供学习使用,请勿用于非法用途,若有侵权,请联系博主删除

作者:zhu

博客:https://blog.csdn.net/zhu

一、查看电脑mac

方式1: ipconfig /all 查看 本地连接 物理地址,注意:是 本地连接 的物理地址。

操作如下:

Python3修改电脑mac地址

Python3修改电脑mac地址

方式2:依次进入 控制面板–网络和 Internet–网络连接,注意:是 本地连接 的物理地址。

Python3修改电脑mac地址

右键 本地连接 –属性–配置–高级–本地管理地址,输入要修改的mac地址,确定。

Python3修改电脑mac地址

Python3修改电脑mac地址

二、手动修改电脑mac

在上述 方法2 的基础上,手动修改成新mac地址(3CEBE),确认保存即可,查看检查确认。

Python3修改电脑mac地址

验证是否生效:

Python3修改电脑mac地址

三、Python修改电脑 本地连接 mac地址

运行效果:

本地连接 Intel(R) 82579LM Gigabit Network Connection

完整代码:

# @Time : 2021/6/24 22:30 # @Author : Tesla # @File : 13.修改本地连接mac地址.py # @Software: PyCharm # Csdn : https://blog.csdn.net/zhu import ctypes import platform import re import subprocess import sys import winreg class SetMac(object): """ 修改 本地连接 mac地址 """ def __init__(self): # regex to MAC address like 00-00-00-00-00-00 or 00:00:00:00:00:00 or # 000000000000 self.MAC_ADDRESS_RE = re.compile(r""" ([0-9A-F]{1,2})[:-]? ([0-9A-F]{1,2})[:-]? ([0-9A-F]{1,2})[:-]? ([0-9A-F]{1,2})[:-]? ([0-9A-F]{1,2})[:-]? ([0-9A-F]{1,2}) """, re.I | re.VERBOSE) # re.I: case-insensitive matching. re.VERBOSE: just look nicer. self.WIN_REGISTRY_PATH = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}" def is_admin(self): """ is user an admin? :return: """ if ctypes.windll.shell32.IsUserAnAdmin() == 0: print('Sorry! You should run this with administrative privileges if you want to change your MAC address.') sys.exit() else: print('admin') def get_macinfos(self): """ 查看所有mac信息 :return: """ print('=' * 50) mac_info = subprocess.check_output('GETMAC /v /FO list', stderr=subprocess.STDOUT) mac_info = mac_info.decode('gbk') print('Your MAC address:\n', mac_info) def get_target_device(self): """ 返回 本地连接 网络适配器 :return: """ mac_info = subprocess.check_output('GETMAC /v /FO list', stderr=subprocess.STDOUT) mac_info = mac_info.decode('gbk') search = re.search(r'(本地连接)\s+网络适配器: (.+)\s+物理地址:', mac_info) target_name, target_device = (search.group(1), search.group(2).strip()) if search else ('', '') if not all([target_name, target_device]): print('Cannot find the target device') sys.exit() print(target_name, target_device) return target_device def set_mac_address(self, target_device, new_mac): """ 设置新mac地址 :param target_device: 本地连接 网络适配器 :param new_mac: 新mac地址 :return: """ if not self.MAC_ADDRESS_RE.match(new_mac): print('Please input a correct MAC address') return # Locate adapter's registry and update network address (mac) reg_hdl = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) key = winreg.OpenKey(reg_hdl, self.WIN_REGISTRY_PATH) info = winreg.QueryInfoKey(key) # Find adapter key based on sub keys adapter_key = None adapter_path = None target_index = -1 for index in range(info[0]): subkey = winreg.EnumKey(key, index) path = self.WIN_REGISTRY_PATH + "\\" + subkey if subkey == 'Properties': break # Check for adapter match for appropriate interface new_key = winreg.OpenKey(reg_hdl, path) try: adapterDesc = winreg.QueryValueEx(new_key, "DriverDesc") if adapterDesc[0] == target_device: adapter_path = path target_index = index break else: winreg.CloseKey(new_key) except (WindowsError) as err: if err.errno == 2: # register value not found, ok to ignore pass else: raise err if adapter_path is None: print('Device not found.') winreg.CloseKey(key) winreg.CloseKey(reg_hdl) return # Registry path found update mac addr adapter_key = winreg.OpenKey(reg_hdl, adapter_path, 0, winreg.KEY_WRITE) winreg.SetValueEx(adapter_key, "NetworkAddress", 0, winreg.REG_SZ, new_mac) winreg.CloseKey(adapter_key) winreg.CloseKey(key) winreg.CloseKey(reg_hdl) # Adapter must be restarted in order for change to take affect # print 'Now you should restart your netsh' self.restart_adapter(target_index, target_device) def restart_adapter(self, target_index, target_device): """ Disables and then re-enables device interface """ if platform.release() == 'XP': # description, adapter_name, address, current_address = find_interface(device) cmd = "devcon hwids =net" try: result = subprocess.check_output(cmd, stderr=subprocess.STDOUT) except FileNotFoundError: raise query = '(' + target_device + '\r\n\s*.*:\r\n\s*)PCI\\\\(([A-Z]|[0-9]|_|&)*)' query = query.encode('ascii') match = re.search(query, result) cmd = 'devcon restart "PCI\\' + str(match.group(2).decode('ascii')) + '"' subprocess.check_output(cmd, stderr=subprocess.STDOUT) else: cmd = "wmic path win32_networkadapter where index=" + str(target_index) + " call disable" subprocess.check_output(cmd) cmd = "wmic path win32_networkadapter where index=" + str(target_index) + " call enable" subprocess.check_output(cmd) def run(self): self.is_admin() self.get_macinfos() target_device = self.get_target_device() self.set_mac_address(target_device, '3CEBE') self.get_macinfos() if __name__ == '__main__': set_mac = SetMac() set_mac.run() 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233104.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号