Link to home
Start Free TrialLog in
Avatar of Phase2
Phase2Flag for United States of America

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?
@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

Open in new window

Avatar of jakethecatuk
jakethecatuk
Flag of United Kingdom of Great Britain and Northern Ireland image

quite simple - adjust the number of tokens: -

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
obviously, if you increase the tokens to 'tokens=4-10', then you need to use %%f and %%g as well.
Avatar of Phase2

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?

Avatar of Phase2

ASKER

Just tried it it gives me an error on the name switch
Avatar of Phase2

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!
Avatar of Phase2

ASKER

in some cases I will not know what the adapter name is. How can I get around that?
Avatar of Phase2

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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Phase2

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?

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!

Avatar of Phase2

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.

Avatar of Bill Prew
Bill Prew

Any luck with mine?

~bp
Avatar of Phase2

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!
Okay, I'm sure Steve will get to the bottom of his as well.

~bp
Avatar of Phase2

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.

Good find.

~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
Avatar of Phase2

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!





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
Congrats Steve!

~bp
Not bad considering I know nothing about shell scripting!!