在windows中使用python修改ip地址,需要WMI (Windows Management Instrumentation )模块:

pip install WMI

设置指定的ip地址:

import wmi

# Obtain network adaptors configurations
nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)

# First network adaptor
nic = nic_configs[0]

# IP address, subnetmask and gateway values should be unicode objects
ip = u'192.168.0.11'
subnetmask = u'255.255.255.0'
gateway = u'192.168.0.1'

# Set IP address, subnetmask and default gateway
# Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed
nic.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
nic.SetGateways(DefaultIPGateway=[gateway])

修改为通过DHCP自动获取ip:

import wmi

# Obtain network adaptors configurations
nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)

# First network adaptor
nic = nic_configs[0]

# Enable DHCP
nic.EnableDHCP()

需要注意的是,在实际使用过程中,需要检查 EnableStatic(), SetGateways() and EnableDHCP() 的返回值,'0' 代表成功, '1' 代表需要重启,其它值需要查看上面链接的说明。返回值的格式如下:

(0,)

即,为python中tuple格式。

之上的方法在win7 python3.8中测试通过。

Contents