#!/bin/sh ###################################################################### # PublishIP - upload computer's IP address to remote system # Copyright (C) 2003 John F. Whitehead # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # The GNU GPL is available at . # ###################################################################### # # PublishIP enables you to upload your computer's IP address to a # remote computer. This can be helpful to: # # - have a web page at a static address that always points to your # computer, even if your computer's address changes, and # # - track your computer's address and ISP in the unfortunate event your # computer is stolen. # # This is version 1.0 -- if you have any suggestions, please email me. # Hope you find it useful. - John F. Whitehead # # How It Works / Requirements # # This is a simple shell script that determines your computer's IP # address and copies it to a remote computer. It requires that you # have a remote machine accessible by ssh, with your keys created # and installed. Although it has been written for Mac OS X, it could # be adapted for Linux and other variants with minimal effort. # # Download / Install # # The latest version can be downloaded from # # # The package is recommended if you want to install it as a # StartupItem, since it includes the self-named directory, # StartupParameters.plist, and the localization Resources to ensure # it runs properly upon startup. # # To install, unzip the package in /Library/StartupItems/ to have it # run on every system reboot. Customize the variables for username, # filename, and remote host appropriately. (This "package" is not a # self-installing executable, just a tarred and gzipped directory.) # # The standalone program (included in the package) is fine if you # want to set it up as a StartupItem yourself, or just run the # program manually or as a cron job, or on a non-Mac platform. # # To install, save it wherever you want (be sure to change its # permissions to be executable). Customize the variables for # username, filename, and remote host appropriately. # # Some comments: # # - If your computer is assigned an address dynamically (as with DHCP), # this will allow its new address to be published to a remote server. # # - You may want to publish to a web server, so you can either view # the file it creates directly or from a web page (via a server-side # include command). # # - This program requires ssh and scp. A key must be set up for a # local user and the public key on the remote server. # # - By default the file is written to be readable/writable only by # the remote user. If you wish the file to be world readable (such # as by a web server), change the $mode variable. # # - The target directory (and file, if it already exists) must remain # writable. (An alternate method would be to alter the script to # write to a different file each time -- use the temp file name or # create one based on the time of day). # # - For theft tracking, this script should be run at startup (in Mac # OS X, the /Library/StartupItems/PublishIP/ directory). # # - Even better, run it regularly as a cron job in case the system # isn't rebooted. This would also be necessary if the network is # not up when the computer is turned on, for instance if a dialup # account is used, or when the network changes without a reboot. # # - You should test the script regularly, since versions of ssh can # change, login scripts can interfere with ssh, etc. # # - Verbose output is the default; for more silent action (no entry # in the /var/log/system.log, nothing written to console), comment # out or change the value of the 'verbose' variable. # # The file created contains just the IP address as returned by # ifconfig -a (as called by CheckNetworkStatus), expecting it to be # the second field after "inet". If your system does not support # this command with a line starting with inet xx.xx.xx.xx, it may # not work. I haven't tested for IPV6 network numbers. (I used to # have this return the DNS name, but since that may not be assigned # properly I no longer recommend doing that.) # # Some caveats: # # - It will not be useful if you have been assigned a NAT address # (such as 192.x.x.x or 10.x.x.x), since those are only local to # your immediate network. # # - It will not work if the program is not run -- for example, if # someone disables or erases it, does not reboot, cancels the cron # job, or reformats the disk. # # - It may not be useful for theft tracking if you have a static IP # address, since the computer won't automatically and silently # reconfigure itself on the new network, as it would if both the old # and new networks use dynamically assigned DHCP addresses. It may # work once the network is correctly configured, though. # # - I have only tested this on Mac OS X 10.2.8 (Jaguar) and Mac OS X # 10.3.3+ (Panther), which includes Darwin Kernel Versions 6.8, # 7.3.0, and 7.50. # # # PublishIP, version 1.0, 29 Sep 2003 ###################################################################### # customize these temp="/tmp/temp-ip-$$" # temporary local filename localuser="USER" # local user must have an ssh private key sshfile="/Users/USER/.ssh/id_dsa" # local user's ssh private key dest="USER@HOST.com" # remote user must have localuser's pubkey file=".home-ip-address" # remote filename (default is a hidden file) mode=600 # 600 = read/writable only for remote user # 644 = world-readable (like for web access) # mode must at least be owner-writable verbose=1 # comment out or set to 1 for silent action (good for # surreptitious execution) ###################################################################### # you probably shouldn't have to edit anything below here ###################################################################### # this is necessary for OS X's CheckForNetwork and ConsoleMessage commands . /etc/rc.common # make sure network is up CheckForNetwork if [ "${NETWORKUP}" = "-NO-" ]; then if [ "${verbose:=0}" = 1 ]; then ConsoleMessage "PublishIP: Network is not up; can't publish IP" fi exit fi # create file containing IP address touch "$temp" chmod $mode "$temp" ip=$(ifconfig -a inet 2>/dev/null | sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' -e '/inet/p' | cut -f2 -d' ') echo "$ip" > "$temp" if [ "${verbose:=0}" = 1 ]; then ConsoleMessage "PublishIP: publishing IP" fi # copy file scp -p -q -i "$sshfile" "$temp" "${dest}:$file" 2>/dev/null result=$? if [ "${result}" -gt 0 ]; then if [ "${verbose:=0}" = 1 ]; then ConsoleMessage "PublishIP: scp file copy error $result" fi fi # clean up rm "$temp"