Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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
1  
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
add comment

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
add comment

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

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

Your Answer

 
discard

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.