Phase2
asked on
Change IP with bat file using variables
I am using the follow script to change my IP info.
I would like to get the name of the existing network adapter, set a variable %nicName% with that name and then set my IP configs. The below script does this very well as long as the network adapter name is 3 words. I need to get it so that no matter what the name is the correct adapter name will be set.
Side note: This will run as a startup script
Any Ideas?
I would like to get the name of the existing network adapter, set a variable %nicName% with that name and then set my IP configs. The below script does this very well as long as the network adapter name is 3 words. I need to get it so that no matter what the name is the correct adapter name will be set.
Side note: This will run as a startup script
Any Ideas?
@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (set nicName=%%a %%b %%c)
echo Setting IP Address and Subnet Mask
netsh interface ip set address name="%nicName%" source=dhcp
echo Setting Primary DNS
netsh int ip set dns name="%nicName%" static 10.1.1.1
echo Setting Secondary DNS
netsh int ip add dns name="%nicName%" 10.1.1.2 index=2
echo Setting WINS to DHCP
netsh int ip set wins name="%nicName%" source=dhcp
obviously, if you increase the tokens to 'tokens=4-10', then you need to use %%f and %%g as well.
ASKER
what if the adapter name is one word?
Will netsh interface ip set address name="%nicName%" source=dhcp work?
or will it put spaces after the name?
Will netsh interface ip set address name="%nicName%" source=dhcp work?
or will it put spaces after the name?
ASKER
Just tried it it gives me an error on the name switch
ASKER
Perhaps there is another way to get the Network adapter name and set to variable?
trim the trailing spaces off then...something like this may work for you
for /l %%a in (1,1,128) do if "!nicName:~-1!"==" " set nicName=!nicName:~0,-1!
for /l %%a in (1,1,128) do if "!nicName:~-1!"==" " set nicName=!nicName:~0,-1!
ASKER
in some cases I will not know what the adapter name is. How can I get around that?
ASKER
the NIC name could be:
Network Adapter 4
or
192.168.1.5
or
myNIC
or
connect me to your leader
I need to be able to pull the name no matter what the name may be.
Network Adapter 4
or
192.168.1.5
or
myNIC
or
connect me to your leader
I need to be able to pull the name no matter what the name may be.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah thats a good point Dragon
What if I have more than one network Adapter and I need to reset all of them at the same time? How could I modify this bat?
What if I have more than one network Adapter and I need to reset all of them at the same time? How could I modify this bat?
Both mine and Bills would do that already as we loop through the output of those commands which will both show all adaptors.
Of course that may or not be what you want!
Of course that may or not be what you want!
ASKER
Dragon I cannot get your script to work. When I run it it just flashes the CMd windows for a split second.
I did a pause after the code and only thing that show is the press any key to continue message.
I did a pause after the code and only thing that show is the press any key to continue message.
Any luck with mine?
~bp
~bp
ASKER
billprew
Yours seems to work well. Let me do a bit of testing and I will close this question soon after.
Thanks again for everyones help!
Yours seems to work well. Let me do a bit of testing and I will close this question soon after.
Thanks again for everyones help!
Okay, I'm sure Steve will get to the bottom of his as well.
~bp
~bp
ASKER
I figured his script out also. The servers I am working on are Japanese lanaguage based.
Changed "Ethernet" to the Japanese equivalent and started to work.
Changed "Ethernet" to the Japanese equivalent and started to work.
Good find.
~bp
~bp
Sorry, snatching a few hours in bed there! That is one issue with using ipconfig .... Language dependent when using find like that .... i'd go with other version as long as it works on your machines, was just giving another way....
Steve
Steve
ASKER
Bill got most of the credit because when testing with adapter names like !name! or Hello!!! or Give! me! 3! dollars!, the full name did not echo out right. !name! didnt print at all. Special Charaters seems to break the ipconfig way.
Thank you both for all your help!!! You guys are great!
Thank you both for all your help!!! You guys are great!
They certainly do.... happy with that.... and thanks, got another "Master" certificate from this in the "Shell Scripting" area...
Shell Scripting: 50,160 (99,840 more points until Guru)
Steve
Shell Scripting: 50,160 (99,840 more points until Guru)
Steve
Congrats Steve!
~bp
~bp
Not bad considering I know nothing about shell scripting!!
for /f "skip=2 tokens=4-8" %%a in ('netsh interface show interface') do (echo %%a %%b %%c %%d %%e)
I've tested this in and prompt window and it works for me: -
C:\Windows\System32>for /f "skip=2 tokens=4-8" %a in ('netsh interface show interface') do (@echo %a %b %c %d %e)
C:\Windows\System32>()
VMware Network Adapter VMnet1
C:\Windows\System32>()
VMware Network Adapter VMnet8
C:\Windows\System32>()
Local Area Connection