Solved

Batch script to change DHCP to Static

Posted on 2014-07-21
7
448 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
  • 3
  • 2
7 Comments
 
LVL 69

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 69

Expert Comment

by:Qlemo
ID: 40210924
It should, just try.
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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 69

Accepted Solution

by:
Qlemo earned 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Copy dir and files with robocopy 2 64
VB Script: restart service function 30 38
Copying directory listing to specific files in another directory 8 24
Modify file details 1 14
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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