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

An AppleScript to obtain the WAN IP address Internet
This my first AppleScript ever so please be kind. If you'd like to get your WAN IP address in order to SSH into your machine from outside, assuming you configured your router to forward SSH traffic (usually port 22) to your Mac, you can use this hint to obtain your WAN IP address:
  1. Copy and paste the script into a new Script Editor document.
  2. Change EMAILADDRESS to an email address of your choosing
  3. Save the script as an application
  4. Invoke it
You'll be presented with an option to email the IP address to the EMAILADRESS address, or to just leave the value in the system pasteboard for later use.

This script can be used as a basis for more tricks if required. For example, make it email the IP by default without presenting the dialog; set up an action in Mail.app for specific subject to call this script, etc. You get the idea. Just my tiny contribution for the great Mac community.
    •    
  • Currently 3.75 / 5
  You rated: 4 / 5 (4 votes cast)
 
[19,402 views]  

An AppleScript to obtain the WAN IP address | 21 comments | Create New Account
Click here to return to the 'An AppleScript to obtain the WAN IP address' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to obtain the WAN IP address
Authored by: ozzyrules on Nov 03, '04 11:22:00AM

in your script you have  instead of

Is anyone else seeing this too, or is it just me?



[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: dontlikehippies on Nov 15, '04 03:56:39PM

Change your browser's text encoding to Western (Mac OS Roman) and you'll see the right character



[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: baltwo on Nov 03, '04 04:20:29PM

Here's a script that uses a few more sites if unable to get to dyndns.org:

WanIP()

display dialog "Your IP Address is " & result
on WanIP()
set WIP to missing value
set siteList to {"http://checkip.dyndns.org/", "http://whatismyip.com", "http://www.whatismyipaddress.com", "http://ipid.shat.net/", "http://www.edpsciences.comhtbin/ipaddress", "http://www.showmyip.com/"}
repeat with thissite in siteList
try
set WIP to item 1 of paragraphs of (do shell script "curl " & thissite & " | tr -cs '[0-9\\.]' '\\012' | awk -F'.' 'NF==4 && $1>0 && $1<256 && $2<256 && $3<256 && $4<256 && !/\\.\\./'")
if WIP is not missing value then exit repeat
end try
end repeat
return WIP
end WanIP



[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: bostmass on Nov 14, '04 03:01:36AM
Thought this might be a nice one-line addition to copy the answer to the clipboard. WanIP()

display dialog "Copy this IP to clipboard? " default answer WanIP()
set the clipboard to WanIP()

on WanIP()
    set WIP to missing value
    set siteList to {"http://checkip.dyndns.org/", "http://whatismyip.com", "http://www.whatismyipaddress.com",
        "http://ipid.shat.net/", "http://www.edpsciences.comhtbin/ipaddress", "http://www.showmyip.com/"}
    repeat with thissite in siteList
        try
            set WIP to item 1 of paragraphs of (do shell script "curl " & thissite &
                " | tr -cs '[0-9\.]' '\012' | awk -F'.' 'NF==4 && $1>0 && $1

[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: bostmass on Nov 14, '04 03:03:48AM
Thought this might be a nice one-line addition to copy the answer to the clipboard.

WanIP()

display dialog "Copy this IP to clipboard? " default answer WanIP()
set the clipboard to WanIP()

on WanIP()
	set WIP to missing value
	set siteList to {"http://checkip.dyndns.org/", "http://whatismyip.com", "http://www.whatismyipaddress.com", 
		"http://ipid.shat.net/", "http://www.edpsciences.comhtbin/ipaddress", "http://www.showmyip.com/"}
	repeat with thissite in siteList
		try
			set WIP to item 1 of paragraphs of (do shell script "curl " & thissite & 
				" | tr -cs '[0-9\\.]' '\\012' | awk -F'.' 'NF==4 && $1>0 && $1<256 && $2<256 && $3<256 && $4<256 && !/\\.\\./'")
			if WIP is not missing value then exit repeat
		end try
	end repeat
	return WIP
end WanIP


[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: xone on Nov 03, '04 06:52:10PM
Hi, Here is my version of the infamous send me my ip address script. It utilises the
extract_ip(this_text)
function and shell script call to get the ip address set out in the hint above. However it is slighty neater in that it is built to be attached to a Mail rule and has no hardcoded email address.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set response to do shell script "/usr/bin/curl http://checkip.dyndns.org/"
		set ip_address to extract_ip(response)
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theSubject to subject of eachMessage
				try
					set replyMessage to reply eachMessage without opening window
					set the subject of replyMessage to ip_address
					send replyMessage
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from
If you just create a rule (subject = wanip for example) in Mail that runs this script it will reply with your WAN IP as the subject. However, Mail does seem to be a bit flaky sometimes when running rules...or is it just me :) Tom

[ Reply to This | # ]
Short, sweet, and flexible?
Authored by: DesignX on Apr 27, '05 04:27:53PM

Since I was looking to do this myself, I'm happy to see someone else has done all the work!
Here is a combination of all the (best? most elegant??) shortest! code and seemingly most flexible in these threads. (Who am I to say? I don't know my elbow from the teakettle with shell scripts and I write AppleScript like I speak Chinese.) But this works from anywhere to anywhere (flexible) and it is short.
A combination of obscure requirements in the Mail rule, a unique script name, and/or (probably better) encrypted e-mail would keep it secure.
It is a combination of code from xone, zendrew, and whoever's shoulders they're standing on.
Comments? Improvements?

Complete and ready to run:
[code]
using terms from application "Mail"
on perform mail action with messages theMessages for rule Name_of_your_mail_rule_here
set ip_address to (do shell script "curl -f http://checkip.dyndns.org | awk '{print substr($6,1,length($6)-15)}'")
tell application "Mail"
repeat with eachMessage in theMessages
set theSubject to subject of eachMessage
try
set replyMessage to reply eachMessage without opening window
set the subject of replyMessage to ip_address
send replyMessage
end try
end repeat
end tell
end perform mail action with messages
end using terms from
[/code]


---
DesignX



[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: danva on Nov 03, '04 06:55:53PM

Sorry for the mess guys. Here comes an updated version.

[code]
set response to do shell script "/usr/bin/curl http://checkip.dyndns.org/"
set ip_address to extract_ip(response)

set the Reply to display dialog "You IP address is: " & ip_address
buttons {"Send by email", "Just leave in pasteboard"}
default button "Just leave in pasteboard" giving up after 10 with icon stop

-- Leave the ip address in the Finder's pasteboard so it is accesible
tell application "Finder"
activate
set the clipboard to ip_address
end tell

-- Send en email if requested
set mailto_addr to "EMAILADDRESS"

if button returned of Reply is "Send by email" then
send_ipget_mail_message(mailto_addr, ip_address)
end if

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 "iPGet report"
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
[/code]

Thanks to everyone who tried this out



[ Reply to This | # ]
2 cents re: getting the IP from the HTML
Authored by: zendrew on Nov 04, '04 12:04:31AM
I'm getting a real kick out of the the various (ingenious) solutions people have used to strip the html tags from around the IP address. Here's my variation on that same task, using awk. Like the author of this hint, I am only querying dyndns.org. I think it's simpler just to pipe to awk in this case, since the curl output is completely predictable and easily parsed:

set TheAddress to (do shell script "curl -f http://checkip.dyndns.org | awk '{print substr($6,1,11)}'")

display dialog "Your IP is : " default answer (TheAddress)




[ Reply to This | # ]
2 cents re: getting the IP from the HTML
Authored by: gblanco on Nov 04, '04 12:31:48AM

Oops! That only works if your IP address is exactly 11 characters....

Try:

curl -f http://checkip.dyndns.org | sed ' s.[A-Za-z<>/: ]..g '

:)



[ Reply to This | # ]
2 cents re: getting the IP from the HTML
Authored by: gblanco on Nov 04, '04 12:37:33AM

... sorry, can't resist these little games....

curl -f http://checkip.dyndns.org | sed 's/[^0-9.]//g'

... is even shorter.



[ Reply to This | # ]
2 cents re: getting the IP from the HTML
Authored by: zendrew on Nov 04, '04 12:39:45AM

sweet.



[ Reply to This | # ]
2 cents re: getting the IP from the HTML
Authored by: danva on Nov 04, '04 12:35:17AM

Cute, but will stop working if IPv6 is used all of a sadden, will it not?
I actually made a point of making it in AppleScript: I am using Unix for 10+ years and, you know, what's the fun in not fighting a new language :)



[ Reply to This | # ]
2 cents re: getting the IP from the HTML
Authored by: zendrew on Nov 04, '04 01:16:12AM
Aw, heck. This is maybe my third AppleScript, which I just wrote a week or so ago. And I'm a complete newb at awk––I understood that it's for parsing text, so I googled it and learned enough to write the snippets I posted here. I'm having a lot of fun learning this stuff, and I couldn't resist throwing in my little tidbit.

Anyway, I actually think your script is excellent––very well thought out, readable and adaptable. I'm totally gonna steal from it (and the idea of using multiple sites, as posted by baltwo) to make my own script more robust. Thanks!

[ Reply to This | # ]
CORRECTION!
Authored by: zendrew on Nov 04, '04 12:38:07AM
Sorry! The script I sent in my previous post will truncate many IP addresses. I inadvertently copied that snippet from my flawed first effort at this script, in which I had (half-asleep, I swear!) simply counted the 11 characters in my IP address (that day) and plugged that number into the script. Duh. I found the mistake as soon as I ran the script at work, where I had a longer IP address.

Here's the correct version:

set TheAddress to (do shell script "curl -f http://checkip.dyndns.org | awk '{print substr($6,1,length($6)-15)}'")

display dialog {"Your IP address is: ", TheAddress} as string


[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: ozzyrules on Nov 07, '04 10:26:25PM
I modified the script to allow for the choice of email app (Mail or Entourage 2004). This is the first time I have ever touched an AppleScript script so excuse any clumbsiness or mistakes. It does work for me though. Although I am having trouble getting the email choice dialog to come to the front. It stays hidden and the script icon bounces in the dock. Can anyone help?



set response to do shell script "/usr/bin/curl http://checkip.dyndns.org/"
set ip_address to extract_ip(response)

set the Reply to display dialog "You IP address is: " & ip_address 
  buttons {"Send by email", "Just leave in pasteboard"} 
  default button "Just leave in pasteboard" giving up after 10 with icon stop

-- Leave the ip address in the Finder's pasteboard so it is accesible
tell application "Finder"
  activate
  set the clipboard to ip_address
end tell

-- Send en email if requested
set mailto_addr to "your_user@yourdomain.com"

if button returned of Reply is "Send by email" then
  send_ipget_mail_message(mailto_addr, ip_address)
end if


on send_ipget_mail_message(email_addr, ip_address)
  
  -- choose email application
  
  set the answer to display dialog 
    "Which Email application do you use?" buttons {"Mail", "Entourage 2004"} 
    default button "Mail" giving up after 10 with icon stop
  
  -- Entourage choice
  
  if button returned of answer is "Entourage 2004" then
    
    tell application "Microsoft Entourage"
      activate
      set theAccount to IMAP account "David"
      set theRecips to {address:{display name:"David", address:"name@address.com"}, recipient type:to recipient}
      
      set newMessage to make outgoing message at out box folder with properties {account:theAccount, subject:"test", content:ip_address, recipient:theRecips}
      send
    end tell
    
    -- Mail choice
    
  else
    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:"name@address.com"}
        set the subject to "iPGet report"
        set the content to ip_address
      end tell
      send composedMessage
    end tell
    
  end if
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

-- end script


[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: Gnarlodious on Nov 15, '04 12:01:35AM

Hey that looks exactly like this script here

-- Gnarlie



[ Reply to This | # ]

An AppleScript to obtain the WAN IP address
Authored by: platon on Nov 30, '04 12:12:21PM
I just wrote another AppleScript before reading this hint, and added an entry ip to my tcsh's aliases. Whenever I have to ssh to my computer I call my daughter or wife at home to type ip and hit return in Terminal (I run Terminal instead of Finder, so after reboot it's always frontmost). Here's my script:

try
	set check_ip to do shell script "curl http://checkip.dyndns.org/" as string
	set head_position to offset of "Current IP Address: " in check_ip
	set body_end_position to offset of "</body></html>" in check_ip
	set text_ip to characters head_position thru (body_end_position - 1) in check_ip as string
	set my_ip to characters (head_position + 20) thru (body_end_position - 1) in check_ip as string
	
	set display_ip to display dialog "  " & return & "       " & text_ip & return & "   " buttons ["Copy to Clipboard", "OK"] default button "OK" giving up after "150"
	if button returned of display_ip is "Copy to Clipboard" then
		set the clipboard to my_ip
	end if
	
on error the error_message number the error_number
	display dialog "Error: " & the error_number & return & the error_message buttons {"OK"} default button "OK" giving up after "30"
end try
Three fingertips away. Very handy.

[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: jcjamesx on Dec 14, '04 04:49:48PM

I was wondering if anybody could help me make one modification to this script.

I need for it to run on my webserver so that while I am back at home for Christmas, if the IP address changes, I will find out about it and can make the changes I need.

So basically, I need this exact script execpt I would like it to send the email by default, or maybe after it times out at the very begining, if it would just send the email after timing out.

Any help would be great as I am not very good with Applescript.
Thanks!



[ Reply to This | # ]
An AppleScript to obtain the WAN IP address
Authored by: omnimac on Dec 25, '04 07:45:49PM

Hello, I just deleted a few lines. I'm sure there is a better way but this works for me. Also, I set the IP address to be the subject of the email as well as the text in the body.
Cheers! - JP

set response to do shell script "/usr/bin/curl http://checkip.dyndns.org/"
set ip_address to extract_ip(response)
set mailto_addr to "YourEmailAddress"
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 | # ]
An AppleScript to obtain the WAN IP address
Authored by: dogboy on Apr 20, '05 10:27:54AM

I notice you've included your own email address in there so all our info gets sent to you. Naughty.



[ Reply to This | # ]