设置IP地址python

时间:2017-12-05 12:59:15

标签: python ip wmi

我使用wmi moudle并尝试更改我自己的计算机IP地址而没有任何成功,似乎运行中没有任何错误但是ip地址没有改变 我已经检查过我是否有多个适配器,但我只有一个

import wmi

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

# First network adaptor
try:
    nic = nic_configs[0]
except Exception as e:
    raise Exception("חבר קודם את המחשב למתג!")

# IP address, subnetmask and gateway values should be unicode objects
ip = u'192.168.43.99'
subnetmask = u'255.255.0.0'
gateway = u'15.0.0.254'

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

输出:

(-2147024891,)
(67,)
编辑:我认为由于它没有在管理员下运行,我怎么能让它由管理员自动运行?

1 个答案:

答案 0 :(得分:0)

答案是如果我没有以管理员身份运行它会忽略代码,但是当我以管理员身份运行它时它会工作