Page 1 of 1
Get IP from dynamic DSN
Posted: Sun Dec 04, 2022 12:39 pm
by jacdelad
Hello,
I hope I find the right word. Via dynamic DSN I can access my home network from outside without knowing the IP. Can I easily get the real IP from the address the dynamic DNS creates? Is this depending on the dynamic DNS provider or is there an universal method (if any)?
Re: Get IP from dynamic DSN
Posted: Sun Dec 04, 2022 3:53 pm
by Bitblazer
To get the public IP adress of a machine behind a router and firewall, save the resulting html page you get from a service like
www.whatsmyip.com.
I wrote a procedure years ago that parses the result and returns the numeric IP and some meta data. It's not difficult to do and my own procedure is embarrassing old, or i would paste it

Re: Get IP from dynamic DSN
Posted: Sun Dec 04, 2022 7:06 pm
by ChrisR
You can get your PublicIP v4 or v6 address with powershell by using Invoke-RestMethod cmdlet
Code: Select all
IPv4: Invoke-RestMethod api.ipify.org
IPv6: Invoke-RestMethod api64.ipify.org
Or with cmd line with:
Code: Select all
IPv4: For /f %A in ('powershell Invoke-RestMethod api.ipify.org') do echo %A
IPv6: For /f %A in ('powershell Invoke-RestMethod api64.ipify.org') do echo %A
Re: Get IP from dynamic DSN
Posted: Sun Dec 04, 2022 7:35 pm
by idle
Gethostbyname api will resolve the ip address the dynamic dns name.
Re: Get IP from dynamic DSN
Posted: Sun Dec 04, 2022 8:53 pm
by infratec
Can I easily get the real IP from the address the dynamic DNS creates?
I think he want to get the internal IP of the host with the DynDNS address.
For example:
A server behind a DSL-Router.
The DynDNS is the address of the router which forwards the access to the internal server.
And he want to know the internal address of the server.
But it is not 100% clear which address he want.
So a better description is needed.
Re: Get IP from dynamic DSN
Posted: Sun Dec 04, 2022 10:45 pm
by ChrisR
You are probably right about what jacdelad wants.
About
ipify, one of the largest and most popular IP address API services on the internet, that has existed for years.
I wrote them to point out that their PureBasic code sample is outdated, InitNetwork() is deprecated since PureBasic version 6.0.
Code: Select all
;InitNetwork() is no more needed to use network functions (deprecated) since PureBasic 6.0
*Buffer = ReceiveHTTPMemory("https://api.ipify.org?format=json")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
Debug "PublicIP v4 = " + GetJSONString(GetJSONMember(JSONValue(0), "ip"))
EndIf
A more complete code sample to get your public IPv4 or IPv6 in plain text or in JSON format, it's so simple, easy
Code: Select all
;InitNetwork() is no more needed to use network functions (deprecated) since PureBasic 6.0
; Get your public IP directly (in plain text)
*Buffer = ReceiveHTTPMemory("api.ipify.org")
If *Buffer
Debug "PublicIP v4 = " + PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8|#PB_ByteLength)
FreeMemory(*Buffer)
EndIf
*Buffer = ReceiveHTTPMemory("api64.ipify.org")
If *Buffer
Debug "PublicIP v6 = " + PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8|#PB_ByteLength)
FreeMemory(*Buffer)
EndIf
; Get your public IP in JSON format
*Buffer = ReceiveHTTPMemory("https://api.ipify.org?format=json")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
Debug "PublicIP v4 = " + GetJSONString(GetJSONMember(JSONValue(0), "ip"))
EndIf
*Buffer = ReceiveHTTPMemory("https://api64.ipify.org?format=json")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
Debug "PublicIP v6 = " + GetJSONString(GetJSONMember(JSONValue(0), "ip"))
EndIf
Re: Get IP from dynamic DSN
Posted: Mon Dec 05, 2022 9:08 am
by jacdelad
Thanks you all. I wanted to get my public address, the internal addresses are known. I guess at least one of the methods will work.
Re: Get IP from dynamic DSN
Posted: Mon Dec 05, 2022 5:05 pm
by tj1010
I don't have the code anymore, but on Windows machines I used INATExternalIPAddressCallback::NewExternalIPAddress() to get WAN IP without any service. It's part of the Firewall API. It uses UpNp IGD internally.
Just an FYI most web hosts allow you to use dynamic IP for A and even NS records. You can set short TTL to make sure it's always updated. You can even do round-robin and fast flux with entirely dynamic WAN IPs. I've setup flux and round robin on both GoDaddy and BlueHost before using only dynamic IP and polled A and NS updated with a endpoint script..
I haven't used a DDNS service since Dyn was the only provider; they actually got router integration before anyone else. Most Router manufacturers embed DDNS support in firmware. Examples:
http://iplookup.asus.com/nslookup.php tplinkdns.com (Linksys and Netgear use DynDNS, TZO, and NoIP)
Re: Get IP from dynamic DSN
Posted: Mon Dec 05, 2022 5:28 pm
by Marc56us
The question was: know your external IP (WAN) when you know your dynamic DNS name
Query DNS (uses
nslookup)
(can take some secs for some IP)
Code: Select all
; Get_WAN_IP
; Query DNS without external internet service
; Windows only (easy to adapt for other os)
; Marc56 - 2022/12/05 - v1.0 - PureBasic any version
; in reply to https://www.purebasic.fr/english/viewtopic.php?t=80228
EnableExplicit
Define Compiler = RunProgram(GetEnvironmentVariable("%windir%") +
"nslookup.exe",
"list " +
InputRequester("WAN IP", "Enter Site Name", "") ,
"",
#PB_Program_Open | #PB_Program_Read)
If Compiler
While ProgramRunning(Compiler)
If AvailableProgramOutput(Compiler)
Debug ReadProgramString(Compiler)
EndIf
Wend
CloseProgram(Compiler)
EndIf
End
PS: To install
nslookup for linux (Debian based)
On linux, prefer
dig (installed with nslookup)

Re: Get IP from dynamic DSN
Posted: Mon Dec 05, 2022 5:44 pm
by tj1010
On Linux you can just use tcpdump for just about everything including UpNp IGD which will get you WAN IP without a service if UpNp is enabled
Basically: No UpNp then you need a service to get NAT gateway WAN address. Windows you use tcpdump or Firewall API