0

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?

4 Answers 4

1

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.

3
  • 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. Nov 30, 2009 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, 2009 at 15:54
  • This should not be an answer, it is completely possible that connecting to another network could result in similar data. For example a common IP scope or similar default gateway IP. Even a ping test is not a valid solution, you could be connected to the corporate wifi and not be domain authenticated and still be able to ping, a server for example.
    – htm11h
    Nov 2, 2021 at 16:32
1

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.

2
  • 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, 2009 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. Nov 30, 2009 at 16:43
1

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

ipconfig | find "10.1.1.1"
if errorlevel 1 exit
1

At a command prompt enter echo %logonserver% and see if your domain computer is returned as the server that you are connected to.

In fact, here is an even better way, put this in your bat file....

@echo off 
IF EXIST %logonserver% (
  echo 'On LAN!'
)ELSE (
  echo 'Not on LAN!'
)

Hope that helps. if you know the authentication server name you can obviously add a conditional equal statement in the if portion to confirm that the server exists, then run the remainder of your batch file. And of course the ELSE portion would simply exit.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .