How can I obtain my internet address?

Just starting out? Need help? Post your questions and find answers here.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: How can I obtain my internet address?

Post by heartbone »

Vera wrote:
heartbone wrote:HeX0R, although yours is by far the easiest to implement, I consider it to be the least stable as far as longevity, which is why it's used third.
But I'm thinking that perhaps I have it backwards. :?:
I wouldn't consider 6 years availability to be unstable :mrgreen:
??? :?:
Vera, if you look at the original post from 4 years ago, that site (http://h3x0r.ath.cx/Sonstiges/ShowMyIp12.php) is no longer there.
Btw: thanks for your collection and inspiration how to get it.
Here's a further variation you might like to add:
And thanks for your contribution to the procedure. :)
I slightly modified your loop exit logic to remove the Break statement.

Code: Select all

Procedure.s GetExternalIPAddress()
; RETURNS A STRING CONTAINING THE CURRENT (IPv4} EXTERNAL IP ADDRESS.
; RETRIEVED FROM ANY ONE OF 9 WEBSITES 
; ALL 9 TESTED GOOD Monday, December 22, 2014. 6:57-6:59 PM CST
; A PREVIOUS CALL TO InitNetwork() IS REQUIRED. 
; WHICH=1 EXTRACT IP ADDRESS FROM http://ip.gwhois.org/
; WHICH=2 EXTRACT IP ADDRESS FROM http://checkip.dyndns.org
; WHICH=3 EXTRACT IP ADDRESS FROM http://ipecho.net/plain
; WHICH=4 EXTRACT IP ADDRESS FROM http://hex0rs.coderbu.de/Sonstiges/ShowMyIp12.php
; WHICH=5 EXTRACT IP ADDRESS FROM http://www.cmyip.com
; WHICH=6 EXTRACT IP ADDRESS FROM http://lloydsplace.com/whatsmyip.php
; WHICH=7 EXTRACT IP ADDRESS FROM http://www.realip.info/api/p/realip.php
; WHICH=8 EXTRACT IP ADDRESS FROM http://www.iplocation.net/
; WHICH=9 EXTRACT IP ADDRESS FROM http://www.whatsmyip.net/

   WHICH= Random(8)+ 1 : START= WHICH : EXTADDR$= ""
   Repeat
      Select WHICH
         Case 1
            If ReceiveHTTPFile("http://ip.gwhois.org/","IPADDRESS")
               ReadFile(1,"IPADDRESS") : EXTADDR$= ReadString(1) : CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 2
            HEADER$= GetHTTPHeader("http://checkip.dyndns.org")
            STIND= FindString(HEADER$, "Current IP Address:", 1)+ 20
            IND= FindString(HEADER$,"</body></html>",1)- STIND
            If IND > 0 : EXTADDR$ = Mid(HEADER$,STIND,IND) : EndIf
         Case 3
            If ReceiveHTTPFile("http://ipecho.net/plain","IPADDRESS")
               ReadFile(1,"IPADDRESS") : EXTADDR$= ReadString(1) : CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 4
            EXTADDR$= StringField(GetHTTPHeader("http://hex0rs.coderbu.de/Sonstiges/ShowMyIp12.php"),2,Chr(34))
         Case 5
            If ReceiveHTTPFile("http://www.cmyip.com","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1) 
                  If FindString(EXTADDR$,"My IP Address",1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
                  EndIf
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 6
            If ReceiveHTTPFile("http://lloydsplace.com/whatsmyip.php","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1) 
                  CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                  If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                  FreeRegularExpression(0)
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 7
            If ReceiveHTTPFile("http://www.realip.info/api/p/realip.php","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 8
            If ReceiveHTTPFile("http://www.iplocation.net/","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1)
                  If FindString(EXTADDR$,"Your IP Address is",1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
                  EndIf
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 9
            If ReceiveHTTPFile("http://www.whatsmyip.net/","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1)
                  If FindString(EXTADDR$," Address is ",1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
                  EndIf
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
      EndSelect
      If CountString(EXTADDR$,".")<>3 Or Len(EXTADDR$)<7 Or Len(EXTADDR$)>15 : EXTADDR$= "" : EndIf
      WHICH +1 : If WHICH=10 : WHICH= 1 : EndIf
   Until EXTADDR$<>"" Or WHICH=START
   ProcedureReturn EXTADDR$
EndProcedure
Marlin wrote:OK, now I actually got to provide some code about getting the external IP address from the router using upnp.
I even tested it, and it works fine here. ;)
{SNIP}
Thanks for the addition Marlin.
Although I'm still not convinced that code is not dependent on an external website,
I'd consider using your code as a separate fallback procedure to be called in the case the above GetExternalIPAddress() procedure returns a null string.
Last edited by heartbone on Tue Dec 23, 2014 2:07 am, edited 3 times in total.
Keep it BASIC.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: How can I obtain my internet address?

Post by Vera »

heartbone wrote:??? :?:
Vera, if you look at the original post from 4 years ago...
You might like to see an even more original post from 2008 ... and a hint suggesting, that if you change the address all will be well :-)
Somehow you too figured out that there was a change :wink:
I slightly modified your loop exit logic to remove the Break statement.
You're welcome to any customization.
I thought it might be good not to have the whole file read, after the search was successful. Some of the sides one could use are heavily overloaded (also within the html-code) ... but the one I shared is rather straight forward and slim.

I like your idea to pick a random address.


Ty dear Marlin
I could fax my biography to you :mrgreen: :lol:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8204
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How can I obtain my internet address?

Post by netmaestro »

I will never leave you or forsake you:

http://lloydsplace.com/whatsmyip.php
BERESHEIT
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: How can I obtain my internet address?

Post by Vera »

netmaestro wrote:I will never leave you or forsake you:

http://lloydsplace.com/whatsmyip.php
... Image ...
... but how come you have the same IP as me ? :lol:
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: How can I obtain my internet address?

Post by Marlin »

heartbone wrote:Although I'm still not convinced that code is not dependent on an external website
I understand, that things may [sometimes] not be what they appear to be, especially in circumstances with unclear perception,
but I don't believe the compiler will betray us in such a way as to compile white to black and black to white.

Code that does access external sites, does access external sites.
Code that does not access external sites, does not access external sites.

Considering that the opposite should be true is ludicrous, or an indication of damaged logic.

When needing a response from an external site, you need to wait for it to arrive,
which can take some time, especially when there is a lot of traffic.

Vera wrote:I could fax my biography to you :mrgreen: :lol:
No, I'm currently not prepared to receive faxes.
You could mail it to me though. ;) That could be interesting. 8)
On the second thought, listening is easier then reading. ;)
Maybe a movie would be even better. :mrgreen:
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: How can I obtain my internet address?

Post by heartbone »

netmaestro wrote:I will never leave you or forsake you:

http://lloydsplace.com/whatsmyip.php
I've added your contribution into the mix.

Here is yet another answer to the topic's question.
If you insert the GetExternalIPAddress() procedure listed at the top of this page into the top of the following 3 line code snippet, and compile it into an executable, then you 'll have a handy little utility.

Code: Select all

InitNetwork() 
ExamineIPAddresses()
MessageRequester("EXTERNAL IPv4 = "+GetExternalIPAddress(),"INTERNAL LAN ADDRESS = "+IPString(NextIPAddress()),#PB_MessageRequester_Ok)
Unfortunately the AVIRA Free antivirus that I use in Windows® freaks out and won't let me run it without first turning off the AV. :(

Security Alert
Type: Detection
A virus or unwanted program 'TR/ATRAPS.Gen' was found in file 'C:\PureNasic\miscellaneous\ShowMyIPs\ShowMyIPs.exe'.
Access to this file was denied.

---more information ----
Special detection
TR/ATRAPS.Gen

Description:
A generic detection routine designed to detect common family characteristics shared in several variants.

This special detection routine was developed in order to detect unknown variants and will be enhanced continuously.
Last modificaion: • 7.09.10.38/8.02.10.38 ( 04/04/2012 )
-----------------------

So it seems as if I'll have to massage the executable to satisfy the AV before I deploy this tool.
Keep it BASIC.
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: How can I obtain my internet address?

Post by Marlin »

heartbone wrote:Unfortunately the AVIRA Free antivirus that I use in Windows® freaks out and won't let me run it without first turning off the AV. :(
Those "Anti"virus programs are sometimes outright dangerous.
There was one, that deleted my application without recourse - sometimes.
Other times it merely blocked it, other times (some updates later) it ignored it completely.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
User avatar
HeX0R
Enthusiast
Enthusiast
Posts: 686
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: How can I obtain my internet address?

Post by HeX0R »

Vera wrote:
heartbone wrote:??? :?:
Vera, if you look at the original post from 4 years ago...
You might like to see an even more original post from 2008 ... and a hint suggesting, that if you change the address all will be well :-)
Somehow you too figured out that there was a change :wink:
Exactly, but anyway the situation is far from being perfect, thinking about programs which used the old domain to obtain the ip.
I had to switch because dynDNS wanted to have money for the redirector.
Therefore I decided to better get a whole server than paying for nothing but a redirection.

The new domain should be safe until I die ;)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: How can I obtain my internet address?

Post by Rescator »

Do note that the IP may not be guaranteed.
For example your LAN may use 192.168.x.x and then your WAN and ISP may use 172.x.x.x and so on, depending on the ISP you may actually be behind a ISP NAT, and I think (my router knowledge is limited when it comes to NAT) it is possible that two machines (or routers) on the Same ISP network may end up referring to each other using the 172.x.x.x address.
So your machine may have 3 IP's, one LAN IP, one iSP IP (via your routers WAN) and one "Internet" IP via the ISP's WAN.
Now if a PC gets a IP for a internal network then it will probably throw an error (unless another ISP are using the same range for their internal network).

But normally, asking a website to echo back the IP of yourself tend to work, provided that server isn't within the same ISP local network as your router, I have no idea what will be returned in that case. (anyone got a clue?).
User avatar
Thunder93
Addict
Addict
Posts: 1773
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

Typically when you behind Router (or ISP Modem + Router capable) you'll be with Internal / Private IP address. Differs from External IP address or true IP that is giving by the ISP, and that is echo backed by sites like whatsmyrealip.com.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Hi-Toro
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Apr 26, 2003 3:23 pm

Re: How can I obtain my internet address?

Post by Hi-Toro »

Another option, for those who want to self-host an IP checker:

Code: Select all

<?php

if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
	$ip   = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
	$ip   = $_SERVER["REMOTE_ADDR"];
}

echo $ip;

exit();

?>
Save as ip.php, upload to your own web space, then visit in your web browser for quick test. (The HTTP_X_FORWARDED_FOR check deals with non-anonymous proxies such as those used by many ISPs.)

My web space uses chunked encoding (out of my control), which makes it a little trickier to read the value, but the code below (not beautiful/foolproof, was just for my own use!) checks for chunked encoding and decodes as necessary, but I've only tested it on my site, which returns the IP string only. Note that Lloyd's Place returns a full web page, using non-chunked encoding, so on testing it will return the full HTML for the page, which you'd have to parse for the IP address.

Code: Select all


InitNetwork ()

Procedure.s GetIP ()
	
	; Chunked encoding example...
	
	; NB. Returns IP string only...
	
	host$  = "www.hi-toro.com"	; NOTE: Leave out http://
	file$ = "/ip.php"			; Needs slash at start!
	
	; Non-chunked encoding example...
	
	; NB. Returns full page HTML including IP as string...
	
	;host$ = "lloydsplace.com"
	;file$ = "/whatsmyip.php"
	
	crlf$  = Chr (13) + Chr (10)
	
	ip$ = ""
	
	chunked = #False
	
	www = OpenNetworkConnection (host$, 80)
	
	If www
		
		; Only valid for chunked response from http://hi-toro.com/ip.php, may need
		; amending for other hosts...

		downloadsize = 65536

		*get = AllocateMemory (downloadsize)
	
		SendNetworkString (www, "GET " + file$ + " HTTP/1.1" + crlf$)
		SendNetworkString (www, "Host: " + host$ + crlf$)
		SendNetworkString (www, "User-Agent: " + "PB Downloader" + crlf$)
		SendNetworkString (www, "Accept: text/plain" + crlf$)
		SendNetworkString (www,  crlf$)
	
		recvd = 0
		gotnd = #False ; netdata is zero until data starts coming in...
		
		Repeat
		
			netdata = NetworkClientEvent (www)
			
			If netdata = #PB_NetworkEvent_Data

				gotnd = #True ; Can read events until zero after we have an event!
				recvd = ReceiveNetworkData (www, *get + recvd, downloadsize - recvd)

			EndIf
			
			Delay (100)
			
		Until gotnd = #True And netdata = 0 ; Had a response and no more data
		
		response$ = PeekS (*get)
		
		If FindString (response$, "Transfer-Encoding: chunked")
			chunked = #True
		EndIf
		
		; Debug response$
		
		; EXAMPLE RESPONSE, comes as a single string (with newline characters):
		
		; HTTP/1.1 200 OK								; <----- [START OF HEADER]
		; Date: Tue, 20 Sep 2011 14:13:34 GMT
		; Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
		; X-Powered-By: PHP/5.2.9
		; Transfer-Encoding: chunked
		; Content-Type: text/html
		; 												; <----- [END OF HEADER (blank line)]
		; e												; <----- [CHUNKED RESPONSE: BYTES IN HEX, E (14) HERE]
		; 99.100.101.102								; <----- [EXAMPLE IP ADDRESS HERE!]
		; 0												; <----- [ZERO DENOTES END OF DATA CHUNK]
		; 												; <----- [BLANK LINE INDICATES END OF ALL DATA]
		
		; SKIP HEADER...
		
		If chunked
			
			Repeat
				eol = FindString (response$, crlf$, 1)
				thisline$ = Left (response$, eol - 1)
				response$ = Right (response$, Len (response$) - (eol + 1))
			Until thisline$ = ""
			
			; FIND THE LINE CONTAINING THE IP (second line in this case, but should really read
			; the number of bytes, $e, and then the data. Still, it comes separated with newlines,
			; so what the hell!)...
			
			count = 0
			
			Repeat
	
				count = count + 1
	
				eol = FindString (response$, crlf$, 1)
				thisline$ = Left (response$, eol - 1)
				response$ = Right (response$, Len (response$) - (eol + 1))
	
				If count = 2
					ip$ = thisline$
				EndIf
	
			Until thisline$ = ""
			
		Else
			
			; Non-chunked...
			
			Repeat
				eol = FindString (response$, crlf$, 1)
				thisline$ = Left (response$, eol - 1)
				response$ = Right (response$, Len (response$) - (eol + 1))
			Until thisline$ = ""
			
			ip$ = response$
			
		EndIf
		
		CloseNetworkConnection (www)
		
	EndIf
	
	ProcedureReturn ip$
	
EndProcedure

Debug GetIP ()

James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: How can I obtain my internet address?

Post by heartbone »

OK, I'm trying to grok all this.
I am assuming that self-host means that it's a file on an internet site that's under your control, or one where you can post pages and scripts to.
And that you've put a script

Code: Select all

<?php

if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
   $ip   = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
   $ip   = $_SERVER["REMOTE_ADDR"];
}

