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

10.4: Retrieve WAN IP via script and Automator Network
I often access my G5 and my home network at large from the office. The problem is my bad connection, NAT, and not having a static IP. It's usually good enough that I only have to check my WAN IP address once a day. The problem is I'm going on vacation for a while, and would like to be able to 'check in.' So, between my abysmal scripting and Automator skills, I think I've got something worked out.

So, the first thing is a bash script that goes out to the web and gets the IP address of the machine it's running on. If things worked, it will print a message saying:
Here's the current IP Information for DuoBook

XX.XX.XX.XX
As far as I can tell everything ran fine.
Next, I compiled an Automator workflow (download) into an application to email me that output, and clean up the temp files. It's running in a cron job.

It seems to be running fine, although, like I said my scripting / Automator skills are terrible, so it could break. If anyone sees anything wrong, could you let me know before I leave for that vacation. You'll have to tweak the workflow, as it's got my email built in, and it relies on knowing where the script is.
    •    
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[20,186 views]  

10.4: Retrieve WAN IP via script and Automator | 13 comments | Create New Account
Click here to return to the '10.4: Retrieve WAN IP via script and Automator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Retrieve WAN IP via script and Automator
Authored by: notverypc on Jun 07, '06 07:58:06AM
I wanted to do a similar thing with my home network. My solution was to use AppleScript and mail. I would email my home Mac and in return it would email me my WAN IP address. In Mail I setup a Rule that would run the following AppleScript. Fairly simple and effective.

set response to do shell script "/usr/bin/curl http://checkip.dyndns.org/"
set ip_address to extract_ip(response)
set mailto_addr to "me@my.domain.com" -- Change to your email address
send_ipget_mail_message(mailto_addr, ip_address)

on send_ipget_mail_message(email_address, ip_address)
	tell application "Mail"
		activate
		set composedMessage to (a reference to (make new outgoing message 
			at the beginning of outgoing messages))
		tell composedMessage
			make new to recipient at beginning of to recipients 
				with properties {address:email_address}
			set the subject to ip_address
			set the content to ip_address
		end tell
		send composedMessage
	end tell
end send_ipget_mail_message

-- Function to extract ip from HTML returned by dydns.com
on extract_ip(this_text)
	set clean_ip to ""
	set this_char to ""
	repeat with this_char in this_text
		set this_char to the contents of this_char
		if the this_char is in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."} then
			set the clean_ip to the clean_ip & this_char as string
		end if
	end repeat
	return the clean_ip
end extract_ip


[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: wsdr on Jun 07, '06 08:04:31AM

Why not just get a dyndns.org account and install one of the DynamicDNS updater apps? I do this on several computers that I have to access remotely, and it works great. The account are free for up to 5 active dynamic addresses.



[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: designr on Jun 07, '06 09:11:40AM

Almost every home DSL/Cable router I've seen in the past few years has directly supported dyndns.org accounts. Look for the dyndns tab in your router's setup page.



[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: delta592 on Jun 07, '06 07:57:11PM
Copuple of alternatives can be run from the Terminal:

curl -s http://whatismyip.com | awk '/Your/ {print $4}' | awk -F\< '{print $1}'



You could use this if you have lynx installed:

lynx -dump http://whatismyip.com | awk '/Your/ {print $4}'

[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: unforeseen:X11 on Jun 08, '06 07:23:40AM
Simply use www.whatismyip.org and you get nothing but your IP back. Much easier.

---
this is not the sig you`re looking for.

[ Reply to This | # ]

10.4: Retrieve WAN IP via script and Automator
Authored by: joshturse on Jun 08, '06 08:57:40AM
Another alternative would be noip - www.noip.com - works well, free for the first 10 or so computers.

[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: phillymjs on Jun 07, '06 11:03:09AM
Here's an Applescript I use to report IP address changes via a message sent through Mail.app. I save it as an application and run it regularly using a cron job.

try
set currentIP to (do shell script "curl -sS http://whatismyIP.org";)
on error
quit
end try
try
set lastknownIP to (do shell script "cat /Users/Shared/externalIP.txt")
on error
set lastknownIP to "0.0.0.0"
do shell script "echo " & currentIP & " > /Users/Shared/externalIP.txt"
end try

if currentIP is not lastknownIP then --if the IPs are not the same, then report it
set mailSubj to "New IP: " & currentIP
tell application "Mail"
set theMessage to (make new outgoing message with properties {subject:mailSubj, content:"IP address has changed.", visible:false})
tell theMessage
make new to recipient at end of to recipients with properties {name:"to recipient", address:"torecipient@company.com"}
make new cc recipient at end of cc recipients with properties {name:"cc recipient", address:"ccrecipient@company.com"}
end tell
send theMessage
end tell
do shell script "echo " & currentIP & " > /Users/Shared/externalIP.txt"
end if

---
--
Ohhh, WHY did I register with Insta-Trace???

[ Reply to This | # ]

10.4: Retrieve WAN IP via script and Automator
Authored by: ghay on Jun 07, '06 11:41:23AM

Why not have your home network machine access a webpage on a static ip?
Use a refresh tag to force it to re-load the page - every 30 minutes or so.

Then just look in the server logs, or have the specific page email you the request address.

My choice for finding WAN ip is whatismyip.org.



[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: scmckenzie on Jun 07, '06 11:47:46AM

Thanks guys for the tips!

I'm embarrassed to say I've never really paid much attention to applescript. Phillymjs, your script, if I understand it, is great. I really don't need the e-mail if the IP doesn't change.

As far as the dynamic dns updater thing went, I know I've tried in the past to set something up with that and for some reason or another failed miserably.



[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: phillymjs on Jun 07, '06 12:25:52PM

My script only reports the IP the very first time it's run and when it detects a change, otherwise it stays silent-- so you did understand it correctly.

You should definitely get better acquainted with Applescript, it can do a lot of nifty stuff, especially coupled with the ability to run Terminal commands via "do shell script"

~Philly

---
--
Ohhh, WHY did I register with Insta-Trace???



[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: zane on Jun 07, '06 04:34:26PM
As fan of home-made solutions, I like the Applescript/Automator routes, but if want to look at other options, Loopware have a hand little utility, "IP Menu", which will do just what you want. IP Menu places a little icon in your menu bar, which lists both local and external IP. In the preferences you can assign polling intervals, and give it an email address. IP Menu will post you an email any time it detects an IP change.

I used this utility for about 6 months or so a little while ago, for the same purposes as yours, and it never failed me. I haven't been remoting into my home computer as much lately, so just haven't gotten around to re-installing it after my last System re-bless.

The only minor downside I found was that the emails are sent thru Loopware's own servers, you can't tell IP Menu to use your own mail servers. So in other words, you could be potentially furnishing Loopware with your many IP's, for whatever that's worth, but I'm not that paranoid, so I didn't care.

You can download IP Menu from here: http://www.loopware.com/software/

Not quite as fun as the homebrew solutions, but it *does* work, and was reliable for me, for the time I used it.

(I have no affiliation with Loopware, btw)

[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: bitwise on Jun 07, '06 06:11:26PM

If the home machine automatically sends an email, I've found it possible to get its ip address from the full header of the email.



[ Reply to This | # ]
10.4: Retrieve WAN IP via script and Automator
Authored by: unforeseen:X11 on Jun 09, '06 01:05:06AM
The widget Network Stat in its latest version can send emails if your IP changes to as many addresses as you like. Easy as that.

---
this is not the sig you`re looking for.

[ Reply to This | # ]