【发布时间】:2019-05-13 20:34:38
【问题描述】:
更新:这个问题有很多更新,因为我已经弄清楚如何获取 NIC 的索引,但它不允许我设置 IP。当我尝试应用它时它不接受 IP 设置,我错过了什么?
我需要编写一个方法来更改 Python 3.7 中的网络适配器设置,但它基于用户输入,其中列出笔记本电脑上的所有网络适配器,然后用户选择正确的适配器。
我一直在使用 WMI 模块,并尝试了许多不同的方法来完成这项工作,但没有成功。我可以获得枚举适配器的列表,然后提供输入。我只是无法将此选择的适配器编号转换为 WMI 找到的网络适配器。
import wmi
#List NICs by Index and Description
c = wmi.WMI()
for nic in c.Win32_NetworkAdapterConfiguration(IPEnabled=True):
print(nic.Index, nic.Description)
#Choose which NIC to apply changes to:
nic_selection_index = c.Win32_NetworkAdapterConfiguration(Index = input('Choose Network Adapter Number on left to change: '))
#Will be hidden once working
print(nic_selection_index)
#Get IP Info to use on NIC
ip = input('Enter IP to use: ')
subnetmask = input('Enter Subnet Mask to use: ')
gateway = input('Enter Default Gateway to use: ')
print(ip)
print(subnetmask)
print(gateway)
## Set IP address, subnetmask and default gateway
## Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed
nic_selection_index.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
nic_selection_index.SetGateways(DefaultIPGateway=[gateway])
#Results:
Traceback (most recent call last):
File "test nic change.py", line 56, in <module>
nic_selection_index.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
AttributeError: 'list' object has no attribute 'EnableStatic'
我参考了其他设置 IP 的问题,但他们没有根据我输入的用户输入来设置它。我需要能够提示用户输入信息。谢谢!!
【问题讨论】: