Python Forum

Full Version: How to rename Network Interface name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone,
I have a Batch script which is working fine, but I want to achieve the same in the python3.7 script.
Please help.
netsh interface set interface name="OLDNIC" newname="NEWNIC"

I had a Pything script which was doing Disable the Wifi but how i can change the name? by using Pything Script.
subprocess.call(['netsh', 'interface', 'set', 'interface', 'Wi-Fi', 'disabled'])
Please advise.
Thank you
ANS
Try this:

import subprocess

cmd = 'netsh interface set interface name="OLDNIC" newname="NEWNIC"'
subprocess.call([cmd], shell=True)
(Jan-30-2019, 04:27 PM)gontajones Wrote: [ -> ]Try this:

Hello Gonatajones,
Thanks for your quick reply, i tried your code and returned following messages.
Please take a look.
Thanks
import subprocess

cmd = 'netsh interface set interface name="OLDNIC" newname="NEWNIC"'
subprocess.call([cmd], shell=True)

import os
import subprocess
cmd = 'netsh interface set interface name="TPALAN" newname="TPALAN2"'
subprocess.call([cmd], shell=True)
os.system("netsh interface show interface")
Output:
'"netsh interface set interface name=\"TPALAN\" newname=\"TPALAN2\""' is not recognized as an internal or external command, operable program or batch file. Admin State State Type Interface Name ------------------------------------------------------------------------- Disabled Disconnected Dedicated Wi-Fi Disabled Disconnected Dedicated TPALAN Enabled Connected Dedicated TPALANDOC Process finished with exit code 0
To disable Wi-Fi
>>> import subprocess
>>> subprocess.call(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', 'admin="DISABLED"'])

0
You need to run cmd in elevated mode (i.e. Run As Administrator)
Change the name if the adapter name is not Wi-Fi, e.g. Wireless Network Connection, etc.

Use this as example how to rename interface
>>> subprocess.call(['netsh', 'interface', 'set', 'interface', 'name="TPALAN"', 'newname="TPALAN2"'])
(Jan-30-2019, 04:54 PM)buran Wrote: [ -> ]To disable Wi-Fi
>>> import subprocess
>>> subprocess.call(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', 'admin="DISABLED"'])

0
You need to run cmd in elevated mode (i.e. Run As Administrator)
Change the name if the adapter name is not Wi-Fi, e.g. Wireless Network Connection, etc.

Use this as example how to rename interface
>>> subprocess.call(['netsh', 'interface', 'set', 'interface', 'name="TPALAN"', 'newname="TPALAN2"'])

Perfect Thanks Buran.
One more questions: I am trying to get input function to get Interface name from User, but it doesn't accept input value. can you please help?
import subprocess
import os

changeName=input("Enter the Interface name:")
print(changeName)
subprocess.call(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', 'newname=changeName'])
os.system("netsh interface show interface")
Output:
Enter the Interface name:Wi-Fi123 Wi-Fi123 Admin State State Type Interface Name ------------------------------------------------------------------------- Disabled Disconnected Dedicated changeName Disabled Disconnected Dedicated TPALAN Enabled Connected Dedicated TPALANDOC Process finished with exit code 0
It doesn't accept the Value from Input function.
Please help.
Thank you
That's simple string formatting. Ignore the fact that it's a network interface, or a different command. How would you just print it out to the screen?
if using 3.6+ use f-strings
subprocess.run(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', f'newname="{changeName}"'])
if using lower version
subprocess.run(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', 'newname="{}"'.format(changeName)])
for greater readability you may want to make this before the call
import subprocess

change_name = input("Enter the Interface name:")
new_name = 'newname="{}"'.format(change_name) # new_name = f'newname="{change_name}"'
subprocess.run(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', new_name])
subprocess.run(['netsh', 'interface', 'show', 'interface'])
don't use os.system - it's depreciated, use subprocess.call or even better subprocess.run
(Jan-30-2019, 05:33 PM)buran Wrote: [ -> ]if using 3.6+ use f-strings
subprocess.run(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', f'newname="{changeName}"'])
if using lower version
subprocess.run(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', 'newname="{}"'.format(changeName)])
for greater readability you may want to make this before the call
import subprocess

change_name = input("Enter the Interface name:")
new_name = 'newname="{}"'.format(change_name) # new_name = f'newname="{change_name}"'
subprocess.run(['netsh', 'interface', 'set', 'interface', 'name="Wi-Fi"', new_name])
subprocess.run(['netsh', 'interface', 'show', 'interface'])
Perfect Thanks Buran.

don't use os.system - it's depreciated, use subprocess.call or even better subprocess.run