Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Retrieve home IP address remotely via email Apps
I've got cable internet. Despite the rare outages, I'm reasonably satisfied ... the only annoying thing is that every once in a while my provider will change my publicly routeable address via DHCP. As you might imagine, this prevents me from being able to ssh from work to home, which I do a lot.

First half of the solution: A cron job that fires off every morning to email my IP address to my address at work at 6:00am:
/Users/me/scripts/getip | mail -s "my IP at home" myemail@work.com
Second half of the solution: Here's an AppleScript which I attach to a rule in Mail.app. For the rule's conditions, set the Subect field to match on a keyword like "sendmemyipaddress." You can use your own keyword, of course. In the Actions section, have it perform an AppleScript, and point it to a compiled copy of the following script:
on perform_mail_action(info)
 set my_ip to (do shell script "/Users/me/scripts/getip")
 set my_dest to "email@work.com"
 set my_subj to "My IP at home"
 
 tell application "Mail"
  set selectedMessages to |SelectedMessages| of info
  set theRule to |Rule| of info
  repeat with eachMessage in selectedMessages
   activate
   GetURL ("mailto:" & my_dest & "?subject=" & my_subj 
    & "&body=" & my_ip)
   send outgoing message 1
  end repeat
 end tell
end perform_mail_action
The purpose behind this is to have a backup in case my provider changes my ip AFTER my mac has emailed my ip address to work -- I just have to send myself an email with the subject "sendmemyipaddress."

With a little imagination, you can adapt this script to things like X11 home automation. Say you are on vacation and you'd like to turn your house lights on, well, just email your Mac at home. One caveat is you have to keep Mail.app launched and set to pull mail from your provider every so often (ie not on Manual).

Credits: All props go to Jerry LeVan for his excellent perl script to fetch the public IP from a Linksys cable router (the "getip" script noted aboev), and to jbs and grrl-geek for their help in getting my thick skull around AppleScripting Mail! Thanks!

[robg adds: You can also use a service like dyndns.org to give yourself a functional hostname. By running the dyndns client, your machine will automatically inform the network of any changes in its IP address. However, the above solution is still interesting in light of the potential to use inbound email to have your machine do certain things.]
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[44,803 views]  

Retrieve home IP address remotely via email | 25 comments | Create New Account
Click here to return to the 'Retrieve home IP address remotely via email' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Retrieve home IP address remotely via email
Authored by: bleachbypass5279 on Jun 12, '03 12:36:07PM

I would recommend the dyndns route that robg mentioned at the end of the hint. It was pretty easy to set up AND it helps me reroute requests on port 80 to an alternate port as my ISP blocks incoming requests on that port.



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: abesapiens on Jun 12, '03 12:59:57PM

My cable modem provider (Comcast) doesn't allow dyndns as part of their AUP. I think its so they can sell static IPs on their expensive work at home plan... :^(



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: Fofer on Jun 13, '03 12:49:24AM

Exsqueeze me? Comcast doesn't allow it? And how exactly do they police it? Doesn't the service work entirely externally, simply linking your current IP to an "dyndns.org" address of your choosing, all in their own online database?

(I ask so pointedly because I am a MediaOne/AT&T Broadband customer, in the process of being assimilated by Comcast.)

...and I use dyndns.org to maintain access to my calendar, contacts, photo album and more on the road! It works perfectly well for the home office, and I'm happy with how it works.

The day Comcast prevents me from using this third party's service (without offering a static IP at a comparable price) is the day I cancel my subscription with them. And move over to DSL!



