在日常工作中,我们可能需要频繁地在不同网络环境之间切换,比如在办公室使用静态IP配置,而在家中使用自动获取IP(DHCP)。为了简化这个过程,我们可以编写一个Python脚本,自动实现网卡在静态IP和DHCP模式之间的切换。
在这篇博客中,我将展示如何通过Python脚本实现这一功能,并且包含两个主要的部分:
将网络配置切换为静态IP。
将网络配置切换为自动获取IP和DNS(即DHCP模式)。
为什么要使用Python脚本自动化网络配置?
手动切换网络配置不仅耗时,还容易出错。通过编写Python脚本,你可以:
快速切换:只需运行脚本即可在不同的网络配置之间快速切换。
减少错误:预先定义好的配置,避免手动输入错误。
提高效率:尤其是在需要频繁切换网络的环境中,脚本化操作可以节省大量时间。
脚本功能一:设置静态IP配置
首先,我们来看如何使用Python脚本将网络适配器(如Wi-Fi)配置为静态IP。
静态IP配置参数如下:
IP地址:
192.168.1.211子网掩码:
255.255.255.0默认网关:
192.168.1.251首选DNS服务器:
192.168.1.251备用DNS服务器: 无
以下是Python脚本的实现:
python复制代码
import subprocess
def set_static_ip(network_interface, ip_address, subnet_mask, gateway, primary_dns, secondary_dns=""):
try:
# 设置静态IP地址
subprocess.run(['netsh', 'interface', 'ip', 'set', 'address', network_interface, 'static',
ip_address, subnet_mask, gateway], check=True)
# 设置主DNS服务器
subprocess.run(['netsh', 'interface', 'ip', 'set', 'dns', network_interface, 'static',
primary_dns], check=True)
# 如果有备用DNS服务器,则设置备用DNS服务器
if secondary_dns:
subprocess.run(['netsh', 'interface', 'ip', 'add', 'dns', network_interface,
secondary_dns, 'index=2'], check=True)
print(f"Successfully set {network_interface} to static IP {ip_address} with DNS {primary_dns} and {secondary_dns}.")
except subprocess.CalledProcessError as e:
print(f"Failed to set static IP and DNS for {network_interface}. Error: {e}")
脚本功能二:设置为自动获取IP和DNS(DHCP)
接下来,我们来看如何通过Python脚本将网络适配器设置为自动获取IP地址和DNS。
以下是实现这一功能的Python脚本:
import subprocess
def set_dhcp(network_interface):
try:
# 设置IP地址为自动获取
subprocess.run(['netsh', 'interface', 'ip', 'set', 'address', network_interface, 'dhcp'], check=True)
# 设置DNS为自动获取
subprocess.run(['netsh', 'interface', 'ip', 'set', 'dns', network_interface, 'dhcp'], check=True)
print(f"Successfully set {network_interface} to obtain IP and DNS automatically.")
except subprocess.CalledProcessError as e:
print(f"Failed to set DHCP for {network_interface}. Error: {e}")
综合使用:一键切换静态IP和DHCP模式
现在,我们可以将以上两个功能整合到一个脚本中,通过简单的选择来切换网络配置。
import subprocess
def set_static_ip(network_interface, ip_address, subnet_mask, gateway, primary_dns, secondary_dns=""):
try:
subprocess.run(['netsh', 'interface', 'ip', 'set', 'address', network_interface, 'static',
ip_address, subnet_mask, gateway], check=True)
subprocess.run(['netsh', 'interface', 'ip', 'set', 'dns', network_interface, 'static',
primary_dns], check=True)
if secondary_dns:
subprocess.run(['netsh', 'interface', 'ip', 'add', 'dns', network_interface,
secondary_dns, 'index=2'], check=True)
print(f"Successfully set {network_interface} to static IP {ip_address} with DNS {primary_dns} and {secondary_dns}.")
except subprocess.CalledProcessError as e:
print(f"Failed to set static IP and DNS for {network_interface}. Error: {e}")
def set_dhcp(network_interface):
try:
subprocess.run(['netsh', 'interface', 'ip', 'set', 'address', network_interface, 'dhcp'], check=True)
subprocess.run(['netsh', 'interface', 'ip', 'set', 'dns', network_interface, 'dhcp'], check=True)
print(f"Successfully set {network_interface} to obtain IP and DNS automatically.")
except subprocess.CalledProcessError as e:
print(f"Failed to set DHCP for {network_interface}. Error: {e}")
if __name__ == "__main__":
network_interface = "Wi-Fi" # 你可以根据需要修改为 "Ethernet" 或其他适配器名称
# 让用户选择要设置的模式
mode = input("选择网络配置模式: 1. 静态IP 2. 自动获取 (DHCP): ")
if mode == "1":
ip_address = "192.168.1.211"
subnet_mask = "255.255.255.0"
gateway = "192.168.1.251"
primary_dns = "192.168.1.251"
secondary_dns = "" # 如果有备用DNS,可以在此填入IP地址
set_static_ip(network_interface, ip_address, subnet_mask, gateway, primary_dns, secondary_dns)
elif mode == "2":
set_dhcp(network_interface)
else:
print("无效的选择,请选择 1 或 2。")
如何运行脚本?
准备工作:
将上述代码保存为Python文件,如
network_switch.py。确保在你的计算机上安装了Python环境。
运行脚本:
以管理员权限打开命令提示符。
导航到脚本所在的目录并运行脚本:
python network_switch.py运行后,选择你需要的网络配置模式:输入
1为静态IP,输入2为DHCP。
自定义配置:
你可以根据自己的网络环境,修改脚本中的IP地址、子网掩码、网关、DNS等参数。
总结
通过这个Python脚本,你可以轻松地在静态IP和DHCP模式之间切换网络配置,再也不用手动在Windows的网络设置界面中繁琐地输入各项参数了。这种方法不仅方便了日常操作,也为频繁切换网络环境的用户提供了极大的便利。
如果你是网络管理员或需要在不同网络环境中频繁切换的用户,这个脚本将是你不可或缺的工具。你还可以根据实际需求对脚本进行进一步的扩展,例如添加更多网络配置选项或支持多种网卡配置。