找传奇、传世资源到传世资源站!

修改本地DNS的方法

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

修改本地DNS的可执行文件,使用管理员权限打开from clipboard# -*- coding: UTF8 -*- import os import re import wmi import ctypes import sysSTD_OUTPUT_HANDLE = -11 # Windows CMD命令行 字体颜色定义 text colors FOREGROUND_DARKGREEN = 0x02 # dark green. FOREGROUND_RED = 0x0c # red. FOREGROUND_GREEN = 0x0a # green. FOREGROUND_BLUE = 0x09 # blue. FOREGROUND_DARKRED = 0x04 # dark red. # reset white def resetcolor(): set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) # 暗绿色 def darkgreen(mess): set_cmd_text_color(FOREGROUND_DARKGREEN) sys.stdout.write(mess) resetcolor() # 暗红色 def darkred(mess): set_cmd_text_color(FOREGROUND_DARKRED) sys.stdout.write(mess) resetcolor() # get handle std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) def set_cmd_text_color(color, handle=std_out_handle): bl = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) return bl def runcmd(cmd): # 执行cmd命令 tmp = os.popen(cmd) res = tmp.read() # 要用read()方法读取后才是文本对象 tmp.close() # 需将对象关闭 pattern = re.compile('. :$', re.M) ip_type = pattern.findall(res) result = [item.strip() for item in res.split("\n") if item.strip() != "" and (item in ip_type or "123.125.97.254" in item) or ( item in ip_type or "114.247.39.112" in item)] ip = str(result).replace('[', '').replace(']', '') return ip def checkip(hostip): # 检查ip是否正确 pat = re.compile(r'([0-9]{1,3})\.') r = re.findall(pat, hostip ".") if len(r) == 4 and len([x for x in r if int(x) >= 0 and int(x) <= 255]) == 4: return True else: return False def moddns(ip): wmis = wmi.WMI() # Windows管理规范 col = wmis.Win32_NetworkAdapterConfiguration(IPEnabled=True) # 遍历可用网络适配器数 if len(col) < 1: print("没有找到可用的网络适配器") exit() num = 0 adns = [ip] # 需要修改的DNS while num < len(col): obj = col[num] if obj.Description.find("Virtual") == -1 and obj.Description.find("VPN") == -1: # 排除虚拟机 # print(obj) # 打印网卡信息(网卡名称、路径、网关、IP地址、DNS、物理地址等信息) rtv = obj.SetDNSServerSearchOrder(DNSServerSearchOrder=adns) # DNS赋值 # print(obj.DNSServerSearchOrder) # 打印DNS信息 if rtv[0] == 0: os.system("ipconfig/flushdns") print("---------------------------------------------") print("以下为修改DNS成功后解析<epay.10010.com>结果,请注意检查核对") print("---------------------------------------------") darkgreen(runcmd("nslookup epay.10010.com")) darkgreen(obj.Description) darkgreen(u'修改DNS成功\n') # 打印网卡修改DNS成功 else: darkred(obj.Description) darkred(u'修改DNS失败\n') # 打印网卡修改DNS失败 num = 1 def dhcpdns(): wmis = wmi.WMI() # Windows管理规范 col = wmis.Win32_NetworkAdapterConfiguration(IPEnabled=True) # 遍历可用网络适配器数 if len(col) < 1: print("没有找到可用的网络适配器") exit() num = 0 while num < len(col): obj = col[num] if obj.Description.find("Virtual") == -1 and obj.Description.find("VPN") == -1: # 排除虚拟机 # print(obj) # 打印网卡信息(网卡名称、路径、网关、IP地址、DNS、物理地址等信息) rtv = obj.SetDNSServerSearchOrder() # print(obj.DNSServerSearchOrder) # 打印DNS信息 if rtv[0] == 0: os.system("ipconfig/flushdns") print("---------------------------------------------") print("以下为修改DNS成功后解析<epay.10010.com>结果,请注意检查核对") print("---------------------------------------------") darkgreen(runcmd("nslookup epay.10010.com")) darkgreen(obj.Description) darkgreen(u'自动获取dns成功\n') # 打印网卡修改DNS成功 else: darkred(obj.Description) darkred(u'自动获取dns失败\n') # 打印网卡修改DNS失败 num = 1 while True: print("---------------------------------------------") print("注意:修改DNS时,请先断开VPN,且以管理员身份运行") print("---------------------------------------------") print("-----请输入0/1/2/3/4-----\n" "0、解析当前DNS\n" "1、自动修改西红门dns\n" "2、自动获取\n" "3、手动修改西红门或硅谷dns\n" "4、退出") i = input("请输入:\n") if i == '0': os.system("ipconfig/flushdns") print("---------------------------------------------") print("以下为解析<epay.10010.com>结果,请注意检查核对") print("---------------------------------------------") print(runcmd("nslookup epay.10010.com")) elif i == '1': print("自动修改西红门DNS地址:222.88.49.150") dns = "222.88.49.150" moddns(dns) # 自动修改西红门DNS elif i == '2': print("自动获取DNS,默认为硅谷") dhcpdns() # 自动获取DNS,默认为硅谷 elif i == '3': dns = input("请输入西红门或硅谷DNS地址:\n" "西红门:24.154.1.4222.172.200.68(云南)、222.88.49.150(河南)\n" "硅谷:202.106.0.20(北京)、219.150.32.132(天津)\n") while True: if checkip(dns): moddns(dns) # 手动修改西红门或硅谷DNS break else: print("-----------------------") print("请注意检查输入DNS是否正确") print("-----------------------") dns = input("请输入西红门或硅谷DNS地址:\n" "西红门:24.154.1.4222.172.200.68(云南)、222.88.49.150(河南)\n" "硅谷:202.106.0.20(北京)、219.150.32.132(天津)\n") elif i == '4': quit() else: print("请重新输入!\n")

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复