[ Reply to This | # ]
dyndns
Authored by: WAW401 on Jun 12, '03 07:21:17PM

dyn dns is so easy ... and here's proof:

I am the master of my firm's DNS zone file, so I could add an entry for my home computer NO problem. However, I choose dyndns because it allows me to have a fully qualified DNS and even alerts me if it's about to expire (I haven't perfected automatic IP updates ... It's only once a month, so 12 times a year doesn't bother me).



[ Reply to This | # ]
NETGEAR Gateway RT314
Authored by: ces3001 on Jun 12, '03 01:59:13PM

I've got a NETGEAR Gateway Rt314 router (Firmware V3.20(CA.1) | 10/27/2000). This script needs to be modified as follows:

 #!/usr/bin/perl
 #
 # retrieve the current ip address from the NetGear router...
 # based on 
 # Jerry LeVan (jerry.levan@eku.edu)
 # Mac OS X 10.1.4
 # May 12,2002
 # Modified by Cecil Esquivel (ces(@)alum.mit.edu) June 11, 2003
 # for NETGEAR RT314
 #
 my ($pattern,$thePage,$theAddress,$wan,$ip,$theip);
 
 #define the pattern we are looking for...
 $wan = 'WAN Port :[^:]*';
 $ip = 'IP Address :[ ]*\n';
 $theip = '(\d+\.\d+\.\d+\.\d+)[ ]*';
 $pattern = $wan.$ip.$theip;
 
 # Fetch the page
 $thePage = `curl -s -u admin:*youradminpassword* http://192.168.0.1/mtenSysStatus.html`  ;
 # Look for the current IP embedded in the Status Page
 if ($thePage =~ m|$pattern|)
 {
  $theAddress = $1;
  print $theAddress,"\n";
  # here you could mail the address ...
 }
 else
 {
  print "Problem getting address...\n";
 }  


[ Reply to This | # ]
NETGEAR Gateway RT314
Authored by: ces3001 on Jun 12, '03 02:04:16PM

For some reason, the HTML code in the post was taken literally, even though I used the ampersand notations. (Problem: They get switched to real HTML angle brackets when I het the Preview button). Hmmm.

Well, Here goes again.

This is the modified routerip.pl script mentioned in the original post.

 #!/usr/bin/perl
 #
 # Retrieve the current ip address from the NetGear router...
 # based on LinkSys router code by
 # Jerry LeVan (jerry.levan@eku.edu)
 # Mac OS X 10.1.4
 # May 12,2002
 # Modified by Cecil Esquivel (ces(@)alum.mit.edu) June 11, 2003
 # for NETGEAR RT314
 #
 my ($pattern,$thePage,$theAddress,$wan,$ip,$theip);
 
 #define the pattern we are looking for...
 $wan = 'WAN Port :</B></FONT></TD>[^:]*';
 $ip = 'IP Address :[ ]*<B>\n';
 $theip = '(\d+\.\d+\.\d+\.\d+)[ ]*</B>';
 $pattern = $wan.$ip.$theip;
 
 # Fetch the page
 $thePage = `curl -s -u admin:*youradminpassword* http://192.168.0.1/mtenSysStatus.html`  ;
 # Look for the current IP embedded in the Status Page
 if ($thePage =~ m|$pattern|)
 {
  $theAddress = $1;
  print $theAddress,"\n";
  # here you could mail the address ...
 }
 else
 {
  print "Problem getting address...\n";
 }  


[ Reply to This | # ]
find out your IP address from whatismyip.com
Authored by: hayne on Jun 12, '03 02:29:57PM
There are a few web sites that can be used for finding out your (external) IP address. One of them is www.whatismyip.com Of course it is better to obtain this info from your router if you can (as in the script used in the hint) but if you can't get such a script to work, here is a Perl script that gets the IP address from the HTML page that is returned by www.whatismyip.com
#!/usr/bin/perl
my $url = "http://www.whatismyip.com/";
my $html = `/usr/bin/curl -s -f  $url`;
if ($html =~ /Your ip is (\d+\.\d+\.\d+\.\d+)/i) 
{
    my $ipaddr = $1;
    print $ipaddr;
    print "\n"; # remove this line if newline at end not wanted
    exit;
}
print STDERR "Failed to get IP address from $url\n";
exit;


[ Reply to This | # ]
backslashes missing
Authored by: hayne on Jun 12, '03 02:34:40PM

It's really frustrating to enter code in these comments since the Preview lies through it's teeth about what will be shown.
In the above script that I posted, there are missing backslashes in several places:
- before each of the 4 d's in the pattern
- before the n's in two of the print statements



[ Reply to This | # ]
Use the special tags...
Authored by: robg on Jun 13, '03 08:24:44AM
Set the mode to html and then write your code like this:
[code]
some <examples> of code \ and \ backslashes
[/code]
Then when you hit publish, everything will be retained. This will not work if you use "pre" or "tt" tags! And yes, they are SQUARE brackets around "code," not angle brackets. I need to add the instructions to the comment posting page; they are already on the story posting page...

-rob.

[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: robJ on Jun 12, '03 02:50:35PM

Can't one just inspect the headers of the email to find the IP address that the message was sent from?



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: grrl_geek on Jun 12, '03 05:15:42PM

Yes, but I think headers would be a nightmare to parse, especially with something as clunky as AppleScript.

---
~~~~~~~~~~~~~
Sinker sucker socks pants, apocryphal awry!



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: robJ on Jun 12, '03 06:26:33PM

I didn't mean with AppleScript - I meant with eyeballs. ;-)



[ Reply to This | # ]
missing something?
Authored by: foodragon on Jun 12, '03 05:45:10PM

Is it me, or am I just missing something? I've got the whole script down, but when I try to create the rule in mail I don't see any perform apple script under the action section.

---
_rob



[ Reply to This | # ]
missing something?
Authored by: robJ on Jun 12, '03 06:29:24PM

Which version of Mail? I don't think it was available in the early releases.



[ Reply to This | # ]
missing something?
Authored by: foodragon on Jun 12, '03 09:50:58PM

It's version 1.2 (v544/552)

---
_rob



[ Reply to This | # ]
missing something?
Authored by: robJ on Jun 13, '03 12:00:41PM

On my OS X 10.2.6 system, the version of Mail is 1.2.5 (v552) and it has the AppleScript option in the rules.



[ Reply to This | # ]
IP Reporter
Authored by: Fofer on Jun 13, '03 12:53:36AM

A handy piece of $5 shareware will take care of this for you, too:
http://www.versiontracker.com/dyn/moreinfo/macosx/7413
It can report your IP address, email it to you, or post it to a web page for access anywhere.
I used it on OS 9 for awhile and it was indispensable. (There are more like it too, on VersionTracker.)

Although I use dyndns.org now - it works well, I can easily share web pages hosted on my Mac with friends and family without giving them annoying-to-type numbers - and it's FREE!



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: Accura on Jun 13, '03 03:52:34AM

Lots of people have mentioned dyndns.org so i think it fair that i drop zoneedit.com in there

Its a free DNS hosting provider (up to 5 domains) that will also handle dynamic dns. that means that u can go out and buy www.ilovethisstupiddomain.com, or what ever u like, use gateway.stupiddomainnameuown.net could be your gateway and so on, all with dynamic IP's. its also lets u do email forwarding, web cloaking and all the standard DNS features.

Since domains are so cheep these days i don't see why every nerd doesn't have at leas one for email

http://www.zoneedit.com

---
"The time has come," the walrus said. "To talk of many things..."



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: jellomizer on Jun 13, '03 02:52:42PM

This is also good for Laptops as well. As a means of tracking down stolen laptops. If someone stole it and started it up on the net then you can get their IP address and one more clue on who the theaf is.



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: yubbie2 on Jun 15, '03 09:43:17PM

here's another good one:

http://www.versiontracker.com/dyn/moreinfo/macosx/19949

-jesse



[ Reply to This | # ]
From Anonymous...
Authored by: robg on Jun 17, '03 09:42:14AM
Anonymous sent this as a standalone hint, but I feel it more properly belongs here as a comment, so here it is:
In a recent post about obtaining one's IP remotely using email, there was a series of questions and comments on how to obtain the IP. Well, calm down! Send yourself an email, no body or subject necessary, and look at the headers (if you use Mail, go to "View" menu and click on "Show all Headers", I have it on my toolbar) It will be the last "Received" tag from top to bottom. There it is, your ip address.
-rob.

[ Reply to This | # ]
That's what I suggested...
Authored by: robJ on Jun 17, '03 05:48:10PM

I was basically hinting at the same technique with my question "Can't one just inspect the headers of the email to find the IP address that the message was sent from?" :-)



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: overrider on May 10, '04 11:18:09PM

Hello All,

there are many different ways to get the WAN IP and perform various actions with it. Thanks for all the great Scripts. My Problem however is that i cant get cron to create the File containing the IP for me. Below my Script
******
#!/bin/sh
ip=`curl -s http://www.whatismyip.com/ | grep '<h1>' | awk '{print $4}'` page=ip.html

echo "<html><head><title>Current IP</title></head>
<body bgcolor=white text=black>
Current IP Address: $ip<br>" > $page
date >> $page
echo '</b><br><br><PRE>' >> $page
echo '</a>' >> $page
echo '</body></html>' >> $page

echo Posting your WAN IP to the Server.....
ftp -nv ftp.server.com<< EOF
user uid pwd
put /ip.html /p35838597/ip.html
bye
****
Above Script is supposed to grap the IP, save it in a file called ip.html, and then send it to my ftp Server. When called from the Terminal sh /upload_ip.sh it works just fine, but when called via cron (user cron or /etc/crontab) it will not create the file ip.html, hence nothing to upload. I tried this as well in Perl, also with using the wget command, but same. Does anybody know wtf is wrong?

Thanks a lot,
Overrider



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: bluehz on May 11, '04 12:22:51AM

Not sure if this is the problem - but something you might want to look into. When running the script as a user - the ip.html is assumed to be created in the current working directory you are in. When running the script via cron - I don't think there is a current working directory so it may not know where to write the ip.html file. You might try putting in a full path for your ip.html like:

path=/var/tmp/ip.html

or something like that. Just a guess...



[ Reply to This | # ]
Retrieve home IP address remotely via email
Authored by: overrider on May 11, '04 02:26:55AM

Bluehz,

you are awesome, it worked flawlessly. So here for reference;

********
#!/bin/sh
ip=`curl -s http://www.whatismyip.com/ | grep '<h1>' | awk {print $4}'` page=/ip.html
echo "<html><head><title>Current IP</title></head>
<body bgcolor=white text=black>
Current IP Address: $ip<br>" > $page
date >> $page
echo '</b><br><br><PRE>' >> $page
echo '</a>' >> $page
echo '</body></html>' >> $page

echo Posting your WAN IP to the Server.....
ftp -nv ftp.server.com<< EOF
user uid pwd
put /ip.html /p35838597/ip.html
bye
******

I only added a "/" before the page.html, and it worked.Cron does not have a working directory, hence it doesnt know where to put the ip.html file. Hah, and i tried for ages to figure it out. Damn....

Thanks a whole lot for that fast reply!!!

Overrider



[ Reply to This | # ]