-
Script To Email Public IP Address
I have a weird request. My background is networking and I have been using Linux for the better part of a decade but I think I might have boldly gone beyond my mediocre knowledge. Here are the facts: I have cable internet access through Cox at my house. They are so paranoid that their subscribers might be running servers from their home accounts, that they expire the IP leases of their customers at least every two weeks and force a new public IP address. Anyway, I'm not trying to host an illegal server or anything but I would like to ssh to my home network, while I'm at work and pull some files I keep on my personal boxes. This becomes quite difficult if Cox changes my darn IP address.
Here's what I would like to get: a simple shell script that runs on one of my home boxes, that would determine my current public IP address, say like at 6 AM and 12 Noon and email it to me at work. That way, if it changes, I will still be able to have access. Yes, I am aware of the security ramifications but being that I also run my network at work, I'm fairly confident I can keep this secure. Can anyone offer me a sample shell script? I have zero talent with writing anything like this and would be most greatful. Thanks in advance!
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
Well, you could do that, but I think a more convienient solution might be dyndns.
~psi42
-
I'm actually already signed up for DynDNS. I must be missing somthing, how could that help me? I'm not trying to use a webserver or anything, I just want to talk to my boxes. I guess I'll poke around there a bit but it seems like like a bit of overkill for what I want to do. Thanks for the help anyway!
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
If you are running a DynDNS update client on your home LAN, it will notify DynDNS when your IP changes.
So all you have to do is ssh to your DynDNS hostname, and it should work...
~psi42
-
Ahh, I'm starting to get the idea! OK, thanks! Let me play around with this and see what happens. The only this is, the box I was going to use for this IP checking is really old and more of an intentional honeypot, than anything. It's still set at localhost.localdomain. I gather I'd have to make it more serious and give it a real name, right?
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
I wrote a bash script a while ago that email's me my public IP if it changes.
Code:
#!/bin/bash
touch old_ip.dat
wget whatismyip.org
read T1 <index.html
read T2 <old_ip.dat
if [ "$T1" = "$T2" ]; then
echo "IP Is the same doing nothing";
else
(echo "From: home@homesystem.com"; echo "To: myemail@domain.com"; echo "Subject: IP Address Update"; echo; echo "Computer's IP Changed. The new IP is:"; cat index.html) | sendmail -f home@homesystem.com myemail@domain.com
fi
rm old_ip.dat
mv index.html old_ip.dat
Hope this helps.
--------------------------------------------------
My Website: http://www.suramya.com
My Blog: http://www.suramya.com/blog
Registered Linux User #: 309391
--------------------------------------------------
*************************************************
Disclaimer:
Any errors in spelling, tact, or fact are transmission errors.
*************************************************
-
psi42:
I tried the whole DynDNS thing and found it somewhat intrusive. I agree that it would work but although there are Linux clients, it seems more "Windows-centric." I think I'll try the script that Suramya provided and report back on my results. Thanks to both of you for your help!!!
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
you could put an ifconfig request in cron and then have it email you the output. not quite sure of the wording. that only works if your computer is hooked directly to the modem. if a router is involved something like dyndns would be your only option. being as you cant get your IP from your router directly.
soule
Anarchism is founded on the observation that since few men are wise enough to rule themselves, even fewer are wise enough to rule others. - Edward Abbey
IRC #linuxn00b
Support your Distro.
Slackware Store
Archlinux Schwag
-
Suramya,
After playing around with your script a bit, I realized that it wasn't worth the liability of running sendmail. In my case, sendmail poses unjustified risks for me, although it might be perfectly fine for you. However, you did show me how to capture the current IP address, so I'm going to take part of your script and find a way to get access to it at work. Regardless, again, thanks for all the help!!!
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
I've used dyndns for doing what you want, the updating client was a perl script but I can't remember what it was called. It worked just fine, the only problem I had was getiing told off for using ssh, apparently it set off the network monitors where I worked or something like that.
-
i made a set of scripts that I use for the purpose of dyndns - one bash script and two python scripts.
If you use these, make sure that you name the files as I specified, since the chkanddyn.sh script references the first two.
Just run chkanddyn.sh in a cron job or something. One minor modification is required first ... you must specify your username, password, and domain in the USER_DATA
variable at the top of the chkanddyn.sh script.
file name: chkip.py
Code:
#!/usr/bin/python
from types import *
from httplib import *
import re
import sys
url = "checkip.dyndns.org"
path = "/"
conn = HTTPConnection(url)
conn.request("GET", path)
resp = conn.getresponse()
str = resp.read()
regex = re.compile("Current IP Address: [\\d.]+")
match = regex.search(str)
if isinstance(match, NoneType):
sys.exit(1)
str = str[match.start():match.end()]
regex = re.compile("[\\d.]+")
match = regex.search(str)
str = str[match.start():match.end()]
print str
sys.exit(0)
file name: dyndns.py
Code:
#!/usr/bin/python
from httplib import *
import base64
import sys
if len(sys.argv) < 5:
print "usage: dyndns.py IP USERNAME PASSWORD HOSTNAME"
sys.exit(1)
userpass = base64.encodestring("%s:%s" % (sys.argv[2], sys.argv[3]))[:-1]
url = "members.dyndns.org"
path = "/nic/update?system=dyndns&hostname=%s&myip=%s" % (sys.argv[4], sys.argv[1])
xheader = { "Host" : "members.dyndns.org",
"Authorization" : "Basic %s" % (userpass),
"User-Agent" : "mydyndns/1.0 vod12481@hotmail.com" }
conn = HTTPSConnection(url)
conn.request("GET", path, headers=xheader)
resp = conn.getresponse()
str = resp.read()
if str.find("good") != -1:
print "dyndns update ok!"
sys.exit(0)
elif str.find("nochg") != -1:
print "dyndns update ok (nochg)!"
sys.exit(0)
else:
print "error occured on dyndns update!"
sys.exit(2)
file name: chkanddyn.sh
Code:
#!/bin/bash
USER_DATA="USERNAME PASSWORD DOMAIN"
PREV_IP=`head --lines=1 ~/.ip.tmp &2> /dev/null`
CURR_IP=`chkip.py`
if [ $? -eq 1 ]
then
exit
fi
if [ "$CURR_IP" != "$PREV_IP" -o "$1" = "force" ]
then
echo "IP has changed from $PREV_IP to $CURR_IP, updating dyndns ..."
dyndns.py $CURR_IP $USER_DATA
echo $CURR_IP > ~/.ip.tmp
else
echo "IP ($CURR_IP) has not changed, skipping dyndns update ..."
fi
echo "Done!"
-
Thanks everyone! I will try these out and let you know!
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
You don't need Sendmail just to send mail from your box. You can run something that doesn't open a port like NullMailer (it's what I use). NullMailer is a simple relay agent that will forward all outbound mail to your ISP's mail server.
I run DynDNS with a Python script from their site. It works flawlessly and is very convenient.
-
You don't need Sendmail just to send mail from your box.
Thanks to all who replied! Interestingly enough, I found an acceptable way to use DynDNS, without using a client at all. Unfortunately, I'm stuck in the Windows world (not all the way but to a large extent) at work, so I'm limited to Putty and Webmin to control my home Linux boxes. Ok, here's how I get my IP address without the client and I apologize if this is a blinding flash of the obvious to anyone. I simply ping the dynamic address set up for me by DynDNS. Now, I don't respond to the ping but I do get the attempt to connect to IP xxx.xxx.xxx.xxx. The minute I see that, I know what my IP address is! The ping fails but I still see the resolution to the IP address. Problem solved! Or, is that problem created? Now, I'm somewhat exposed. Oh well, can't think of doing anything else without spending lots of money.
I equivocate, therefore I might be.
My Linux/Unix Boxes:
Home: Slackware 10, CentOS 5.3, RHEL 5, Ubuntu Workstation 9.10, Work: RHEL 5, CentOS 5
-
Have a look at:
http://www.justlinux.com/forum/showt...threadid=85478
You could have that script executed when your dhcp address is changed, check your doc for the dhcp client that your are using for which file to place the the call to the script.
search first, ask second.
gui tool? what is a gui tool?
I want my 5 stars back.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|