Or you could do what i've been using, for public IPv4 or IPv6 address.
The PureBasic versions:
Code: Select all
Procedure.s GetPubIPv4Address()
; https://www.ipify.org/
InitNetwork()
*Buffer = ReceiveHTTPMemory("https://api.ipify.org?format=json")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
ip4addrs$ = GetJSONString(GetJSONMember(JSONValue(0), "ip"))
EndIf
ProcedureReturn Trim(ip4addrs$)
EndProcedure
Procedure.s GetPubIPv6Address()
; http://ip6.anysrc.net/json
InitNetwork()
*Buffer = ReceiveHTTPMemory("http://ip6.anysrc.net/json")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
ip6addrs$ = GetJSONString(GetJSONMember(JSONValue(0), "clientip"))
EndIf
ProcedureReturn Trim(ip6addrs$)
EndProcedure
Debug GetPubIPv4Address()
Debug GetPubIPv6Address()
The code above is from the
http://www.ipify.org site and written by the owner of that site (I think, he has contributors also, so one of them may have written it - scroll down a little on his page and he has implementations for different coding platforms), credit to him. I used the same code for the IPv6 side also and just changed the site address. You can just about plug in any site that uses json.
If using Windows 10 and want to use powershell in Windows 10, here are some basic draft powershell versions that work for use in PureBasic:
Code: Select all
Procedure.b GetTestIfSiteIsUp(thesitetotest$) ; if site is down (or does not exist) PowerShell returns "Access down..."
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " wget " + thesitetotest$ + " | % {$_.StatusCode}","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If KeepOutCmd$ = "200" ; 200 = site OK
siteupordown.b = #True
Else
siteupordown.b = #False
EndIf
ProcedureReturn siteupordown
EndProcedure
Procedure.b GetValidateIPAddress(ipadrtoval$)
; will validate IPv4 and IPv6
ipadr$ = Chr(34) + Chr(39) + ipadrtoval$ + Chr(39) + Chr(34)
OutCmd$ = ""
KeepOutCmdA$ = ""
CmdPromptRun = RunProgram("powershell.exe", " [System.Net.IPAddress]::TryParse( " + ipadr$ + ", [ref] $NULL)","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmdA$ = KeepOutCmdA$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If Trim(KeepOutCmdA$) = "True"
ProcedureReturn #True ; is a valid IPv4 or IPv6 address
Else
ProcedureReturn #False ; is not a valid IPv4 or IPv6 address
EndIf
EndProcedure
Procedure.s GetIPv4ipify()
; https://www.ipify.org/
; according to web site It "works flawlessly with both IPv4 and IPv6 addresses,
; so no matter what sort of technology you're using, there won't be issues."
; but... the Ipv6 side is not working right now due to, from what I understand is, an IPv6 issue with Amazons domain (in which the site is hosted)
; IPv6 supposedly due to be fixed by moving to another domain in about a month
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Invoke-RestMethod https://api.ipify.org?format=json | Select ip","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipinfoio$ = Trim(KeepOutCmd$)
ipinfoio$ = RemoveString(ipinfoio$, "ip", #PB_String_NoCase, 1, 1)
ipinfoio$ = Trim(RemoveString(ipinfoio$, "-", #PB_String_NoCase, 1))
If CountString(ipinfoio$, ".") = 3 And Len(ipinfoio$) <= 15
ProcedureReturn ipinfoio$
Else
ProcedureReturn "(No IPv4 Information resource 2)"
EndIf
EndProcedure
Procedure.s Getipv4whatismyip()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://ipv4.whatismyip.akamai.com/).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv4whatismyipakamai$ = Trim(KeepOutCmd$)
If CountString(ipv4whatismyipakamai$, ".") = 3 And Len(ipv4whatismyipakamai$) <= 15
ProcedureReturn ipv4whatismyipakamai$
Else
ProcedureReturn "(No IPv4 Information resource 1)"
EndIf
EndProcedure
Procedure.s GetIPv4ipinfo()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Invoke-RestMethod http://ipinfo.io/json | Select -exp ip","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipinfoio$ = Trim(KeepOutCmd$)
If CountString(ipinfoio$, ".") = 3 And Len(ipinfoio$) <= 15
ProcedureReturn ipinfoio$
Else
ProcedureReturn "(No IPv4 Information resource 2)"
EndIf
EndProcedure
Procedure.s GetIPv4dnsomatic()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://myip.dnsomatic.com/).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
myipdnsomatic$ = Trim(KeepOutCmd$)
If CountString(myipdnsomatic$, ".") = 3 And Len(myipdnsomatic$) <= 15
ProcedureReturn myipdnsomatic$
Else
ProcedureReturn "(No IPv4 Information resource 3)"
EndIf
EndProcedure
Procedure.s GetIPv4icanhazip()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest IPv4.Icanhazip.com).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv4icanhazip$ = Trim(KeepOutCmd$)
If CountString(ipv4icanhazip$, ".") = 3 And Len(ipv4icanhazip$) <= 15
ProcedureReturn ipv4icanhazip$
Else
ProcedureReturn "(No IPv4 Information resource 4)"
EndIf
EndProcedure
Procedure.s GetIPv4myipdk()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://ipv4.myip.dk/api/info/IPv4Address).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv4myipdk$ = Trim(RemoveString(KeepOutCmd$, Chr(34), #PB_String_NoCase, 1))
If CountString(ipv4myipdk$, ".") = 3 And Len(ipv4myipdk$) <= 15
ProcedureReturn ipv4myipdk$
Else
ProcedureReturn "(No IPv4 Information resource 5)"
EndIf
EndProcedure
Procedure.s GetIPv4AddressPublic()
siteup.b = #True
gotip4adr.b = #True
; https://www.ipify.org/ in case we need it and has json - both IPv4 and IPv6 addresses
; http://whatismyip.akamai.com/advanced
; http://ipecho.net/plain
; http://v4.ident.me/
; http://ident.me/
; http://ipv4.icanhazip.com/
; http://ipv6.icanhazip.com/
; http://api.ident.me/
; ifcfg.me/all all information
; ifcfg.me/json all information As json
; ifcfg.me/rss ip, host And isp As rss feed
; ifcfg.me/? this help
; http://checkip.amazonaws.com/
; http://ipof.in/json - has a blacklist check
; http://bot.whatismyipaddress.com/
; https://www.ipify.org/
; https://api.ipify.org/?format=json
; http://www.whatsmyip.website/api/json
; bot.whatismyipaddress.com - Responds To IPv4 & IPv6 queries.
; ipv4bot.whatismyipaddress.com - Responds To IPv4 queries only.
; ipv6bot.whatismyipaddress.com - Responds To IPv6 queries only.
; http://ip.anysrc.net/json
; http://ip.anysrc.net/
; IPv4 - http://ip4.anysrc.net
; IPv6 - http://ip6.anysrc.net
; Auto detection - http://ip.anysrc.net
If GetTestIfSiteIsUp("https://api.ipify.org?format=json") = #True
pubipv4addr$ = GetIPv4ipify()
If GetValidateIPAddress(pubipv4addr$) = #True
ProcedureReturn pubipv4addr$
Else
gotip4adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipv4.whatismyip.akamai.com/") = #True And (gotip4adr = #False Or siteup = #False)) = #True
pubipv4addr$ = Getipv4whatismyip()
If GetValidateIPAddress(pubipv4addr$) = #True
ProcedureReturn pubipv4addr$
Else
gotip4adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipinfo.io/json") = #True And (gotip4adr = #False Or siteup = #False)) = #True
pubipv4addr$ = GetIPv4ipinfo()
If GetValidateIPAddress(pubipv4addr$) = #True
ProcedureReturn pubipv4addr$
Else
gotip4adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://myip.dnsomatic.com/") = #True And (gotip4adr = #False Or siteup = #False)) = #True
pubipv4addr$ = GetIPv4dnsomatic()
If GetValidateIPAddress(pubipv4addr$) = #True
ProcedureReturn pubipv4addr$
Else
gotip4adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipv4.icanhazip.com/") = #True And (gotip4adr = #False Or siteup = #False)) = #True
pubipv4addr$ = GetIPv4icanhazip()
If GetValidateIPAddress(pubipv4addr$) = #True
ProcedureReturn pubipv4addr$
Else
gotip4adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipv4.myip.dk/api/info/IPv4Address") = #True And (gotip4adr = #False Or siteup = #False)) = #True
pubipv4addr$ = GetIPv4myipdk()
If GetValidateIPAddress(pubipv4addr$) = #True
ProcedureReturn pubipv4addr$
Else
gotip4adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(gotip4adr = #False Or siteup = #False) = #True
ProcedureReturn "IPv4 Address resource not available."
EndIf
EndProcedure
Procedure.s Getipv6whatismyip()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://ipv6.whatismyip.akamai.com/).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv6whatismyipakamai$ = Trim(KeepOutCmd$)
If CountString(ipv6whatismyipakamai$, ":") > 0
ProcedureReturn ipv6whatismyipakamai$
Else
ProcedureReturn "(No IPv6 Information resource 1)"
EndIf
EndProcedure
Procedure.s GetIPv6icanhazip()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://ipv6.icanhazip.com/).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv6icanhazip$ = Trim(KeepOutCmd$)
If CountString(ipv6icanhazip$, ":") > 0
ProcedureReturn ipv6icanhazip$
Else
ProcedureReturn "(No IPv6 Information resource 2)"
EndIf
EndProcedure
Procedure.s GetIPv6ipv6test()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://v6.ipv6-test.com/api/myip.php).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv6test$ = Trim(KeepOutCmd$)
If CountString(ipv6test$, ":") > 0
ProcedureReturn ipv6test$
Else
ProcedureReturn "(No IPv6 Information resource 3)"
EndIf
EndProcedure
Procedure.s GetIPv6ipv6botwhatismyipaddress()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest ipv6bot.whatismyipaddress.com).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv6bot$ = Trim(KeepOutCmd$)
If CountString(ipv6bot$, ":") > 0
ProcedureReturn ipv6bot$
Else
ProcedureReturn "(No IPv6 Information resource 4)"
EndIf
EndProcedure
Procedure.s GetIPv6anysrc()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Invoke-RestMethod http://ip6.anysrc.net/json | Select -exp clientip","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv6anysrc$ = Trim(KeepOutCmd$)
If CountString(ipv6anysrc$, ":") > 0
ProcedureReturn ipv6anysrc$
Else
ProcedureReturn "(No IPv6 Information resource 5)"
EndIf
EndProcedure
Procedure.s GetIPv6myipdk()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " (Invoke-WebRequest http://ipv6.myip.dk/api/info/IPv6Address).Content","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ipv6myipdk$ = Trim(RemoveString(KeepOutCmd$, Chr(34), #PB_String_NoCase, 1))
If CountString(ipv6myipdk$, ":") > 0
ProcedureReturn ipv6myipdk$
Else
ProcedureReturn "(No IPv6 Information resource 6)"
EndIf
EndProcedure
Procedure.s GetIPv6AddressPublic()
siteup.b = #True
gotip4adr.b = #True
; https://www.ipify.org/ in case we need it and has json - both IPv4 and IPv6 addresses
; http://ipv6.icanhazip.com/
; http://api.ident.me/
; IPv6 - http://ip6.anysrc.net
; http://ip6.anysrc.net/json
; http://ip6.anysrc.net/plain/clientip
; Auto detection - http://ip.anysrc.net
If GetTestIfSiteIsUp("http://ipv6.whatismyip.akamai.com") = #True
pubipv6addr$ = Getipv6whatismyip()
If GetValidateIPAddress(pubipv6addr$) = #True
ProcedureReturn pubipv6addr$
Else
gotip6adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipv6.icanhazip.com") = #True And (gotip6adr = #False Or siteup = #False)) = #True
pubipv6addr$ = GetIPv6icanhazip()
If GetValidateIPAddress(pubipv6addr$) = #True
ProcedureReturn pubipv6addr$
Else
gotip6adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://v6.ipv6-test.com/api/myip.php") = #True And (gotip6adr = #False Or siteup = #False)) = #True
pubipv6addr$ = GetIPv6ipv6test()
If GetValidateIPAddress(pubipv6addr$) = #True
ProcedureReturn pubipv6addr$
Else
gotip6adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipv6bot.whatismyipaddress.com/") = #True And (gotip6adr = #False Or siteup = #False)) = #True
pubipv6addr$ = GetIPv6ipv6botwhatismyipaddress()
If GetValidateIPAddress(pubipv6addr$) = #True
ProcedureReturn pubipv6addr$
Else
gotip6adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ip6.anysrc.net/json") = #True And (gotip6adr = #False Or siteup = #False)) = #True
pubipv6addr$ = GetIPv6anysrc()
If GetValidateIPAddress(pubipv6addr$) = #True
ProcedureReturn pubipv6addr$
Else
gotip6adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(GetTestIfSiteIsUp("http://ipv6.myip.dk/api/info/IPv6Address") = #True And (gotip6adr = #False Or siteup = #False)) = #True
pubipv6addr$ = GetIPv6myipdk()
If GetValidateIPAddress(pubipv6addr$) = #True
ProcedureReturn pubipv6addr$
Else
gotip6adr = #False
EndIf
Else
siteup = #False
EndIf
If Bool(gotip6adr = #False Or siteup = #False) = #True
ProcedureReturn "IPv6 Address resource not available or busy."
EndIf
EndProcedure
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.