1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import subprocess import time
def set_dns(dns_primary, dns_secondary): try: connection_name = subprocess.check_output(['nmcli', '-t', '-f', 'NAME', 'con', 'show','--active']).decode().strip() subprocess.run(['nmcli', 'con', 'mod', connection_name, 'ipv4.dns', f'{dns_primary},{dns_secondary}']) subprocess.run(['nmcli', 'con', 'down', connection_name]) subprocess.run(['nmcli', 'con', 'up', connection_name]) subprocess.run(['systemd-resolve', '--flush', 'caches']) time.sleep(10) try: dns_info = subprocess.check_output(['nmcli'], text=True) if dns_primary in dns_info and dns_secondary in dns_info: print("设置DNS配置成功") except subprocess.CalledProcessError as e: print(f"设置DNS配置失败: {e}") return False except subprocess.CalledProcessError as e: print(f"设置DNS配置失败: {e}") return False
if __name__ == "__main__": dns_primary = "114.114.114.114" dns_secondary = "8.8.8.8" set_dns(dns_primary, dns_secondary) input("按任意键退出...") # pyinstaller --onefile -c --uac-admin --noconfirm setDNS.py
|