caozhi00

mac地址的获取和修改

#!/usr/local/bin/python3

__author__ = \'曹植\'

import os
import random
from winreg import OpenKey, HKEY_LOCAL_MACHINE, QueryInfoKey, EnumKey, CloseKey, QueryValueEx, SetValueEx, REG_SZ, \
    KEY_ALL_ACCESS, KEY_WOW64_64KEY
import sys
import ctypes

mackey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"


def readipconfig():
    """
     # 读取ipconfig信息,列出当前网卡地址,主机名
    :return:
    """
    s = \'描述\'
    t = "物理地址"
    h = \'主机名\'
    macfact = {}
    description = []
    address = []
    for line in os.popen("ipconfig /all"):
        if h in line:
            macfact[\'host\'] = line.split(\':\')[1].strip()
        if s in line:
            description.append(line.split(\':\')[1].strip())
        if t in line:
            address.append(line.split(\':\')[1].strip())
    for des in description:
        macfact[des] = address[description.index(des)]
    print(\'macfact:\', macfact)
    return macfact


def readinfo(ethernetname):  # 读取当前网络设备并选择要修改的设备

    key = OpenKey(HKEY_LOCAL_MACHINE, mackey)
    countkey = QueryInfoKey(key)[0]
    keylist = []  # 获取{4D36E972-E325-11CE-BFC1-08002bE10318}子键列表
    mackeylist = []

    for i in range(int(countkey)):
        name = EnumKey(key, i)  # 获取子键名
        keylist.append(name)
    CloseKey(key)

    for t in keylist:
        mackey_zi = mackey + \'\\\' + t

        try:
            key = OpenKey(HKEY_LOCAL_MACHINE, mackey_zi)
            # print key
            value, type = QueryValueEx(key, "DriverDesc")
            # 列出有mac地址的网卡,及对应注册表中的编号
            if value in ethernetname:
                mackeylist.append(t)
                print(\'%s: %s  MAC:%s\' % (t, value, ethernetname[value]))
            else:
                pass
        except:
            value = \'None\'
    CloseKey(key)
    # print(keylist)
    print(mackeylist)
    judge = True
    while judge:
        d = input(\'请从上面选择您的网卡号(注意按格式填写每行冒号前的数字):\')
        if d in mackeylist:
            judge = False
        else:
            print("输入错误,重新输入!")
    return d


def modify_mac(newmac):  # 修改mac地址
    mackey_fix = mackey + \'\\\' + newmac
    # print(mackey_fix)
    if is_admin():
        key = OpenKey(HKEY_LOCAL_MACHINE, mackey_fix, 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS)
        name = \'mac\'
        inputmac = yield_mac(name)
        print(\'您输入的新MAC地址是%s\' % inputmac)
        SetValueEx(key, "NetworkAddress", 0, REG_SZ, inputmac)  # 设置注册表值
        print(\'MAC地址已修改成功,重启后生效。\')
        CloseKey(key)
    else:
        print(\'该用户没有权限\')


def modify_host():
    """
    修改主机名
    :param :
    :return:
    """
    path = \'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\'
    key = OpenKey(HKEY_LOCAL_MACHINE, path, 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS)
    name = \'host\'
    input_host = yield_mac(name)
    print(\'修改后的主机名为:\', input_host)
    SetValueEx(key, "NV HostName", 0, REG_SZ, input_host)


def yield_mac(n):
    """
    随机生成mac地址
    :return:
    """
    char_list = [\'0\', \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'A\', \'B\', \'C\', \'D\', \'E\', \'F\', \'a\', \'b\', \'c\', \'d\',
                 \'e\',
                 \'f\']
    if n == \'mac\':
        char = \'\'.join(random.sample(char_list, 12))
    else:
        char = \'\'.join(random.sample(char_list, 6))
    return char


def is_admin():
    """
    Checks if the script is running with administrative privileges.
    Returns True if is running as admin, False otherwise.
    :return:
    """
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False


if __name__ == \'__main__\':
    if is_admin():
        if sys.platform == "win32":
            iplist = readipconfig()
            m = readinfo(iplist)
            modify_host()
            modify_mac(m)
        else:
            print("不支持当前系统")
    else:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

  若出现访问被拒绝的问题,可以在管理员windows powershell下运行代码。

分类:

技术点:

相关文章:

  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-12-12
  • 2021-04-30
猜你喜欢
  • 2021-12-02
  • 2021-11-19
  • 2022-12-23
  • 2021-11-27
  • 2021-12-19
  • 2021-07-27
相关资源
相似解决方案