Link to home
Start Free TrialLog in
Avatar of mtthompsons
mtthompsons

asked on

Batch script to change DHCP to Static

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)
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of mtthompsons
mtthompsons

ASKER

Thanks
I have Windows 7. Can i use the same?
It should, just try.
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...
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 . . .
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial