Solved

Batch script to change DHCP to Static

Posted on 2014-07-21
7
437 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

22 Experts available now in Live!

Get 1:1 Help Now