Click to See Complete Forum and Search --> : Email myself my IP address...
BoysDontCry
06-08-2001, 02:40 PM
I have a cable modem connection at home with a dynamic IP address. I would like to set up a script that will take my IP address and email it to my email account at work.
I was going to do this by just doing ifconfig | sendmail .... but there's a problem with that. For some reason, my ISP has set it up so that my ifconfig doesn't show me my real address. To get my IP, I had to look myself up on ICQ.
Does anyone know how I can get my IP some other way? Any ideas of how I can write a script to do this? It'll have to run whenever my box reboots, plus a couple times a day just to make sure that my IP hasn't changed...
Bradmont2
06-08-2001, 03:21 PM
I do something very similar to this. What mine does is run every hour from a cron job, which stores my IP in a file. Every hour, it compares my current IP to the one in the file, and if it's different, mails it to a webbased email account.
I don't have the whole thing accessable ATM, but here are the basics:
to get your IP info, do ifconfig | grep inet > somefile
then compare the results of that with the stored one.
to email the stuff to you, you can do:
mail my@email.address < somefile
I don't have all the script here, but that should be enough to get it working ;)
[ 08 June 2001: Message edited by: Bradmont ]
Strike
06-08-2001, 05:43 PM
Bradmont, ifconfig doesn't work right for him ... they probably have the cable modem get its own IP and then DHCP you an internal one. I can't think of an easy solution to this, but http://www.whatismyip.com will return it for you :) If you want to write a script to telnet to port 80 on that domain and then do a "GET" and then mail it to you, that'd work.
FoBoT
06-08-2001, 05:53 PM
there are scripts for doing this available here (http://support.dyndns.org/dyndns/clients/unix.shtml)
unless i am totally misunderstanding what you are trying to do
BoysDontCry
06-08-2001, 10:02 PM
I'm hoping to do this by writing my own little script. I tried telnetting to port 80 and doing a GET on www.whatismyip.com, (http://www.whatismyip.com,) but it didn't like me connecting that way or something. I got "can't find requested script" (or something like that) errors.
Any more ideas? I'm sure that this is something that a lot of people have done before...
Strike
06-08-2001, 10:12 PM
BDC, check the link that FoBoT put, it's got some scripts just for your need :)
Bradmont2
06-08-2001, 10:40 PM
Originally posted by Strike:
<STRONG>Bradmont, ifconfig doesn't work right for him ... they probably have the cable modem get its own IP and then DHCP you an internal one. I can't think of an easy solution to this, but http://www.whatismyip.com will return it for you :) If you want to write a script to telnet to port 80 on that domain and then do a "GET" and then mail it to you, that'd work.</STRONG>
Whoops... My bad....
/me should pay more attention... :o
EscapeCharacter
06-09-2001, 02:43 AM
heres a start, an expect script to get the page then you could use grep and cut and awk to chop up the output to a nice $IP variable
#!/usr/bin/expect -f
set timeout -1
spawn telnet www.whatismyip.com (http://www.whatismyip.com) 80
match_max 100000
expect -exact "Trying 209.213.123.155...\r
Connected to whatismyip.com.\r
Escape character is '^\]'.\r
"
send -- "GET /\r"
expect eof
[ 09 June 2001: Message edited by: EscapeCharacter ]
EscapeCharacter
06-09-2001, 03:27 AM
if anybody is interested i wrote up a small shell script thatll check your ip to see if it changed and email mail you the new one if it has. its not the best code but what the hay :)
#!/bin/sh
while [ true ]; do
IP=`ifconfig ppp0 | awk '/inet addr/ { gsub(".*:", "", $2) ; print $2 }'`
sleep 60
CURIP=`ifconfig ppp0 | awk '/inet addr/ { gsub(".*:", "", $2) ; print $2 }'`
if [ $IP = $CURIP ]; then
echo "IP HAS NOT CHANGED"
else
echo $CURIP >IPADDY
sendmail -f root@monsterbash.dyndns.org -i escape@linuxmail.org <IPADDY
IP=CURIP
fi
done
BoysDontCry
06-09-2001, 07:37 PM
Thanks again for your help guys. I looked through the scripts on that page. Thanks, but part of the reason I'm doing this is as a programming exercise for myself. I'm teaching myself PERL. :)
Here's what I have so far...
#!/usr/bin/perl
@text=`lynx -source http://www.whatismyip.com`;
foreach $test (@text){
# I have no idea how to match a line #with an IP address... Help. I know this #is cheating... I want to strip the IP #address from the page and compare it to #the value in /var/myip.txt. If they're #different, replace myip.txt with the #value and also email it to my hotmail #account...
if($test=~/[0-255]<br>/){
print $test;
@ip=split(/ /,$test);
$myip=@ip[3];
}
}
I'm really having trouble stripping my IP address out of the file, as I'm very new to PERL. Actually, this is my first script ever. Can someone help me with this? If I can get this, I can easily finish the script myself.
EscapeCharacter
06-10-2001, 12:33 AM
well i know absolutely no perl(its too ugly :)) but you could easily do that in a shell script with grep and cut
cat file.with.source.in.it|grep "<TITLE>"|cut -d " " -f 4
infact i bet you could add the grep and cut part to that line that gets the lynx source just pipe the output to grep
[ 10 June 2001: Message edited by: EscapeCharacter ]
EyesWideOpen
06-11-2001, 11:47 AM
This will get the IP from the page. Let us know if you need help with the other stuff.
Here are some sites to help you get started with Perl:
Perl.com tutorials (http://www.perl.com/reference/query.cgi?section=tutorials&x=14&y=5)
Perl CD Bookshelf (http://dmitry.dn.ua/library/oreilly/link/perl/)
Learning Perl and Programming Perl, published by O'Reilly (http://www.oreilly.com) are both very good books. ;)
#!/usr/bin/perl
my $ip;
my @text=`lynx -source http://www.whatismyip.com`; (http://www.whatismyip.com;)
foreach my $line (@text){
if ( $line =~ /<h1>your ip is/i ) {
$line =~ /(\d+\.\d+\.\d+\.\d+)/;
$ip = $1;
print "My IP address is: $ip\n";
}
}
BoysDontCry
06-11-2001, 02:48 PM
Once again, thanks for your help guys. I managed to get my script to work today. Tonight I'll be creating my first cron job. :)
The Kooman
06-13-2001, 03:13 AM
Using perl, etc. seems to be an overkill to me! If you send a mail
to yourself, wouldn't the mail header show the IP address of the
originator of the mail? So all you'd have to do is simply send a mail
to yourself - "sendmail ...". At your workplace just see the mail
headers for the IP address it has come from.
... Am I missing out something here??!
--
Koo