I’m too cheap to get a fixed IP address, and at some point recently, my router’s external IP address (i.e. the one assigned by my cable provider) changed. So I’d like to write a cron job that runs every hour and dumps my external IP address on a computer somewhere else, so I’ll always know how to connect to it.
I’m blanking on a good way to figure this out from within a cron job. If I just ssh ‘lastlog’ or ‘who’ to the remote computer, I only get my last actual login. If I try to mimic sshing in for real in a naive way (e.g. ssh $REMOTE_HOST << EOF) then I get complaints because stdin isn't a terminal. I'm pretty sure I could go down that path a little longer and fix it, but I get the feeling that I must be missing some much simpler solution to this problem: the information I'm trying to get isn't exactly a big secret, after all. And no, I don't want to log into the router and ask it, if for no other reason than that I've set up the router's administrative interface to only be accessible via a wired connection, which isn't possible from the computer where I want to run the cron job.
Post Revisions:
There are no revisions for this post.
David, may be the shell script that I use can help you.
#!/bin/bash
curl -s http://checkip.dyndns.org | awk '{print $6}' | awk ' BEGIN { FS = "<" } { print $1 } '
5/1/2006 @ 1:57 am
Alternatively there is another site that provides text output to avoid all the ‘awk’ing business. But I felt that dyndns is more reliable than other guys.
#!/bin/bash
curl "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"
5/1/2006 @ 2:02 am
I also wanted to tell you about the free DNS service provided by http://www.dyndns.com. Once you have registered a free account with them, you have an option to create an entry for your host with a good number of options of firstlevel domain names like dyndns.org, homelinux.net, etc. Once you have created the host entry, you could use the following script to update the records from your cron or whatever.
#!/bin/bash
# Set the username and password for the dyndns account
USERNAME=
PASSWORD=
# Set the system being used to either static or dynamic DNS
SYSTEM=dyndns
# Set the hostname for the record to change
DYNHOST=
# Set whether to wildcard the DNS entry, i.e. *.$DYNHOST
WILDCARD=OFF
############################################
## DO NOT EDIT ANYTHING BEYOND THIS POINT ##
############################################
if [ -z "$DYNDNS" ]; then
DYNDNS="$DYNHOST"
fi
if [ -z "$DNSWILD"]; then
DNSWILD="$WILDCARD"
fi
LOOKUP=`host $DYNHOST | cut -f3`
MYIP=`curl -s http://checkip.dyndns.org | awk '{print $6}' | awk ' BEGIN { FS = "<" } { print $1 } '`
# Do the work
if [ "$LOOKUP" = "$MYIP" ]; then
echo "No change in DNS entry."
else
echo `lynx -auth=${USERNAME}:${PASSWORD} -source "http://members.dyndns.org:8245/nic/update?system=${SYSTEM}&hostname=${DYNDNS}&myip=${MYIP}&wildcard=${DNSWILD}"`
fi
5/1/2006 @ 2:48 am
Just curious to know if that was helpful at all? :-)
5/2/2006 @ 5:47 am
Thanks, I’d read about dyndns in my router’s documentation but hadn’t realized it was free. Hmm; maybe I will give that a try.
In the meantime, your scripts do exactly what I want. I’ve taken your idea of using http and modified it somewhere: I’m instead looking up a nonexistent web page on the remote host where I’m storing my IP address (to avoid relying on two different hosts), and grepping through the Apache log to find where I connected from. Works like a charm.
5/2/2006 @ 5:32 pm
As Per pointed out, ssh also knows the answer:
ssh HOSTNAME 'echo $SSH_CLIENT'
does the trick.5/11/2006 @ 10:23 pm