Python Forum
How to rename Network Interface name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to rename Network Interface name
#1
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
Reply
#2
Try this:

import subprocess

cmd = 'netsh interface set interface name="OLDNIC" newname="NEWNIC"'
subprocess.call([cmd], shell=True)
Reply
#3
(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
Reply
#4
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"'])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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
Reply
#6
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?
Reply
#7
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  os.system("netsh interface set interface 'Wi-Fi' enabled") aniyanetworks 12 9,961 Jan-18-2019, 04:07 AM
Last Post: aniyanetworks

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020