echo $ip;

exit();

?>
on a that server which when accessed returns the IP.
At first glance that seemed close in function to cases 1 & 3 in GetExternalIPAddress() above, and was wondering why we couldn't build code like it to use from our apps.
Then I realized that the script must only work from an internet server.
It is interesting to see the processing innards of grabbing data from a server.
I'll assume that what's in your ip.php code is essentially what is going on at the other web sites when they extract the ip and send it back to me.
I hope that I've got it. :?
Thanks.
Keep it BASIC.
Hi-Toro
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Apr 26, 2003 3:23 pm

Re: How can I obtain my internet address?

Post by Hi-Toro »

Yeah, that sounds about right. In most cases these days you can't get your own IP address other than from an external source (whereas connecting via an old analogue modem used to give you your public IP), hence running the PHP script from your web space allows your local PC to connect to a known server that can actually 'read' the remote IP making the request (ie. the remote IP making the request being your local PC).

Your own router would be able to tell you your IP address, but because there are so many routers with different GUIs/protections out there, it would in practise be really difficult even to ask your own router for its public IP address. They really ought to come up with a standard interface across routers for at least this basic piece of information!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: How can I obtain my internet address?

Post by Marlin »

Hi-Toro wrote:They really ought to come up with a standard interface across routers for at least this basic piece of information!
"They" came up with upnp quite some time ago (example code above; second code tag).

An old router would not necessarily come up to date all by itself - some might get something like automatic updates, though I tend to reserve myself some control over such things.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
Hi-Toro
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Apr 26, 2003 3:23 pm

Re: How can I obtain my internet address?

Post by Hi-Toro »

UPnP is frequently disabled, though, isn't it? The example fails to connect to my router, too, even though I've amended the IP (and I just logged in to confirm UPnP is enabled).

Interesting to see an example of UPnP usage, though, never really known much about it before.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Post Reply