如何使用Python在Windows中以编程方式更改DNS服务器?

时间:2011-09-14 11:45:50

标签: python windows dns

我想以编程方式更改DNS服务器。我不想构建DNS服务器,我只想自动更改主DNS服务器和辅助DNS服务器。这项工作需要在Python中完成。

2 个答案:

答案 0 :(得分:6)

您可以使用命令行工具调用os.system。

import os
os.system('netsh interface ip set dns "Local Area Connection" static 192.168.0.200')

答案 1 :(得分:0)

import os
# The first thing you need to import os and identify is the name of the network interface you want to modify.
# You can find the names of all network interfaces by running the following command:
os.system('netsh interface ipv4 show interfaces')
# for me its "Wi-Fi"
# For the primary DNS server run:
os.system('netsh interface ip set dns name="Wi-Fi" static 185.37.37.37')
# For the secondary DNS server run:
os.system('netsh interface ip add dns name="Wi-Fi"  185.37.39.39 index=2')
# whene you'r done with the DNS server run :
os.system('netsh interface ip set dnsservers name="Wi-Fi" source=dhcp')
#keep in mind  that you need administrator privilege