【问题标题】:How do I change Network Adapter settings based on input from user on which adapter?如何根据用户在哪个适配器上的输入更改网络适配器设置?
【发布时间】: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 的问题,但他们没有根据我输入的用户输入来设置它。我需要能够提示用户输入信息。谢谢!!

【问题讨论】:

    标签: python wmi


    【解决方案1】:

    所有,我能够让它工作,甚至添加了一些 DNS 更改。我发帖以防其他人一直在寻找这种确切的需求。

    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: '))
    
    #This selects the entire NIC properties -- can't just use the index value above
    nic_selection_all = nic_selection_index[0]
    
    #Will be hidden once working
    print(nic_selection_all)
    
    #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: ')
    dns = '127.0.0.1'
    
    
    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_all.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
    nic_selection_all.SetGateways(DefaultIPGateway=[gateway])
    nic_selection_all.SetDNSServerSearchOrder([dns])
    nic_selection_all.SetDNSDomain("my.random.domain")
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多