How do I test in my script if I'm on my home network?

2

I need to run a batch file when I unlock the computer and, for part of it, it needs to test whether or not I am on my home network.

My current solution involving pinging the HTPC and making the assumption that if it can be found then I'm at home. I'm aware that this isn't the greatest solution - for starters, if the HTPC is off then it'll fail.

rem Ping the HTPC 4 times, pausing every 5 seconds.
for /l %%A in (1,1,4) do (
    timeout 5 >NUL
    ping MyHTPC -n 3 | find "TTL=" > NUL
    if not ERRORLEVEL 1 goto working
)
rem The HTPC cannot be found, so we're probably somewhere else    
[other code here]
exit

:working
rem The HTPC can be found, so we're probably at home
[other code here]

Is there a more robust way of doing this?

Richard

Posted 2017-03-19T13:34:39.310

Reputation: 4 197

Could you do something like this: http://pastebin.com/rTFNtp75 where you add your home router mac address as the variable $mac. You can get this from running arp -a

– HelpingHand – 2017-03-19T14:33:08.437

@EMK That is basically what I wrote in my answer, but using some PowerShell/WMI dark magic. It can be done with just CMD commands (ipconfig/find/arp and using FOR to parse the deault gateway from the ipconfig info.) – Tonny – 2017-03-19T16:44:35.980

Answers

3

Been there, done that.

First check/wait if your network is actually connected in the 1st place and if you have TCPIP up. (In case of Wifi connecting after the login your script may start before the Wifi is actually working.)
The "ipconfig" output (WITHOUT the /all parameter) will only show interfaces that are UP and will provide the ip-address of the default gateway.

Then ping that default gateway, which should be your router (I assume that is always on).
Then grep the output of "arp -a" to see if the MAC-address of your router is there. (If you use both wired and wireless check for both MAC-addresses. They are probably NOT the same.)
This MAC-address check also catches the case where you are on someone else's network where the router happens to have the same ip-address as you have at home.

And there is no need to do this 4 times with delays (I presume that is an attempt to get the HTPC out of power-sleep if needed). Just 1 ping (which under Windows does 4 pings with 1 second delay anyway) to the router is enough.
The router will either respond (so you can check the MAC) or it doesn't in which case something is really wrong with the network and it is unusable anyway.

Code below is tested on Windows 10. I'm fairly certain it will work on any Windows NT version.

@echo off
set mymac=ac-9e-17-96-6e-60

set delayedexpansion=on

rem Pull the default gateways from ipconfig and extract the one with a value.
rem Carefull! There is 1 extra space before the ip-address.

for /F "delims=: tokens=2 usebackq" %%a in ( `ipconfig ^| find /I "default gateway"` ) do (
  if NOT "%%a."==" ." set IP=%%a
)
echo Default gateway:%IP%

rem Ping it to make sure it appears in arp -a output
ping -n 1 %IP% >nul

rem Filter the line with the ip-address and MAC from arp -a and take action if found
arp -a | find /I "%IP%" | find /I "%mymac%"
if errorlevel 1 (echo Not found: Not at home) else ( echo I'm at home)

Tonny

Posted 2017-03-19T13:34:39.310

Reputation: 19 919

Works great, thanks! The reason I did 4 times with delays was because sometimes the WiFi wouldn't be connected quickly enough - so this would make the script wait up to 20 seconds for it to be ready. – Richard – 2017-03-19T17:21:37.420

@Richard The other alternative is to loop the For /F in my version until IP is set. That would require a very ugly GOTO construction though. If you don't mind the 20 seconds delay your solution to that issue is just fine. – Tonny – 2017-03-19T19:26:26.147

Shouldn't set delayedexpansion=on be setlocal EnableDelayedExpansion ? Recent windows versions have Getmax.exe to retrieve the MAC. – LotPings – 2017-03-19T20:21:48.723

@LotPings Yes, should be setlocal. I didn't notice the error because I have it always enabled as default. Getmac gets your own mac. Not that of the router. – Tonny – 2017-03-19T22:50:28.407