;; 
;;====================================================================== 
;; 
;;FUNCTION       GetMyPublicIP() 
;; 
;;ACTION         Identify the public IP address of an Internet connection 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.2  - 2019/01/25 
;; 
;;HISTORY        1.0  - 2015/08/12 - Identify the public IP Addresss 
;;               1.1  - 2016/03/22 - fixed bug in IP address validation logic 
;;               1.2  - 2019/01/25 - Used a private source for default IP identification 
;; 
;;SYNTAX         GetMyPublicIP([URL], [Phrase], [Offset], [LDelim], [RDelim]) 
;; 
;;PARAMETERS     URL    - OPTIONAL - The URL to query (Default: "www.ipchicken.com") 
;;               Phrase - OPTIONAL - The HTML phrase to search for to identify the 
;;                                   line containing the IP Address string 
;;               Offset - OPTIONAL - The line offset to extract the IP String from. 
;;                                   In some cases, the identifier phrase comes 1-2 lines 
;;                                   before the line containing the IP address. 
;;                                  (Default: "images/1main_10.gif") 
;;               Delim  - OPTIONAL - A delimiter string that immediately preceeds/follows 
;;                                   the IP address text. (Default: "" / "<br>") 
;; 
;;REMARKS        Queries a web page that identifies the user's IP Address. Often used to 
;;               determine if the Internet is on a primary or secondary connetion. The delim 
;;               strings (LeftDelim, RightDelim) can be approximations if the text closesed 
;;               to the IP address text is variable. Once the leading/trailing text is  
;;               parsed and removed, any non-digit data is also removed. 
;;                
;;               Note that as this parses the result of an HTML query, accuracy may not be 100%. 
;; 
;;RETURNS        IP Address string if found, empty string and error otherwise. 
;; 
;;DEPENDENCIES   GetPage UDF 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12, Windows 7, 8, 10 
;; 
;;EXAMPLES       ; get public address using default site 
;;               $PubIP = GetMyPublicIP() 
;;               ; get public address using an alternate site: whatismyipaddress.com 
;;               $PubIP = GetMyPublicIP('http://whatismyipaddress.com', 'href="//whatismyipaddress.com/ip/', 0, ';">', '</a>') 
;;               ; get public address using an alternate site: ipinfo.info 
;;               $PubIP = GetMyPublicIP('http://ipinfo.info/html/my_ip_address.php', '<td width=255 class="TextObjectNOFnomargins"', 1, '<B>&nbsp;', '</B>') 
; 
Function GetMyPublicIP(OPTIONAL $_Target, $_Phrase, $_Offset, $_LDelim, $_RDelim)
 
  Dim $_aPageData				; HTML data returned from page 
  Dim $_I, $_J, $_P				; index and return pointer 
  Dim $_C					; Character 
  Dim $_IpData					; the IP data extracted 
  Dim $_aIP					; array of IP data for filtering 
 
  $_Target = IIf($_Target = '', 'https://dist.mspbuilder.com', $_Target)
  $_Phrase = IIf($_Phrase = '', '<body>', $_Phrase)
  $_Offset = IIf('' + $_Offset = '', 1, $_Offset)
  $_LDelim = IIf('' + $_LDelim = '', '{', $_LDelim)
  $_RDelim = IIf('' + $_RDelim = '', '}', $_RDelim)
 
 
  $_aPageData = Split(Url($_Target)[0], Chr(10))
  For $_P = 0 to UBound($_aPageData)
    If InStr($_aPageData[$_P], $_Phrase)
      $_I = 0 + $_Offset + $_P
      $_IpData = LTrim($_aPageData[$_I])
      If InStr($_IpData, $_LDelim)
        $_IpData = Split($_IpData, $_LDelim)[1]
      EndIf
      If InStr($_IpData, $_RDelim)
        $_IpData = Split($_IpData, $_RDelim)[0]
      EndIf
 
      $_aIP = Split($_IpData, '.')		; convert IP string to array 
      $_IpData = ''				; clear IP string 
      If UBound($_aIP) = 3			; Does it have 4 elements? 
        For $_I = 0 to 3
          For $_J = 1 to Len($_aIP[$_I])	; Filter the chars - only digits allowed 
            $_C = SubStr($_aIP[$_I], $_J, 1)
            If InStr('0123456789', $_C)
              $_IpData = $_IpData + $_C		; numeric digit - add to IP string 
            EndIf
          Next
          If $_I < 3
            $_IpData = $_IpData + '.'		; add the dot to the IP string 
          EndIf
        Next
        $GetMyPublicIP = $_IpData		; return the IP string 
        $_P = 9998
      Else
        $GetMyPublicIP = ''
        $_P = 9999
      EndIf
    EndIf
  Next
 
  Exit ($_P - 9999)
 
EndFunction