Using AppleScript to capture external IP.

Posted on
Sat Sep 12, 2015 9:13 pm
Ankleshanker offline
Posts: 9
Joined: Jul 12, 2013

Using AppleScript to capture external IP.

I've been using the following script to check my external IP every four hours. If it detects a change, I get an email with the new IP. It usually works...but not always.

Code: Select all
(*
Based on:
http://www.perceptiveautomation.com/wiki/doku.php?id=applescript_snippit
http://www.perceptiveautomation.com/userforum/viewtopic.php?f=11&t=6237&p=37246&hilit=import+variable+from+applescript#p37246
http://applehelpwriter.com/2013/07/30/applescript-get-your-external-ip-address/
This script will deteremine your public internet IP address (aka 'External IP') and import it in to IndigoServer as a variable.
The script is set to display 'No connection' after a pretty short 3-second timeout. Feel free to increase that at the line in the script after the comment "# CHANGE THE DELAY HERE…". Just change the number '3' to something longer (the number = seconds).
*)
property theNetwork : ""
property extIP : ""
set myip to "myIPaddress_Current"
on setVariable(theVariable, theValue)
   tell application "IndigoServer"
      if variable theVariable exists then
         set value of variable theVariable to theValue
      else
         make new variable with properties {name:theVariable, value:theValue}
      end if
   end tell
end setVariable
on getIP()
   try
      set myTemp to do shell script "mktemp -t txt"
      do shell script "curl -s http://checkip.dyndns.org &> " & myTemp & " &2> /dev/null"
      
      # CHANGE THE DELAY HERE…      
      delay 10
      set extIP to do shell script "sed 's/[a-zA-Z/<> :]//g' " & myTemp
      if extIP = "" then
         set my theNetwork to "No connection"
      else if extIP contains "=" then
         set theNetwork to "Can't get IP"
      else
         set theNetwork to extIP
      end if
   on error
      set theNetwork to "No connection"
   end try
end getIP
repeat
   try
      getIP()
      if result = "Try Again" then
         getIP()
      else if theNext = "Copy" then
         getCopyItem()
         exit repeat
      end if
   on error
      exit repeat
   end try
end repeat
setVariable(myip, theNetwork)


I found a python module that I think will be more reliable, if I can get it to run.
https://pypi.python.org/pypi/ipgetter

When I run "python -m ipgetter" from my terminal it almost instantly returns my IP. How can I capture this output in a variable as I've been doing?

Thanks,

- Ben

Posted on
Sat Sep 12, 2015 9:25 pm
Ankleshanker offline
Posts: 9
Joined: Jul 12, 2013

Re: Using AppleScript to capture external IP.

I think I figured it out. Was it as simple as this, or am I missing something? I'm just posting the section I edited:

Code: Select all
on getIP()
   try
      set myip to do shell script "python -m ipgetter"
      set extIP to myip
      if extIP = "" then
         set my theNetwork to "No connection"
      else if extIP contains "=" then
         set theNetwork to "Can't get IP"
      else
         set theNetwork to extIP
      end if
   on error
      set theNetwork to "No connection"
   end try
end getIP

Posted on
Sun Sep 13, 2015 5:24 am
DaveL17 offline
User avatar
Posts: 6743
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Using AppleScript to capture external IP.

It can be super simple, but it depends on how you have your stuff installed. I liked your idea of having a variable that shows the externally-facing IP address. So here's what I did.

I installed ipgetter by running Terminal on my server machine with the following command:
Code: Select all
sudo pip2.6 install ipgetter

After entering my password, the ipgetter package was installed. (Note that this is installing the ipgetter package to a Python version that is different from the default Python version for my machine--which is Python 2.7.)

Provided that Indigo can see the folder where pip has installed your package (it should be able to), you can run the entire operation from an embedded Python script within Indigo:
Code: Select all
import ipgetter

myip = ipgetter.myip()

indigo.variable.updateValue(1186495075, myip)

Just replace the number '1186495075' with the ID of your variable.
Dave

EDIT: this is of course predicated on the fact that you have pip installed on your server machine. If not, the instructions are a little more involved. See this thread: http://forums.indigodomo.com/viewtopic.php?f=181&t=14572&p=100734&hilit=easy_install#p100734

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Sep 14, 2015 11:07 am
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Using AppleScript to capture external IP.

i have this set up never let me down.

Code: Select all
property currentIP : missing value -- keep track of current IP address
property mailtoAddress : "PUT YOUR EMAIL ADDRESS HERE"

# check if network is up

set newIP to word 25 of (do shell script "/usr/bin/curl checkip.dyndns.org")
if newIP is not equal to currentIP then
   if currentIP is not missing value then -- previous address set
      mailTo_message_(mailtoAddress, newIP)
   end if
   set currentIP to newIP -- update
end if

on mailTo:emailAddress message:theMessage
   tell application "Mail"
      -- activate
      set composedMessage to (make new outgoing message at beginning of outgoing messages)
      tell composedMessage
         -- set visible to true
         make new to recipient at beginning of to recipients ¬
            with properties {address:emailAddress}
         
         set the subject to theMessage
         set the content to theMessage
      end tell
      send composedMessage
   end tell
end mailTo:message:
tell application "IndigoServer"
   set value of variable "external_ip" to currentIP as string
end tell



just set up a variable called currentIP and run it as an external applescript

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests