?
Solved

Change IP with bat file using variables

Posted on 2011-10-18
23
Medium Priority
?
802 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Phase2
  • 10
  • 5
  • 5
  • +1
23 Comments
 
LVL 23

Expert Comment

by:jakethecatuk
ID: 36985520
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
0
 
LVL 23

Expert Comment

by:jakethecatuk
ID: 36985524
obviously, if you increase the tokens to 'tokens=4-10', then you need to use %%f and %%g as well.
0
 

Author Comment

by:Phase2
ID: 36985642
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?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Phase2
ID: 36985659
Just tried it it gives me an error on the name switch
0
 

Author Comment

by:Phase2
ID: 36985670
Perhaps there is another way to get the Network adapter name and set to variable?
0
 
LVL 23

Expert Comment

by:jakethecatuk
ID: 36985715
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!
0
 

Author Comment

by:Phase2
ID: 36985746
in some cases I will not know what the adapter name is. How can I get around that?
0
 

Author Comment

by:Phase2
ID: 36985815
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.
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1600 total points
ID: 36986793
Try this.  By using tokens=3* it will place the 3rd space delimited token in %%a, and the rest of the line into %%b.

@echo off
for /f "skip=2 tokens=3*" %%a in ('netsh interface show interface') do (
    echo Setting IP Address and Subnet Mask
    netsh interface ip set address name="%%b" source=dhcp

    echo Setting Primary DNS
    netsh int ip set dns name="%%b" static 10.1.1.1 

    echo Setting Secondary DNS
    netsh int ip add dns name="%%b" 10.1.1.2 index=2

    echo Setting WINS to DHCP
    netsh int ip set wins name="%%b" source=dhcp
)

Open in new window

~bp
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 400 total points
ID: 36991183
Have you conisdered the profliferation of other adaptors such as firewire, vpn, 3g cards and the like this might pickup too?

Another similar way is to get it from ipconfig instead:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2* delims= " %%a in ('ipconfig ^| find "Ethernet"') do (
  set name=%%b
  set name=!name:~,-1!

  echo Setting IP Address and Subnet Mask for interface !name!
  netsh interface ip set address name="!name!" source=dhcp

  echo Setting Primary DNS
  netsh int ip set dns name="!name!" static 10.1.1.1

  echo Setting Secondary DNS
  netsh int ip add dns name="!name!" 10.1.1.2 index=2

  echo Setting WINS to DHCP
  netsh int ip set wins name="!name!" source=dhcp
)
0
 

Author Comment

by:Phase2
ID: 36992293
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?

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36992463
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!

0
 

Author Comment

by:Phase2
ID: 36997155
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.

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36997344
Any luck with mine?

~bp
0
 

Author Comment

by:Phase2
ID: 36997386
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!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36997399
Okay, I'm sure Steve will get to the bottom of his as well.

~bp
0
 

Author Comment

by:Phase2
ID: 36997409
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.

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36997417
Good find.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36997894
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
0
 

Author Comment

by:Phase2
ID: 36999165
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!





0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36999231
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
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37001266
Congrats Steve!

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37001281
Not bad considering I know nothing about shell scripting!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question