NO

Author Topic: Retrieving your WAN IP address  (Read 4375 times)

Offline Vortex

  • Member
  • *
  • Posts: 867
    • http://www.vortex.masmcode.com
Retrieving your WAN IP address
« on: February 06, 2022, 06:50:34 PM »
Command line tool to get the WAN IP address :

Code: [Select]
; Example of retrieving the WAN IP adress from an external source :
;
; GetWanIP.exe http://whatismyip.akamai.com
;              https://ipecho.net/plain
;
; If no command line option is specified then the application
; defaults to the address http://icanhazip.com

include GetWanIP.inc

.data

url db 'http://icanhazip.com',0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi ebx

LOCAL buffer[512]:BYTE
LOCAL buffer2[68]:BYTE
LOCAL App[128]:BYTE
LOCAL hInt:DWORD
LOCAL hUrl:DWORD
LOCAL BuffSize:DWORD

    lea     esi,buffer
    invoke  ParseCmdLine,esi
    cmp     eax,2
    jz      @f

    mov     DWORD PTR [esi+4],OFFSET url   
@@:   
    invoke  GetModuleFileName,0,ADDR App,128

    xor     ebx,ebx
    invoke  InternetOpen,ADDR App,\
            INTERNET_OPEN_TYPE_PRECONFIG,\
            ebx,ebx,ebx
           
    test    eax,eax
    jnz     @f
    ret
@@:
    mov     hInt,eax
    invoke  InternetOpenUrl,eax,\
            DWORD PTR [esi+4],ebx,ebx,ebx,ebx

    test    eax,eax
    jz      _exit

    mov     hUrl,eax
    lea     esi,buffer2
@@:
    invoke  InternetReadFile,hUrl,esi,64,\
            ADDR BuffSize
    push    eax
    invoke  InternetCloseHandle,hUrl
    pop     eax
    test    eax,eax
    jz      _exit

    mov     edx,BuffSize
    mov     ecx,32
    mov     BYTE PTR [esi+edx],ch
    dec     esi
@@:
    inc     esi
    movzx   eax,BYTE PTR [esi]
    test    eax,eax
    jz      @f
    cmp     BYTE PTR [esi],cl
    jnb     @b
    mov     BYTE PTR [esi],cl
    jmp     @b
@@:   
    invoke  StdOut,ADDR buffer2

_exit:

    invoke  InternetCloseHandle,hInt
    ret

main ENDP

END start
Code it... That's all...

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Retrieving your WAN IP address
« Reply #1 on: February 19, 2022, 12:09:00 PM »
Thanks Vortex,

This is useful, even if just for the URL. 

I always wanted to find out my TRUE IP without needing to use another web site though.  Just seems ridiculous I can't get my true IP from my own computer or modem through software without asking a web site to send it to me. 

Maybe some sort of ping trace then at least it is just a ping.

Anyone has solved this question without hitting an external web site, and without a DNS service?

John Z

Offline Vortex

  • Member
  • *
  • Posts: 867
    • http://www.vortex.masmcode.com
Re: Retrieving your WAN IP address
« Reply #2 on: February 19, 2022, 09:09:56 PM »
Hi John Z,

Thanks. I am afraid there is no easy way to get the WAN IP without referring to external sources. The only other method is to access the web interface of your router to check the WAN IP.
Code it... That's all...

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Retrieving your WAN IP address
« Reply #3 on: February 20, 2022, 12:28:28 PM »
Yup, so I appreciate your code contribution even more!

Thanks

John Z

Offline Vortex

  • Member
  • *
  • Posts: 867
    • http://www.vortex.masmcode.com
Re: Retrieving your WAN IP address
« Reply #4 on: July 17, 2022, 12:04:47 PM »
Hi John,

There is an easier method to get the WAN IP address, nslookup does the job :

Code: [Select]
nslookup myip.opendns.com. resolver1.opendns.com
Code it... That's all...

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Retrieving your WAN IP address
« Reply #5 on: July 17, 2022, 01:03:06 PM »
Hi Vortex,

Wow worked great !
Can even pipe it to a file.

Thanks so much,

John Z