Alternative to DDNS: Mailing myself my IP address?
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Alternative to DDNS: Mailing myself my IP address?
In the past I have had a lot of trouble with DDNS services and client software (both provided by my linksys router and through ddclient). I was thinking the easiest solution would be to simply have the computer I need to work on e-mail me its IP address every so often. Is there a small program or script that does this? Better yet, is there one that would e-mail me only when the IP address changes?
Another benefit (as I see it) of this approach would be that this machine wouldn't be sitting on the web quite so wide open as it would with a DDNS address. I am the only one that needs access to this machine remotely anyway.
Thanks much,
Jeremy
p.s. This is a repost from the networking forum. I got no replies there so I changed the title and re-tried here.
I'm not happy with the sed command but maybe someone will clean that up for us. The script below should work if you have only one IP. You might need to change some of the paths to ifconfig, sed and cat, if they aren't the same as my system. At any rate this should give you a start.
Code:
#!/bin/bash
# grab current IP
current_ip=`/sbin/ifconfig | /bin/sed -n -e 's/\(.*\)inet addr:\(.*\)B.*$/\2/p'`
# populate file with current IP, if it doesn't exist
if [ ! -f /tmp/original_ip ];then
echo "$current_ip" > /tmp/original_ip
fi
# place original IP into variable
original_ip=`/bin/cat /tmp/original_ip`
# if original IP and current IP don't match send email and update /tmp/original_ip
if [ "$current_ip" != "$original_ip" ];then
echo "$current_ip is the new IP address" | mail -s "[New IP]" you@mail.server.net
echo "$current_ip" > /tmp/original_ip
fi
Well that looks pretty useful except that I sit behind a linksys router, so ifconfig should always report my IP address as 192.168.1.2 (It's static). :-/ Someone suggested using wget and mailx to perform a similar function. Does that sound workable?
Well that looks pretty useful except that I sit behind a linksys router, so ifconfig should always report my IP address as 192.168.1.2 (It's static). :-/ Someone suggested using wget and mailx to perform a similar function. Does that sound workable?
I'm not familiar with your setup but does the external IP show up in a "traceroute google.com" from the internal machine? Does the IP/MAC show up in an "arp -a" on the internal machine, probably not but thought I'd check. If either of those shows the outward facing interface the current IP can be placed into a variable.
Kent
Here's an idea. Why not just use chekip.dyndns.org? It'll list the IP it recieved the connection from. So far I've found it is very reliable (uptime-wise), free, and simple.
#!/bin/bash
### set variables ###
# change those which don't work locally
# fill in the appropriate blanks
# cmd variables
SED=`which sed`
CURL=`which curl`
ETH='eth0'
TMP='/tmp/original_ip'
# the URL that will return your current IP address
CHECKIP='checkip.dyndns.com'
# put your e-address here, e.g. you@mail.server.net
E-MAIL=''
SUBJ='[New IP]'
# grab current IP
IP1=`$CURL -# $CHECKIP | sed -nr 's,^.*: ,,;s,<.*$,,p'`
# populate $TMP with current IP, if it doesn't exist
[ -f $TMP ] || echo $IP1 > $TMP
# place original IP into variable
IP0=`$CAT $TMP`
# if original and current IP's don't match,
# send e-mail and update $TMP
if [ "$IP1" != "$IP0" ]; then
echo "$IP1 is the new IP address" | mail -s "$SUBJ" "$E-MAIL"
echo "$IP1" > $TMP
fi
WARNING: Except for the curl|sed statment, I haven't debugged or otherwise checked this.
I'm sitting behind a SmoothWall Express (dedicated firewall) & the curl part works for me; OTOH, traceroute google.com (or better, traceroute -m2 google.com) gives not my IP address, but that of my ISP's server.
As to the sed command, I heartily recommend absorbing the "FM" -- there is a lot of neat stuff there. 2 of the "tricks" I have used here are:
',' instead of '/' (any character is allowed)
"-r" to avoid escaping meta-characters
Both of these add considerably to the readability of the regex(es), which can otherwise look like a sparring match between Chuck Norris & David Carradine.
Wow. That seems very complicated to me (I cannot script at all) but it definitely looks workable. In the other thread I started, someone suggested a similar approach:
What do you think of this simple wget script? It is what I have been using since yesterday and it seems quite workable.
While I'm thinking of it: where did you learn how to script? I really shouldn't have to ask other folks to write scripts this simple for me. Do you have any recommended resources for learning bash scripting? Perhaps a book or an online source?
If the wget script is working for you, then keep using it.
There are couple of reasons that my/kbrede's script is more complicated:
1st, it is going a step further & parsing the checkip.dyndns.org output to isolate the IP addr. -- ask a programmer for a value & you're likely to get it. I think that both kbrede & I were assuming that you wanted the isolated value for input into another script, say something that would add it to a hosts file. If just having the contents of the web page is enough, then stick w/ that. Furthermore, there is less chance of a false positive if the answer format changes -- as 1ojnab pointed out in his comments about his script.
2nd, kbrede was writing for a general case, giving you something that could be tuned for his, my, or your computer. He also took many repeated values & made them variables, making it easier to change them w/o accidentally breaking the script. What I did was to substitute checkip.dyndns.org for ifconfig as the source of the IP, & curl for wget as the method of getting it. I also made it still more general & added some of my own programming style preferences.
As to how to learn scripting. There are 2 aspects to it:
1. programming methods & bash syntax.
2. the "unixverse" (excuse the deliberate pun, I couldn't resist ) of available command to use.
I don't have copy of Learning the bash Shell, Third Edition, but I believe it will help w/ both aspects, more #1 than #2. Linux in a Nutshell, Fifth Edition will help w/ both, but more #2 than #1. Since a shell script is nothing more than file of the commands you would have typed, first learning the commands you need may be more important than "fancy" shell tricks.
I just located a copy of Learning the Bash shell, second edition. If that's new enough than I'll start with that. I don't have either of those other books though, so I might want to look into those once I'm done with "Learning the Bash shell." And you're right that I should probably learn my way around the shell a bit more. For the past several years just the basics (chmod, mv, cp, etc...) plus some Gentoo specific commands have been enough, but I'm starting to run into issues such as this where I could use some more general knowledge of the shell (and of course shell scripting).
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.