Page 1 of 1

Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 5:07 pm
by Nituvious
How can we get the internet IP as it's seen at: http://www.myipaddress.com
I've tried using:

Code: Select all

     ExamineIPAddresses()
     ValidateIP$ = IPString(NextIPAddress())
     MessageRequester("",ValidateIP$)
However this does not work in returning the IP that is shown by http://myipaddress.com

Re: Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 5:55 pm
by ts-soft
Here a small snippet by HeXOr:

Code: Select all

InitNetwork()
Debug StringField(GetHTTPHeader("http://h3x0r.ath.cx/Sonstiges/ShowMyIp12.php"), 2, Chr(34))

Re: Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 6:04 pm
by Nituvious
Darn, I guess there are not native functions in PB for this then?

Re: Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 6:09 pm
by ts-soft
There can't a native function for this!

Re: Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 6:36 pm
by Nituvious
ts-soft wrote:There can't a native function for this!
Oh, so we'll have to always locate the internet IP from outside of PureBasic?

Re: Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 6:59 pm
by ts-soft
yes, you can read the ip from some routers, modems and so on but there is no way to get it on all pc's this way

Re: Retreiving the Internet IP?

Posted: Mon Nov 16, 2009 8:08 pm
by Seldon
I wrote this function some time ago... It uses a service at: http://www.indirizzo-ip.com .

Code: Select all

#PUBLIC_IP=1
#LOCALE_IP=2
#DATA_BUFFER_LENGHT=256

Declare.s GetIPAddress(IP_Type)

InitNetwork()

Procedure.s GetIPAddress(IP_Type)
  Protected i=0, l
  Protected *BUFFER_connection, *p
  Protected tmp$=""
  Select IP_Type
    Case #PUBLIC_IP
      *BUFFER_connection=AllocateMemory(#DATA_BUFFER_LENGHT)
      If *BUFFER_connection
        l=OpenNetworkConnection("www.indirizzo-ip.com",80)
        If l
          tmp$="GET /ip.php?.txt HTTP/1.1"+#CRLF$
          tmp$+"Host: www.indirizzo-ip.com"+#CRLF$
          tmp$+"Connection: Close"+#CRLF$+#LF$
          SendNetworkString(l,tmp$)
          Repeat
            i=NetworkClientEvent(l)
          Until i<>0
          If i=#PB_NetworkEvent_Data
            i=ReceiveNetworkData(l,*BUFFER_connection,#DATA_BUFFER_LENGHT)
          EndIf
          CloseNetworkConnection(l)
          tmp$=""
          If i<>-1
            If PeekS(*BUFFER_connection,15,#PB_Ascii)="HTTP/1.1 200 OK"
              *p=*BUFFER_connection+213
              Repeat
                *p+1
                i=PeekB(*p)
                If i<>10
                  tmp$+Chr(i)
                Else
                  Break
                EndIf
              ForEver
            EndIf
          EndIf
        EndIf
        FreeMemory(*BUFFER_connection)
      EndIf
    Case #LOCALE_IP
      If ExamineIPAddresses()
        Repeat
          l=NextIPAddress()
          If l
            If i>0
;           If tmp$
              tmp$+","
            EndIf          
            tmp$+IPString(l)
            i+1
          EndIf
        Until l=0
      EndIf
  EndSelect
  ProcedureReturn(tmp$)
EndProcedure

; Esempio
;IP$=GetIPAddress(#LOCALE_IP)
;Debug IP$
;Debug Len(IP$)

;IP$=GetIPAddress(#PUBLIC_IP)
;Debug IP$
;Debug Len(IP$)

Re: Retreiving the Internet IP?

Posted: Tue Nov 17, 2009 12:57 am
by klaver

Re: Retreiving the Internet IP?

Posted: Tue Nov 17, 2009 9:29 am
by SFSxOI
Something I saved from someone here in the forum - credit to the original poster. No external sites needed.

Code: Select all

If InitNetwork()

Hostname$ = Space(255)
gethostname_(Hostname$,255)
Debug "Hostname: "+Hostname$

pHostinfo = gethostbyname_(Hostname$)
If pHostinfo = 0 
Debug "Unable to resolve domain name."
Else
CopyMemory (pHostinfo, hostinfo.HOSTENT, SizeOf(HOSTENT))

If hostinfo\h_addrtype <> #AF_INET
Debug "A non-IP address was returned."
Else
While PeekL(hostinfo\h_addr_list+AdressNumber*4)
ipAddress = PeekL(hostinfo\h_addr_list+AdressNumber*4)
Debug StrU(PeekB(ipAddress),0)+"."+StrU(PeekB(ipAddress+1),0)+"."+StrU(PeekB(ipAddress+2),0)+"."+StrU(PeekB(ipAddress+3),0)
AdressNumber+1
Wend

EndIf
EndIf
Else
Debug "Network can't be initialized"
EndIf

Re: Retreiving the Internet IP?

Posted: Tue Nov 17, 2009 10:58 am
by jpd
SFSxOI wrote:Something I saved from someone here in the forum - credit to the original poster. No external sites needed.
Hi SFSxOI,

with this code you can read all local IP adresses an not the Internet IP!.

Best
jpd

Re: Retreiving the Internet IP?

Posted: Tue Nov 17, 2009 12:56 pm
by SFSxOI
Ahhh...your after the external IP address. I see now, I was just thinking "IP address" and did not stop to consider external vs internal. Sorry 'bout that.

Re: Retreiving the Internet IP?

Posted: Tue Nov 17, 2009 1:48 pm
by John Puccio
Not sure what you're trying to do, but if you are running a web server then.

Code: Select all

OpenConsole()
  PrintN(GetEnvironmentVariable("REMOTE_ADDRESS")) ; The IP address of the visitor
CloseConsole()
The exact name of the environment variable will depend on the server. Could also be "REMOTE_ADDR"