[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Change IP with bat file using variables

Posted on 2011-10-03
6
Medium Priority
?
463 Views
Last Modified: 2012-06-21
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 with that name and replace name="Local Area Connection" with something like name=%nicName%

How can I do this?
REM ***** Change IP back to DHCP ******

@ECHO OFF
         
ECHO Setting IP Address and Subnet Mask
netsh interface ip set address name="Local Area Connection" source=dhcp
     
ECHO Setting Primary DNS
netsh int ip set dns name="Local Area Connection" static 10.0.1.1 
     
ECHO Setting Secondary DNS
netsh int ip add dns name=Local Area Connection" 10.0.1.2 index=2

ECHO Setting WINS to DHCP
netsh int ip set wins name="Local Area Connection" source=dhcp

ECHO Here are the new settings for %computername%:
netsh int ip show config
     
pause

Open in new window

0
Comment
Question by:Phase2
  • 3
  • 3
6 Comments
 
LVL 15

Assisted Solution

by:Giovanni Heward
Giovanni Heward earned 2000 total points
ID: 36902789
Here's one way to do it...
@echo off
for /f "skip=2 tokens=4,5,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.0.1.1 
     
     echo Setting Secondary DNS
     netsh int ip add dns name="%nicName%" 10.0.1.2 index=2

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

     echo Here are the new settings for %computername%:
     netsh int ip show config
     
)
pause

Open in new window

0
 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 2000 total points
ID: 36902797
Err... here is the corrected version
@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (
	set nicName=%%a %%b %%c
	echo "!nicName!"
)
pause

Open in new window

0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 36902834
Here is an alternative method of extracting the NIC name(s)...
@echo off
setlocal enabledelayedexpansion
for /f "skip=3 tokens=*" %%a in ('netsh interface show interface') do (
	set nicName=%%a
	set nicName=!NicName:Enabled=!
	set nicName=!NicName:Disconnected=!
	set nicName=!NicName:Connected=!
	set nicName=!NicName:Dedicated=!
	for /f "tokens=* delims= " %%n in ("!nicName!") do set nicName=%%n
	echo [!nicName!]
)
pause

Open in new window

0
Technology Partners: 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: 36907347
Hi X
Thanks for the script. Below is what I am using. This extracts the NIC name just fine but doesnt seem to do the rest with setting IPs.

Also I only need it to find the name of the first NIC it finds.

@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (
	set nicName=%%a %%b %%c
	echo "!nicName!"
)

     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 59.106.192.1 
     
     echo Setting Secondary DNS
     netsh int ip add dns name="%nicName%" 59.106.192.2 index=2

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

     echo Here are the new settings for %computername%:
     netsh int ip show config
     
)
pause

Open in new window

0
 

Author Comment

by:Phase2
ID: 36907444
OK figured out the issue.

%%c  was adding an extra space in the nicName variable. My nicName is actually in Japanese so the format is a little different.

¿¿¿¿ ¿¿¿¿¿

3words but only one space in the name.

Thank you very much!

Below is the code that works for me



@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (
	set nicName=%%a %%b
)

     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 59.106.192.1 
     
     echo Setting Secondary DNS
     netsh int ip add dns name="%nicName%" 59.106.192.2 index=2

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

     echo Here are the new settings for %computername%:
     netsh int ip show config
     

DEL "%~f0"

Open in new window

0
 

Author Comment

by:Phase2
ID: 36907451
For those trying to use this script make sure you take off the

DEL "%~f0"

at the end or else your bat file will delete after you run it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
If you’re involved with your company’s wide area network (WAN), you’ve probably heard about SD-WANs. They’re the “boy wonder” of networking, ostensibly allowing companies to replace expensive MPLS lines with low-cost Internet access. But, are they …
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

872 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