Reputation: 41
UPDATE: Lots of updates to this question as I've figured out how to get the Index of the NIC, but it's not allowing me to set the IP. What am I missing where it won't accept the IP settings when I tried to apply it?
I'm needing to script a method to change network adapter settings in Python 3.7, but base it on user input where it lists all network adapters on the laptop and the user then chooses the correct adapter.
I've been using the WMI module and have tried many different ways to make this work with no luck. I can get a list of enumerated adapters and then provide input. I just cannot get this chosen adapter number translated to the WMI found network adapter.
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'
I've referenced other questions that are setting the IP but they're not setting it based on user input that I put in. I need to be able to prompt the user for the information. Thanks!!
Upvotes: 3
Views: 3789
Reputation: 41
All, I was able to get this working, even adding in some DNS changes as well. I'm posting in case anyone else is ever looking for this exact need.
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")
Upvotes: 1