Solved

Batch script to change DHCP to Static

Posted on 2014-07-21
7
425 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 68

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 68

Expert Comment

by:Qlemo
ID: 40210924
It should, just try.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 68

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now