1

I want to use a batch script which:

  • changes my network settings to a static ip when I'm at home
  • changes my network settings to dhcp when I'm not at home

I read here that you can check the network name by using

for /f "tokens=3" %%a in ('netsh wlan show interface ^| findstr /r "^....SSID"' ) do @echo %%a

So if I want to create a batch script like that would it look something like this?

for /f "tokens=3" %%a in ('netsh wlan show interface ^| findstr /r "^....SSID"' ) do (
    if "%%a"=="My_Cool_Home_Network" 
        netsh interface ip set address "NetworkAdapter" static 192.168.2.33 255.255.255.0 192.168.2.1
        netsh interface ip set dns "NetworkAdapter" static 192.168.2.1
    else
        netsh interface ip set address "NetworkAdapter" dhcp
        netsh interface ip set dns "NetworkAdapter" dhcp
)
6
  • You missed parenthesis at your if/else construct: if "%%a"=="My_Cool_Home_Network" ( -- netsh [...] -- ) else ( -- netsh [...] -- ) --- type if /? in command prompt for details... Commented Nov 26, 2015 at 19:11
  • Even with the parenthesis it doesn't work :( Commented Nov 26, 2015 at 20:57
  • My intention was just to fix the if/else syntax; I'm afraid I cannot help you with the netsh command... Commented Nov 26, 2015 at 22:37
  • Why not just configure your router at home to assign your device a static lease / IP reservation? Most can assign a pre-defined IP address outside the DHCP scope based on the device's MAC address. That's how I assign IPs in my house, anyway. That way if your significant other borrows your laptop and doesn't trigger the script, then he/she won't waste 30 minutes of the wifi provider's time troubleshooting the inability to access the net despite the successful connection. Commented Nov 27, 2015 at 4:29
  • Could you post the updated code? I think I might have an idea of what's going wrong. Commented Nov 27, 2015 at 12:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.