Retreiving the Internet IP?

Just starting out? Need help? Post your questions and find answers here.
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Retreiving the Internet IP?

Post 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
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Retreiving the Internet IP?

Post 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))
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Retreiving the Internet IP?

Post by Nituvious »

Darn, I guess there are not native functions in PB for this then?
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Retreiving the Internet IP?

Post by ts-soft »

There can't a native function for this!
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Retreiving the Internet IP?

Post 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?
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Retreiving the Internet IP?

Post 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
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: Retreiving the Internet IP?

Post 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$)
klaver
Enthusiast
Enthusiast
Posts: 147
Joined: Wed Jun 28, 2006 6:55 pm
Location: Schröttersburg

Re: Retreiving the Internet IP?

Post by klaver »

Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Retreiving the Internet IP?

Post 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
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: Retreiving the Internet IP?

Post 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
PB 5.10 Windows 7 x64 SP1
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Retreiving the Internet IP?

Post 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.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Re: Retreiving the Internet IP?

Post 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"
Post Reply