Performing an action based on your IP address

Here’s a neat little script that I just came up with that will be useless to most people, but someone might find it handy.

I’ve been playing with Microsoft’s SyncToy recently, as a way to keep the stuff I’m working on up-to-date between my workstation and my laptop. In order to do this, I kick off a synchronization every hour that compares a bunch of directories on my laptop and workstation, and ensures that the latest changes are present on each machine. This means that I can do work on my desktop and when I go home, it’s automatically on my laptop. Any work I do at home is then automatically moved back across to the desktop the next morning.

It’s very neat, but there are certain times when you don’t want this to take place. For example, when I’m at my desk, I have a nice 100mbit ethernet connection between my machines, so the sync typically takes a few seconds. However, when I’m in a meeting, it can easily take a few minutes over a WiFi link. If I’m working from home, that can extend to half an hour over VPN. And if I’m on the road, connected through my phone, well, it doesn’t bear thinking about.

Of course, it would be easy to simply disable the automatic synchronization in these circumstances - but in my experience, when I disable something like that, it can very easily end up not being re-enabled. I wanted something that could detect my location and act accordingly.

Fortunately, when I’m at my desk I have a static ethernet IP address assigned to my laptop. This means that I instantly know if the laptop is at my desk just by checking this IP. All I needed was a batch script that would check that IP and perform an action if it was set to a certain value. This was, to my surprise, remarkably simple:

@echo off
ipconfig /all|find "9.20.234.23" > NUL
if errorlevel 1 goto :notatdesk
echo You are at your desk!
%1
goto :eof
:notatdesk
echo You are not at your desk!

Let’s run down what this does. Firstly, it calls ‘ipconfig /all’. This lists a whole bunch of details about all the network adapters on your system, including their IP address (try it). We pipe this into the find command, and search for the IP address we want to check. (We also pipe the output of this command to null, for tidyness)

We then check the error code returned by the find command. If it’s ‘1′, then the command couldn’t find the IP address in the output from ipconfig. We jump down to the ‘notatdesk’ section, print out a helpful reply, and quit.

However, if the error code wasn’t 1, then we know that the find command [b]did[/b] find our IP address, and we instead print out a helpful message to this effect. The ‘%1′ following that is neat - it runs any command that was passed as an argument to our script. And when we’re done with that, we jump to the end of the file.

This means that you can simply run ‘checkip.bat notepad.exe’ (assuming you called your batch script checkip.bat) and the script will automatically launch notepad - but only if you’re at your desk!

Like I said, not useful to most people, but very useful to some.

One Response to “Performing an action based on your IP address”

  1. was looking all over the net for something like this! you are absolutely right about the “…very useful to some.” bit!

    thanks so much…

Leave a Reply