• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Tip: How to set a Static IP from the Command Prompt and setting it back to DHCP.

newtekie1

Semi-Retired Folder
Joined
Nov 22, 2005
Messages
28,472 (4.23/day)
Location
Indiana, USA
Processor Intel Core i7 10850K@5.2GHz
Motherboard AsRock Z470 Taichi
Cooling Corsair H115i Pro w/ Noctua NF-A14 Fans
Memory 32GB DDR4-3600
Video Card(s) RTX 2070 Super
Storage 500GB SX8200 Pro + 8TB with 1TB SSD Cache
Display(s) Acer Nitro VG280K 4K 28"
Case Fractal Design Define S
Audio Device(s) Onboard is good enough for me
Power Supply eVGA SuperNOVA 1000w G3
Software Windows 10 Pro x64
I have a friend that carries his laptop back and forth between his home and his office. At the office he has to use a static IP, and at home the static IP doesn't work so he has to use DHCP(or set a static IP that does work). So a did a little research and found out how to set a static IP via the command prompt and then how to remove that static IP and go back to DHCP.

The command lines to set a static IP are:

Code:
netsh int ip set address "<connection_name>" static <static_ip> <subnet_mask> <default_gateway> 1
netsh int ip set dns "<connection_name>" static <primary_dns> primary

So it would look like this:
Code:
netsh int ip set address "local area connection" static 10.0.0.100 255.0.0.0 10.0.0.1 1
netsh int ip set dns "local area connection" static 10.0.0.1 primary

The command lines to set everything back to DHCP are:

Code:
netsh int ip set address "local area connection" dhcp
netsh int ip set dns "local area connection" dhcp

I've included a zip file with two bat files that do this that I created for him so he doesn't have to run the commands manually every time he moves between the office and his home.

Hopefully someone else finds this useful.:toast:
 

Attachments

  • Staticbat.zip
    358 bytes · Views: 138

PVTCaboose1337

Graphical Hacker
Joined
Feb 1, 2006
Messages
9,501 (1.43/day)
Location
Texas
System Name Whim
Processor Intel Core i5 2500k @ 4.4ghz
Motherboard Asus P8Z77-V LX
Cooling Cooler Master Hyper 212+
Memory 2 x 4GB G.Skill Ripjaws @ 1600mhz
Video Card(s) Gigabyte GTX 670 2gb
Storage Samsung 840 Pro 256gb, WD 2TB Black
Display(s) Shimian QH270 (1440p), Asus VE228 (1080p)
Case Cooler Master 430 Elite
Audio Device(s) Onboard > PA2V2 Amp > Senn 595's
Power Supply Corsair 750w
Software Windows 8.1 (Tweaked)
I have a bat just like this that I made a while back. Never though anyone else would automate something like this.

I tried for a while to get it to recognize if it was at school or my home, but it was too tricky to do in just batch commands.

Anyway I tried to do like:

Code:
If netsh show mode = online then
exit
else
netsh static address here
    If netsh show mode = online then exit
    Else netsh dhcp stuff here
    exit
    end if
end if


Never got it to work though.

Edit: Forgot a lot of the logic, had it switch between, but again, never worked.
EDIT2: Yeah he only really need 1 batch file if you follow my pseudo code. (and run it at startup for epic win)
 

newtekie1

Semi-Retired Folder
Joined
Nov 22, 2005
Messages
28,472 (4.23/day)
Location
Indiana, USA
Processor Intel Core i7 10850K@5.2GHz
Motherboard AsRock Z470 Taichi
Cooling Corsair H115i Pro w/ Noctua NF-A14 Fans
Memory 32GB DDR4-3600
Video Card(s) RTX 2070 Super
Storage 500GB SX8200 Pro + 8TB with 1TB SSD Cache
Display(s) Acer Nitro VG280K 4K 28"
Case Fractal Design Define S
Audio Device(s) Onboard is good enough for me
Power Supply eVGA SuperNOVA 1000w G3
Software Windows 10 Pro x64
Yeah, I tried to automate it too, but it just became too much of a hassle so I said screw it.:laugh:
 
Top