Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a batch file that runs whenever I log in on my laptop. I only want the batch file to execute when I'm on my employer's network. At home, I'd like the batch file to simply exit.

What's an easy way to detect (from a batch file) whether I'm on a certain network?

share|improve this question
up vote 2 down vote accepted

A generic approach to check for specific network characterisics, e.g. a specific IP-Address, aber given server IP or the output of a traceroute. For this to work, it is important to know the differences in the network topologies and exploit them. A more detailed explaination would required some more information on the problem setting.

share|improve this answer
I didn't see this answer before I posted my ping idea. This approach seems a bit indirect -- try something that should only work on a certain network. I was wondering if there was something more direct -- a file or environment variable that can be tested. Perhaps there isn't. – Jeremy Stein Nov 30 '09 at 15:13
Vista and Win7 have intelligence to detect what type of network they're plugged into and do stuff differently depending, presumably they've got that sort of variable. XP doesn't have that kind of network discovery intelligence built-in. Checking the machines IP address, address range, subnet address or default gateway sound like the most direct ways to do this. – GAThrawn Nov 30 '09 at 15:54

I could ping our domain controller and check for an error:

ping -n 1 servername
if errorlevel 1 exit

The downside is that I have to wait for a timeout when it doesn't work. I was hoping for something immediate. I'm open to better suggestions.

share|improve this answer
This is better than checking IP addresses, because lots of places use the common non-public address spaces. You can call ping -r to control how long to wait. So ping -n 1 -w 5 servername will only wait 5 milliseconds. – tomjedrz Nov 30 '09 at 16:04
@tomjedrz: I think the -w option controls how long to wait for the ping to respond, not how long to wait for name resolution. It still takes several seconds for it to fail when I use -w 5. – Jeremy Stein Nov 30 '09 at 16:43

I could check whether the default gateway is set for work:

ipconfig | find ""
if errorlevel 1 exit
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.