![]() |
whereami - Location Tests |
whereami will run all scripts in the /etc/whereami/tests directory until one sets the LOCATED variable to 1.
Each script should echo location information onto the end of the ./iwillbe file, and the last line of this file is expected to indicate the new location.
In my situation I have around 7 different locations I want to identify:
I don't currently have scripts to detect all of these situations, but the ones that are most important to me are when I am in a client's office for a week and I need to be able to get onto the internet and send/receive my e-mail without jumping through hoops.
In most of these situations DHCP is set up, which makes things quite a bit easier because I can assume that the network adapter is already configured when whereami is run. At Catalyst I also use the lspci command to see if my computer is in its docking station.
For the home network I don't have DHCP set up yet, so my last check is to ifconfig the adaptor to my home network address and try and ping the server (yeah, I should use 'arping' or 'divine' :-)
Since the docking station doesn't mean that I'm at Catalyst (in a 'network' sense), I do that check first, followed by checks for all of the DHCP addresses I might have assigned. Fortunately these all assign addresses on different class C ranges, so I don't have to get complicated and do 'nslookups' on assigned IP addresses and so forth, but the possible approaches are almost endless.
If all of the DHCP addresses failed, then I attack the addresses that have to be probed. If they fail then I must be 'somewhere else' and I configure the system as if I wasn't on a network at all. At this point you can use mii-tool to see if you actually have a link, or use arping to try and ping for a MAC address, or perhaps use divine, a package which includes lots of functionality for working out network topography.
Someday I will set some 'inertia' on the current location so that the last active location will weight my system's decision so that if I was last in Auckland it will assume that, since I am now off-line, I must be off-line in Auckland (so it could configure the defaults for ppp appropriately).
If you want, you can have a single script that works out your location, puts it into the ./iwillbe file, sets LOCATED=1 and returns. My examples don't do this simply because I find it easier to concentrate on finding out one thing at a time.
Having the detection of each location written in a separate script means that if you want a location to not be detected any longer, just move the script out of the 'tests' directory. If you have a new location that you want to add, just copy a similar example and change a couple of lines and its done.
Having the detection logic for each location in its own script makes these scripts very simple to put together (in general, anyway).
That said: one script, many scripts or something in between. You will make the decision they're all valid. If you're not string at scripting keep them separate and you'll find it easier to understand what each one is doing.
There are a bunch of programs which are useful to you in constructing tests (and another bunch that are useful once you know where you are :-). These are the ones that I and others have found useful for finding out where you are:
lspci | pciutils | Useful for finding if you are in a docking station. Use this to look for the presence of particular hardware devices. Of course you can also use /proc/pci to the same effect. |
---|---|---|
mii-tool | net-tools | Chris Halls pointed out this little tool which will detect whether a link is present to help speed up detecting when your network card is internal. |
fping | fping | This 'fast ping' program can help you find the closest of a bunch of hosts. By tuning the number of pings and the wait time you can get timeouts of around 100mS to find a host. |
arpfind | arping | This utility script provided with whereami uses arping to search for a particular MAC address on te network. This can be faster and more reliable than configuring the interface and then pinging for a particular IP address. |
divine | divine | Divine is a fairly sohpisticated program which can detect your network location in a variety of ways. I haven't used this one, but from looking at the docs it should be possible to do. Please send me any reports of success, with examples if possible, and I will include them here. |
Look at the sample scripts in /usr/share/doc/whereami/examples for examples of the use of these utilities.
Having found out where you are, what actions do you want your system to take?
Andrew's Linux Page | The 'Whereami' Utility | Whereami Tests | Whereami Actions | | © Andrew McMillan, 1999-2001 released under the GPL |