[Python] 纯文本查看 复制代码 import ctypes
import sys
import subprocess
import time
def is_admin():
"""检查是否以管理员身份运行"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if not is_admin():
print("正在尝试以管理员身份运行...")
subprocess.run(["powershell", "Start-Process", "python", f'"{sys.argv[0]}"', "-Verb", "RunAs"], shell=True)
sys.exit()
# 现在是管理员身份
def set_windows_network(interface, ip, subnet, gateway, dns):
"""修改 Windows 本地 IP、子网掩码、网关,并设置 DNS"""
try:
print(f"正在修改 {interface} 的 IP 地址...")
subprocess.run(f'netsh interface ip set address name="{interface}" static {ip} {subnet} {gateway}', shell=True, check=True)
print(f"正在设置 {interface} 的 DNS 为 {dns}...")
subprocess.run(f'netsh interface ip set dns name="{interface}" static {dns}', shell=True, check=True)
# 禁用网卡
print(f"正在禁用 {interface}...")
subprocess.run(f'netsh interface set interface "{interface}" admin=disable', shell=True, check=True)
time.sleep(3) # 等待 3 秒
# 启用网卡
print(f"正在启用 {interface}...")
subprocess.run(f'netsh interface set interface "{interface}" admin=enable', shell=True, check=True)
print(f"成功修改 {interface} 的网络配置:")
print(f"IP 地址:{ip}")
print(f"子网掩码:{subnet}")
print(f"默认网关:{gateway}")
print(f"DNS 服务器:{dns}")
except subprocess.CalledProcessError as e:
print(f"修改网络配置失败: {e}")
# 你的网络配置
INTERFACE_NAME = "WLAN" # 确保你的无线网卡名称是 WLAN
IP_ADDRESS = "192.168.5.108"
SUBNET_MASK = "255.255.255.0"
GATEWAY = "192.168.5.1"
DNS_SERVER = "114.114.114.114"
# 执行修改
set_windows_network(INTERFACE_NAME, IP_ADDRESS, SUBNET_MASK, GATEWAY, DNS_SERVER)
我自己执行了可以,用chatgpt写的 |