LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Closed Thread
 
Search this Thread
Old 12-29-2006, 05:36 PM   #1
shortname
Member
 
Registered: Dec 2004
Location: Georgetown, DE
Distribution: Gentoo (Kernel-2.6.18-gentoo-r5, customized :))
Posts: 101

Rep: Reputation: 15
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.
 
Old 12-30-2006, 11:39 AM   #2
kbrede
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Rep: Reputation: 0
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
 
Old 12-30-2006, 06:31 PM   #3
shortname
Member
 
Registered: Dec 2004
Location: Georgetown, DE
Distribution: Gentoo (Kernel-2.6.18-gentoo-r5, customized :))
Posts: 101

Original Poster
Rep: Reputation: 15
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?
 
Old 12-30-2006, 07:07 PM   #4
kbrede
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by shortname
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
 
Old 12-30-2006, 11:31 PM   #5
shortname
Member
 
Registered: Dec 2004
Location: Georgetown, DE
Distribution: Gentoo (Kernel-2.6.18-gentoo-r5, customized :))
Posts: 101

Original Poster
Rep: Reputation: 15
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.
 
Old 01-02-2007, 12:36 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Would this work?:
Code:
#!/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.
 
Old 01-02-2007, 02:11 PM   #7
shortname
Member
 
Registered: Dec 2004
Location: Georgetown, DE
Distribution: Gentoo (Kernel-2.6.18-gentoo-r5, customized :))
Posts: 101

Original Poster
Rep: Reputation: 15
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:

http://www.linuxquestions.org/questi...d.php?t=514640

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?

Thanks much!
Jeremy
 
Old 01-02-2007, 03:45 PM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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.

Linux Cookbook: Tips and Techniques for Everyday Use, Second Edition3 is a good mix & works from a practical, rather than theoretical standpoint.
 
Old 01-02-2007, 10:25 PM   #9
shortname
Member
 
Registered: Dec 2004
Location: Georgetown, DE
Distribution: Gentoo (Kernel-2.6.18-gentoo-r5, customized :))
Posts: 101

Original Poster
Rep: Reputation: 15
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).

Thanks again everyone,

Jeremy
 
Old 01-03-2007, 12:45 AM   #10
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
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.

Discussions continues here.
 
  


Closed Thread

Tags
ddns, dns, dynamic


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DDNS with BIND pcmilhouse Linux - Networking 5 09-20-2007 12:03 AM
DDNS help on linux generalachoo Linux - Software 5 12-23-2006 10:29 PM
Ddns hiieu Debian 3 11-10-2006 03:39 PM
DDNS in router does not renew IP address; host and dig report incorrect IP Bruce Hill Linux - Networking 9 10-19-2006 04:10 PM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 05:33 AM


All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration