Applescript to report IP address

I’m leaving home for college in a couple of days, and I’m setting up my dad’s computer so I can perform various maintenance and troubleshooting via Apple Remote Desktop. Since his connection gives him a dynamic IP address, I’d have to manually enter it each time, and he wants some degree of control over the process, so I’m making a little applescript that, when he runs it, e-mails me his IP address. Problem is, I have no idea how to do this. I’ve used applescript before and I know the syntax decently, but I have a very poor “vocabulary” and will need some help, so any would be appreciated. Thanks!

There are various methods you can use (found-able here using the Search function, btw). Eg:

set IPAddressInfo to do shell script "curl http://checkip.dyndns.org/ | grep 'Current IP Address'"

About emailing this text, it depends on your email client. I’m sure you can find lots of examples here (Search) for Mail, Entourage, Outlook, Eudora, etc. And you have also XMail.osax if you prefer a faceless solution…

If you want just the IP address, you could use something like this:

do shell script "curl http://checkip.dyndns.org/ | grep 'Current IP Address' | cut -d : -f 2 | cut -d \\< -f 1"
set IPAddress to (characters 2 through -1 of result) as text

Thanks so much for the prompt replies! The e-mail client I’ll be using is Eudora.

If the target computer has Apple’s Mail program on it, this will work:

set the_script to "curl -s http://checkip.dyndns.org/ | sed 's/.*body>Cur/Cur/' | sed 's/<.*$//'"
set my_IP to do shell script the_script
--display dialog my_IP
set the_message to "Remote Computer's  " & my_IP

set the_sub to "Current IP Address"
set me_ to "yourname@yourip.com"

tell application "Mail"
	set k to make new outgoing message with properties {subject:the_sub, content:the_message}
	tell k to make new to recipient at end of to recipients with properties {address:me_}
	send k
	quit
end tell

Model: G5 Dual 2.7 GHz
AppleScript: 2.0 (v43.1)
Browser: Safari 312
Operating System: Mac OS X (10.4)

Actually, I figured it out myself. :stuck_out_tongue: Thanks anyway!

These methods seem woefully slow, resource intensive and potentially unreliable (in fact it didn’t work for me at all–after about 2-3 minutes I received an error stating “no response from server” or some such thing). The following line of code should execute far more quickly (I get a response back in about a tenth of a second):

set IPAddressInfo to do shell script "ifconfig en0 | sed -n 's/..*inet \\(..*\\) net..*/\\1/p'"

(I found it odd that I had to escape the backslash characters, but then I’m fairly inexperienced with Applescripting)

Still, the code you guys gave was on the right track and gave me a place to start :slight_smile:

BTW, if he’s on dialup, you’ll need to modify en0 to point to the modem interface.

The djames42 method presumes that the machine is directly connected to the network (WAN). The earlier methods will give you your WAN IP Address if you have a router of any sort upstream of your machine.

I personally use this script:

set theHTML to do shell script "curl checkip.dyndns.org"
set myIP to word 21 of theHTML
-- add your preferred notification here.

Easiest would be:

set my_externalIP to do shell script "curl http://whatismyip.org"

anaxamander, that’s a good solution. :cool:

Ah, that’s a very good point, I hadn’t thought about that. I just wasn’t able to reach the first site mentioned from behind our rediculously strict corporate firewall, and so the method I mentioned worked perfectly for my needs. A typical home broadband setup would almost certainly have an internal NAT’d address, and so the snipped I posted wouldn’t do him any good.

Hey all,

I looked into this thread and none of the script suggested here is still valid…
Did something change in Applescript commands or the result obtained from the webpage (checkip.dyndns.org) is now different ?

I am looking for a simple script to get my external IP address.
Thanks !!!

Very simple

set my_externalIP to do shell script "curl ipinfo.io/ip"

There are many other ways, please read Command for determining my public IP

Excellent !
Thanks !!!
L.