我想製作一個Python腳本,將我的IP地址設置爲靜態而不是動態。我已經爲此搜索了方法,並且Python的WMI實現似乎是最佳選擇。我得到的有關信息的stackoverflow問題是here。這個Python WMI腳本中錯誤的原因是什麼? (Windows 8.1)(網絡適配器配置)(Python 2.7)
我可以將IP地址設置爲靜態地址,但我必須設置DNS服務器。 This site here是我獲得DNS設置的基礎,但它造成問題。
回溯從IDLE
Traceback (most recent call last):
File "C:\Users\james_000\Desktop\SetIP.py", line 18, in <module>
c = nic.SetDNSServerSearchOrder(dns)
File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
handle_com_error()
File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0,
u'SWbemProperty', u'Type mismatch ', None, 0, -2147217403), None)>
SetIP.py
import wmi
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.151'
subnetmask = u'255.255.255.0'
gateway = u'192.168.0.1'
dns = u'192.168.0.1'
# Set IP address, subnetmask and default gateway
# Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed
a = nic.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
b = nic.SetGateways(DefaultIPGateway=[gateway])
c = nic.SetDNSServerSearchOrder(dns)
d = nic.SetDynamicDNSRegistration(true)
print(a)
print(b)
print(c)
print(d)
請不要在評論,因爲它使得它更難爲其他人瞭解如何解決補充解決方案問題。
你可以包括你的代碼中的問題本身有關的片段,而不是指示用戶下載的文件? – brianpck
@brianpck啊是啊...我現在要做 –
你需要閱讀[mcve]。代碼需要在問題本身,而不是外部鏈接。 –