Get IP from dynamic DSN

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Get IP from dynamic DSN

Post 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)?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Bitblazer
Enthusiast
Enthusiast
Posts: 733
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Get IP from dynamic DSN

Post 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 ;)
webpage - discord chat links -> purebasic GPT4All
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Get IP from dynamic DSN

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Get IP from dynamic DSN

Post by idle »

Gethostbyname api will resolve the ip address the dynamic dns name.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get IP from dynamic DSN

Post 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.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Get IP from dynamic DSN

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Get IP from dynamic DSN

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
tj1010
Enthusiast
Enthusiast
Posts: 621
Joined: Mon Feb 25, 2013 5:51 pm
Location: US or Estonia
Contact:

Re: Get IP from dynamic DSN

Post 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)
Last edited by tj1010 on Mon Dec 05, 2022 5:37 pm, edited 1 time in total.
The truth hurts.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Get IP from dynamic DSN

Post 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)

Code: Select all

$ sudo apt install dnsutils
On linux, prefer dig (installed with nslookup)
:wink:
User avatar
tj1010
Enthusiast
Enthusiast
Posts: 621
Joined: Mon Feb 25, 2013 5:51 pm
Location: US or Estonia
Contact:

Re: Get IP from dynamic DSN

Post 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
The truth hurts.
Post Reply