?
Solved

Batch script to change DHCP to Static

Posted on 2014-07-21
7
Medium Priority
?
474 Views
Last Modified: 2014-08-03
Change ipaddress
I need help with a batch file that can switch from DHCP to static ipaddress
Ip/Subnet/Gateway
Preferred DNS and Alternative DNS

These 5 to be added and when the same batch file is run if already static make it DHCP (Automatically select)
0
Comment
Question by:mtthompsons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40208628
Which OS? Usually you are doing it with netsh, but the syntax and default interface names depend on the OS. Example:
@echo off
REM values needing changes for each machine
set NIC=LAN
set IP=1.2.3.4

REM DHCP or static?
(
  netsh interface ip show addresses %NIC% | find "DHCP enabled" | find "No" >nul && (
    REM is static, switch to DHCP
    echo set address   name=%NIC% source=DHCP
    echo set dnsserver name=%NIC% source=DHCP
  ) || (
    REM is DHCP, switch to static
    echo set address   name=%NIC% source=static address=%IP%/24 gateway=1.2.3.1
    echo set dnsserver name=%NIC% source=static address=1.2.3.1
    echo add dnsserver name=%NIC%               address=1.2.3.2 index=2
  )
) | netsh interface ip

Open in new window

0
 

Author Comment

by:mtthompsons
ID: 40210630
Thanks
I have Windows 7. Can i use the same?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40210924
It should, just try.
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!

 
LVL 16

Expert Comment

by:vivigatt
ID: 40212203
You can adapt the script (or use another one) for you to
- get the currently assigned IP address and subnetmask + gateway
- set the IP config to use a static config, re-using the former IP address, subnet mask and gateway.

I have some tools that can help with these goals...
0
 

Author Comment

by:mtthompsons
ID: 40213319
Qlemo sorry i missed seeing your batch script
I tried it and i got this on the screen. Also where can i enter the subnet


The following commands are available:

Commands in this context:
?              - Displays a list of commands.
add            - Adds a configuration entry to a table.
delete         - Deletes a configuration entry from a table.
dump           - Displays a configuration script.
help           - Displays a list of commands.
install        - Install the IP protocol.
reset          - Reset the IP configurations.
set            - Sets configuration information.
show           - Displays information.
uninstall      - Uninstall the IP protocol.

To view help for a command, type the command, followed by a space, and then
 type ?.

Press any key to continue . . .
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40213859
Forgot to use the "context" parameter. Also note that there are language-dependent parts for detecting whether DHCP is used.
I assumed the subnet to be /24 (255.255.255.0), and used that as a fixed value.
@echo off
REM values needing changes for each machine
set NIC=LAN
set IP=1.2.3.4

REM DHCP or static?
(
  netsh interface ip show addresses %NIC% | find "DHCP enabled" | find "No" >nul && (
    >con echo static, switch to DHCP
    echo set address   name=%NIC% source=DHCP
    echo set dnsserver name=%NIC% source=DHCP
  ) || (
    >con echo DHCP, switch to static
    echo set address   name=%NIC% source=static address=%IP%/24 gateway=1.2.3.1
    echo set dnsserver name=%NIC% source=static address=1.2.3.1
    echo add dnsserver name=%NIC%               address=1.2.3.2 index=2
  )
) | netsh -c "interface ip" >nul

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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