软件星级:3分
DNS资源来自于CloudXNS,官网也有相应的修改软件,但是没有定时修改功能,所以我用python写了个脚本获取dns并每隔一个小时就修改一次。 补充一下:运行后不是马上就能修改dns,因为一次获取了十个dns,要通过ping来比较哪个延时最少,最后选出两个最好的dns。比较需要时间,大概运行40s后就会修改dns.
开源一下,就七十多行代码:
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
#author:trust
import wmi
import json
import urllib2
import os
import string
import time
import sys
def http_get():
try:
url = 'http://tools.cloudxns.net/Api/getLdns'
response = urllib2.urlopen(url)
return response.read()
except urllib2.URLError, e:
pass
def readJson():
dnsAdd=[]
s=json.loads( http_get())
status=s['status'];
if status!='success':
return dnsAdd
m_list=s['data']
m_dict=m_list[0]
m_list_info=m_dict['info']
for m_dirt_vip in m_list_info:
dnsAdd.append(m_dirt_vip['vip'])
return dnsAdd
def ChooseDNS():
DNSDIR={}
DNSADDR=readJson()
for dns_child in DNSADDR:
tempStr='ping %s'%(dns_child,)
p = os.popen(tempStr)
strs = p.read()
index1 = strs.rfind('ms')
if index1==-1:
continue
index2 = strs.rfind('=')
item = strs[index2 + 1:index1]
item = item.strip()
time = string.atoi(item)
DNSDIR[dns_child]=time
DNSDIR = sorted(DNSDIR.items(), key=lambda d: d[1])
return [DNSDIR[0][0],DNSDIR[1][0]]
def ModifyDNS():
wmiService = wmi.WMI()
colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled=True)
if len(colNicConfigs) < 1:
exit()
arrDNSServers = ChooseDNS()
for objNicConfig in colNicConfigs:
intReboot = 0
returnValue = objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder=arrDNSServers)
if returnValue[0] == 0 or returnValue[0] == 1:
intReboot += returnValue[0]
else:
returnValue = objNicConfig.SetDNSServerSearchOrder()
# exit()
if intReboot > 0:
print 'need reboot'
if __name__ == "__main__":
if sys.executable.endswith("pythonw.exe"):
sys.stdout = open(os.devnull, "w");
sys.stderr = open(os.path.join(os.getenv("TEMP"), "stderr-" + os.path.basename(sys.argv[0])), "w")
while 1:
ModifyDNS()
time.sleep(3600)
sys.exit() 上面的代码只能用于装有python环境的电脑用,为了照顾大部分编程小白,所以准备了exe版本的程序,只要双击就能打开了,打开后没有界面的,可以在任务管理器里面看到“熊本熊DNS修改器.exe”进程。 如果想做成开机启动,可以右键“熊本熊DNS修改器.exe”,新建快捷方式,把快捷方式复制粘贴到C:ProgramDataMicrosoftWindowsStart MenuProgramsStartup里面。