Switch IP Quickly

Posted by Lynn on September 27, 2019

I usually need to access different networks through different network settings, such as accessing the campus network via DHCP and using static IP to access the server in the lab. So I need a way to switch IP quickly.

For macOS

macOS supports saving multiple network settings for different locations. You can see the settings in System Preferences -> Network -> Location.

Network settings for different locations

Add New Locations

You just need to click Location -> Edit Locations... -> +, and set a name for the untitled new location. For example, I set two locations, Automatic and lab. The former uses the default network settings to enable Wi-Fi to automatically obtain IP, while the latter will set a static IP, which allows me to access resources on the LAN.

Add a new location for network

Switch Locations

After you add a new network, you will find a new option in the menu of the Apple logo in the upper left corner of the screen. All you need to do is switch between different locations when you need to change the IP quickly.

Change IP quickly by switching locations

For Windows

Windows users could quickly configure the network through Network Shell (Netsh). For convenience, you can create a batch file and choose to execute different commands when you need to switch IP.

Create Batch File

You can create a batch file (such as config-ip.bat) based on the code below (You can also view the source file here).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
REM Please update the variables below before you run it
SET ethernet-adapter-name=Ethernet
SET wifi-adapter-name=Wi-Fi
SET ethernet-static-ip=192.168.1.90
SET wifi-static-ip=192.168.1.90

@echo off
cls
color 0A

:check_Permissions
fsutil dirty query %systemdrive% >nul
if %errorLevel% == 0 (
    goto loop
) else (
    echo ######
    echo #     # #      ######   ##    ####  ######    #####  #    # #    #      ##    ####
    echo #     # #      #       #  #  #      #         #    # #    # ##   #     #  #  #
    echo ######  #      #####  #    #  ####  #####     #    # #    # # #  #    #    #  ####
    echo #       #      #      ######      # #         #####  #    # #  # #    ######      #
    echo #       #      #      #    # #    # #         #   #  #    # #   ##    #    # #    #
    echo #       ###### ###### #    #  ####  ######    #    #  ####  #    #    #    #  ####
    echo=
    echo    #    ######  #     # ### #     # ###  #####  ####### ######     #    ####### ####### ######     ### ###
    echo   # #   #     # ##   ##  #  ##    #  #  #     #    #    #     #   # #      #    #     # #     #    ### ###
    echo  #   #  #     # # # # #  #  # #   #  #  #          #    #     #  #   #     #    #     # #     #    ### ###
    echo #     # #     # #  #  #  #  #  #  #  #   #####     #    ######  #     #    #    #     # ######      #   #
    echo ####### #     # #     #  #  #   # #  #        #    #    #   #   #######    #    #     # #   #
    echo #     # #     # #     #  #  #    ##  #  #     #    #    #    #  #     #    #    #     # #    #     ### ###
    echo #     # ######  #     # ### #     # ###  #####     #    #     # #     #    #    ####### #     #    ### ###
    pause
    Exit
)

:loop
echo 1 DHCP %ethernet-adapter-name%
echo 2 Static %ethernet-adapter-name% (%ethernet-static-ip%)
echo 3 DHCP %wifi-adapter-name%
echo 4 Static %wifi-adapter-name% (%wifi-static-ip%)
echo 5 Show IP Configuration
echo 6 Exit
echo|set /p=Please choose a option:

set/p sel=
if "%sel%"=="1" goto DHCP-Ethernet
if "%sel%"=="2" goto Static-Ethernet
if "%sel%"=="3" goto DHCP-Wifi
if "%sel%"=="4" goto Static-Wifi
if "%sel%"=="5" goto Show-IP-Configuration
if "%sel%"=="6" EXIT
echo=
echo Please choose a valid option
goto loop

:DHCP-Ethernet
netsh interface ip set address name=%ethernet-adapter-name% source=dhcp
netsh interface ip delete dns %ethernet-adapter-name% all
ipconfig /flushdns
goto Show-IP-Configuration

:Static-Ethernet
netsh interface ip set address name=%ethernet-adapter-name% source=static addr=%ethernet-static-ip% mask=255.255.255.0 gateway=192.168.1.1
netsh interface ip set dns name=%ethernet-adapter-name% source=static addr=192.168.1.1
ipconfig /flushdns
goto Show-IP-Configuration

:DHCP-Wifi
netsh interface ip set address name=%wifi-adapter-name% source=dhcp
netsh interface ip delete dns %wifi-adapter-name% all
ipconfig /flushdns
goto Show-IP-Configuration

:Static-Wifi
netsh interface ip set address name=%wifi-adapter-name% source=static addr=%wifi-static-ip% mask=255.255.255.0 gateway=192.168.1.1
netsh interface ip set dns name=%wifi-adapter-name% source=static addr=192.168.1.1
ipconfig /flushdns
goto Show-IP-Configuration

:Show-IP-Configuration
netsh interface ip show addresses %ethernet-adapter-name%
netsh interface ip show addresses %wifi-adapter-name%

echo=
goto loop

Note that before you run the batch file you should update the contents of the file at the start based on your network adapters and IP. If your settings for LAN are different from mine, you may need to modify other network parameters.

Run Batch as Administrator

When you want to switch IP, you only need to run the batch file you created earlier and then choose one option. But be aware that you must run it as administrator, otherwise you will only see one message PLEASE RUN AS ADMINISTRATOR !!.

BTW, you can avoid manually select Run as administrator from context menu by following steps below

  1. Create a shortcut for this batch
  2. Click Properties from the context menu to open Shortcut Properties
  3. Click Advanced from the Shortcut tab
  4. Check Run as administrator in the pop-up window
  5. Save and close pop-up windows

After that, you only need to double-click the shortcut to run the batch file as administrator. Enjoy!😃