Advertisement
applehelpwriter

get computer's local and external IP addresses

Jul 30th, 2013
5,223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. script by Phil Stokes
  3. www.applehelpwriter.com
  4. 2013
  5.  
  6.  
  7. This script will deteremine the IP Addresses of your router, your node on the internal network (aka 'Internal IP') and your public internet IP address (aka 'External IP') and allow you to copy them for use in Terminal or other applications
  8.  
  9. 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). Alternatively,  just keep hitting the 'Try Again' button when the app runs as necessary.
  10.  
  11. Enjoy!
  12.  
  13. Phil  
  14. *)
  15.  
  16.  
  17.  
  18. property theNext : ""
  19. property theNetwork : ""
  20. property theRouter : ""
  21. property theLocalNode : ""
  22.  
  23.  
  24. on getIP()
  25.     try
  26.         set myTemp to do shell script "mktemp -t txt"
  27.         do shell script "curl -s http://checkip.dyndns.org &> " & myTemp & " &2> /dev/null"
  28.        
  29.         # CHANGE THE DELAY HERE…     
  30.         delay 3
  31.         set extIP to do shell script "sed 's/[a-zA-Z/<> :]//g' " & myTemp
  32.        
  33.         if extIP = "" then
  34.             set my theNetwork to "No connection"
  35.         else if extIP contains "=" then
  36.             set theNetwork to "Can't get IP"
  37.         else
  38.             set theNetwork to extIP
  39.         end if
  40.     on error
  41.         set theNetwork to "No connection"
  42.     end try
  43. end getIP
  44.  
  45. on getRouter()
  46.     try
  47.         set oldDelims to AppleScript's text item delimiters
  48.         set AppleScript's text item delimiters to "gateway:"
  49.         set theGateway to do shell script "route get default | grep gateway"
  50.         set AppleScript's text item delimiters to oldDelims
  51.         set theRouter to the last word of theGateway
  52.     on error
  53.         set my theRouter to "No connection"
  54.     end try
  55. end getRouter
  56.  
  57. on getLocalNode()
  58.     try
  59.         set theIP to (do shell script "ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2")
  60.         set theLocalNode to the last word of theIP
  61.     on error
  62.         set theLocalNode to "Can't get Local IP"
  63.     end try
  64. end getLocalNode
  65.  
  66. end
  67.  
  68. on getCopyItem()
  69.     try
  70.         set theList to {"Router", "Local IP", "External IP"}
  71.         choose from list theList with prompt "Choose an item to copy:"
  72.         set myResult to (item 1 of the result)
  73.         if myResult = "Router" then
  74.             set the clipboard to theRouter
  75.         else if myResult = "Local IP" then
  76.             set the clipboard to theLocalNode
  77.         else
  78.             set the clipboard to theNetwork
  79.         end if
  80.        
  81.     end try
  82.    
  83. end getCopyItem
  84.  
  85. on userInfo()
  86.     display dialog "Router: " & theRouter & return & return & "Local IP: " & theLocalNode & return & return & "External IP: " & theNetwork buttons {"OK", "Copy", "Try Again"} cancel button {"OK"} with title "Your IP addresses" default button "Try Again"
  87.     set theNext to the button returned of the result
  88.     return result
  89. end userInfo
  90.  
  91. repeat
  92.     try
  93.         getRouter()
  94.         getIP()
  95.         getLocalNode()
  96.         userInfo()
  97.         if result = "Try Again" then
  98.             getIP()
  99.         else if theNext = "Copy" then
  100.            
  101.             getCopyItem()
  102.             exit repeat
  103.            
  104.         end if
  105.        
  106.     on error
  107.         exit repeat
  108.     end try
  109. end repeat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement