How can I obtain my internet address?
How can I obtain my internet address?
Hi to all
I'm connected to internet throuht a ADSL router. From a LAN PC, how can I obtain my public address?
Thanks in advanced.
I'm connected to internet throuht a ADSL router. From a LAN PC, how can I obtain my public address?
Thanks in advanced.
The PB community is great... nice to meet you!
Re: How can I obtain my internet address?
I am unsure who the author of the following is?
Code: Select all
ProcedureDLL.s OuterIP()
#INTERNET_FLAG_RELOAD = $80000000
Bytes.l = 0
Html.s = Space(512)
IPAddr$ ="";
hInet.l = InternetOpen_("http://checkip.dyndns.org", 1, #Null, #Null, 0)
If hInet
hURL.l = InternetOpenUrl_(hInet, "http://checkip.dyndns.org", #Null, 0, #INTERNET_FLAG_RELOAD, 0)
If hURL
If InternetReadFile_(hURL, @Html, Len(Html), @Bytes)
Html = Trim(Html)
Debug html
For i= 1 To Bytes
If (Mid(Html,i,1) >= "0" And Mid(Html,i,1) <= "9") Or Mid(Html,i,1) = "."
IPAddr$ = IPAddr$ + Mid(Html,i,1)
EndIf
Next
InternetCloseHandle_(hURL)
Else
ProcedureReturn "Failed"
EndIf
Else
ProcedureReturn "Failed"
EndIf
InternetCloseHandle_(hInet)
Else
ProcedureReturn "Failed"
EndIf
ProcedureReturn IPAddr$
EndProcedure
Debug OuterIP()
I may look like a mule, but I'm not a complete ass.
Re: How can I obtain my internet address?
waaaay too many code
Code: Select all
InitNetwork()
Debug StringField(GetHTTPHeader("http://h3x0r.ath.cx/Sonstiges/ShowMyIp12.php"), 2, Chr(34))
{Home}.:|:.{Dialog Design0R}.:|:.{Codes}.:|:.{Downloads}.:|:.{History Viewer Online}
Re: How can I obtain my internet address?
Thanks to all!
Greetings
Greetings
The PB community is great... nice to meet you!
Re: How can I obtain my internet address?
Thank you kind sir for your contribution.HeX0R wrote:waaaay too many codeCode: Select all
InitNetwork() Debug StringField(GetHTTPHeader("http://h3x0r.ath.cx/Sonstiges/ShowMyIp12.php"), 2, Chr(34))
For program longevity I wanted 3 methods to get this information, and you provided one.
Code: Select all
If InitNetwork()=0
MessageRequester("SOFTWARE","could not initialize network interface.",#PB_MessageRequester_Ok)
NETWORKED= 0
Else
NETWORKED= 1
; OBTAIN EXTERNAL AND LOCAL IP ADDRESSES.
If ReceiveHTTPFile("http://ip.gwhois.org/","IPADDRESS")
; OPEN DOWNLOADED FILE AND EXTRACT IP ADDRESS FROM IT.
ReadFile(1,"IPADDRESS")
EXTADDR$= ReadString(1)
CloseFile(1)
DeleteFile("IPADDRESS")
If Not FindString(EXTADDR$,".") : EXTADDR$= "" : EndIf
EndIf
If EXTADDR$=""
; SECOND ALTERNATIVE.
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
If Not FindString(EXTADDR$,".") : EXTADDR$= "" : EndIf
If EXTADDR$=""
; THIRD ALTERNATIVE.
EXTADDR$= StringField(GetHTTPHeader("http://hex0rs.coderbu.de/Sonstiges/ShowMyIp12.php"),2,Chr(34))
If Not FindString(EXTADDR$,".") : EXTADDR$= "" : EndIf
EndIf
EndIf
; GET INTERNAL (LAN) IP ADDRESS.
ExamineIPAddresses() : IND= NextIPAddress()
If IND
INTADDR$= IPString(IND)
EndIf
EndIf
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.
Keep it BASIC.
Re: How can I obtain my internet address?
anytime you use a 3rd party website (perhaps, even websites in general) you run the risk of things changing or being temporarily down or permanently done.
I would consider using something that probably won't go away, like google and just use their search... (https://www.google.com/search?q=my+ip) and parse the result Of course, the format could change w/o notice, but chances are, it'll be there far into the future.
If I use an external website, I usually try to make the extraction as generic as possible and store the parameters in a text file, so even if I am not available, the end user has a shot at updating the extraction parameters to succeed.
I would consider using something that probably won't go away, like google and just use their search... (https://www.google.com/search?q=my+ip) and parse the result
Code: Select all
StringField(StringField(HTML$,2,"<div class="+#DQUOTE$+"_h4c _rGd vk_h"+#DQUOTE$+">"),1,"</div>"))
If I use an external website, I usually try to make the extraction as generic as possible and store the parameters in a text file, so even if I am not available, the end user has a shot at updating the extraction parameters to succeed.
Re: How can I obtain my internet address?
http://ip.gwhois.org/ looks rather permanent to me, but who knows?
I'll try to work your suggestion into my little snippet.
Thanks for the feedback jassing.
I'll try to work your suggestion into my little snippet.
Thanks for the feedback jassing.
Keep it BASIC.
Re: How can I obtain my internet address?
My best solution for that so far is to ask the router via upnp for its external ip address
- the router must not be set up to suppress that of course.
Send a POST request to: to the routers server at port 49000 (upnp port) with the following post data: (A normal single line of post data iirc )
The reply will contain the external ip address (if there is one) between and
Despite the fact, that especially the post data look rather bloated, it works quite fast.
Edited: to add the required port for upnp requests.
- the router must not be set up to suppress that of course.
Send a POST request to:
Code: Select all
/upnp/control/WANIPConn1
Code: Select all
<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" /></s:Body></s:Envelope>
The reply will contain the external ip address (if there is one) between
Code: Select all
<NewExternalIPAddress>
Code: Select all
</NewExternalIPAddress>
Edited: to add the required port for upnp requests.
Last edited by Marlin on Thu Dec 18, 2014 2:19 pm, edited 1 time in total.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
- George Orwell
- netmaestro
- PureBasic Bullfrog
- Posts: 8425
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: How can I obtain my internet address?
Marlin, could you put that in a runnable test please?
BERESHEIT
Re: How can I obtain my internet address?
It is possible, that I have made a mistake posting that here - I am uncertain about that at the moment.netmaestro wrote:Marlin, could you put that in a runnable test please?
To clarify that, I'd like to ask:
Assuming that my current condition - physical or environmental... - would not permit me to do the work
required to provide such a "runnable test", so that I might assume another could do that more readily and easily, than me,
should I have withheld posting, what I could post easily enough?
I feel very exhausted at this time, the above data was readily available to me - in a config file of a more complex framework...
Looking at that now, I see that there are more details, that I should (and can) provide...
I already supplemented the upnp port above.
There should be a buffer containing the request - ASCII data with lines separated with $CRLF:
Code: Select all
POST /upnp/control/WANIPConn1 HTTP/1.1
Host: your.router
User-Agent: your-useragentname
Content-Type: text/xml; charset="utf-8"
Content-Length: 275
SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress
Connection: close
<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" /></s:Body></s:Envelope>
Code: Select all
yourConnection = OpenNetworkConnection(yourRouter, 49000, #PB_Network_TCP)
Code: Select all
SendNetworkData(yourConnection, *yourBuffer, youBufferLength)
possibly making shure, you got all or at least enough of the reply.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
- George Orwell
Re: How can I obtain my internet address?
Marlin, your suggestion appears to be dependent on http://schemas.xmlsoap.org/.
So in what way is this any better than the previous suggestions?
I found an alternative site that can be used with my suggested method.
http://ipecho.net/plain
I reckon the more these sites are used, the less likely they are to disappear.
I'll try to figure out a way to randomize the use of these sites in my programs instead of always hitting the same one.
So in what way is this any better than the previous suggestions?
I found an alternative site that can be used with my suggested method.
http://ipecho.net/plain
I reckon the more these sites are used, the less likely they are to disappear.
I'll try to figure out a way to randomize the use of these sites in my programs instead of always hitting the same one.
Keep it BASIC.
Re: How can I obtain my internet address?
It is probably not dependent the way you think it would be.heartbone wrote:Marlin, your suggestion appears to be dependent on http://schemas.xmlsoap.org/.
"http://schemas.xmlsoap.org/" is merely part of the string the upnp (Universal Plug aNd Play iirc) requires in the communication.
This method only connects to the router (via upnp), and so only requires a router supporting upnp.
It should continue to work as long as that upnp stays compatible (providing hardware and network... keep working )
This does look nice and simple and is rather simple to implement.heartbone wrote:I found an alternative site that can be used with my suggested method.
http://ipecho.net/plain
I started out in similar ways.
After too many of my original sites, providing the external ip address, became unavailable,
I resorted to parsing the page provided by the router.
After a few firmware upgrades, this became very slow,
but now the router was ready for upnp, and that is very fast compared to the web interface.
- and does not require a login.
This method will not open a (new) internet connection, as it does not connect to external sites.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
- George Orwell
Re: How can I obtain my internet address?
Thanks for the information Marlin.
If you would provide a snippet, then it would be better understood.
Until then.
If you would provide a snippet, then it would be better understood.
Until then.
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
Last edited by heartbone on Tue Dec 23, 2014 2:06 am, edited 2 times in total.
Keep it BASIC.
Re: How can I obtain my internet address?
I wouldn't consider 6 years availability to be unstableheartbone 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.
Btw: thanks for your collection and inspiration how to get it.
Here's a further variation you might like to add:
Code: Select all
InitNetwork()
If ReceiveHTTPFile("http://www.cmyip.com", "IPADDRESS")
ReadFile(1, "IPADDRESS")
While Eof(1) = 0
EXTADDR$ = ReadString(1)
If FindString(EXTADDR$, "My IP Address", 1)
Dim sIP.s(0)
CreateRegularExpression(0, "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
If ExtractRegularExpression(0, EXTADDR$, sIP()) > 0
Debug sIP(0)
EXTADDR$ = sIP(0)
EndIf
FreeRegularExpression(0)
Break ;End
EndIf
Wend
CloseFile(1)
DeleteFile("IPADDRESS")
EndIf
Debug EXTADDR$
Code: Select all
<?php
echo $_SERVER['REMOTE_ADDR']
?>
@jassing
From my own experience to maintain scripts running on g00g pages I can tell you it's the worst side to choose.
Also the returned search results may contain IP-dummies, so you can't be too sure if you'd grab the 'right' one.
Hi Marlin,
nice to meet you again
cheers ~ Vera
Last edited by Vera on Fri Dec 19, 2014 9:22 am, edited 1 time in total.
Two growing code-collections: WinApi-Lib by RSBasic ~ LinuxAPI-Lib by Omi
Missing a download-file on the forums? ~ check out this backup page.
Missing a download-file on the forums? ~ check out this backup page.
Re: How can I obtain my internet address?
Apparently I'm not the only one [having been] handicapped.
Even though, I was the one proclaiming that most loudly.
There are also 2 distinctly different possibilities to get ones external ip address, depending on the given circumstances.
If your router can be reached via DNS, when using Dynamic DNS [for running a server or whatever],
you can use a simple trick, someone once posted in this forum, like:
That is, if some other mechanism is making sure, the DNS entry of your router stays up to date.
Also such a router supporting Dynamic DNS, can be set up to report the newly gotten external ip address to some server. Unless the router is restricted in that regard, it can report its new ip to a server running on your machine as well.
It will only give you updates on changes and your server must wait for such.
- If that is already in use, my previous suggesting should work.
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.
Provided as example code, that can and probably should be made safer
- should f.e. not blindly trust the router [to close the connection in time] the way it currently does.(?)
You should be able to see the striking similarities to my post above.
If you can't, I'll be disappointed.
Hi Vera
reading you, is loving you.
Edit1: Supplemented a code comment.
Even though, I was the one proclaiming that most loudly.
There are also 2 distinctly different possibilities to get ones external ip address, depending on the given circumstances.
If your router can be reached via DNS, when using Dynamic DNS [for running a server or whatever],
you can use a simple trick, someone once posted in this forum, like:
Code: Select all
Connection = OpenNetworkConnection(YourDnsName, 0, #PB_Network_UDP)
ExtIp = GetClientIP(Connection)
CloseNetworkConnection(Connection)
Also such a router supporting Dynamic DNS, can be set up to report the newly gotten external ip address to some server. Unless the router is restricted in that regard, it can report its new ip to a server running on your machine as well.
It will only give you updates on changes and your server must wait for such.
- If that is already in use, my previous suggesting should work.
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.
Code: Select all
EnableExplicit
; Get the external ip address from router using upnp
; compile as ASCII - NOT unicode, so the string can easily be used as buffer
#UserAgent = "UPNP ext. IPaddr"
#Router = "fritz.box" ; Adapt to your router
#StartToken = "<NewExternalIPAddress>"
#EndToken = "</NewExternalIPAddress>"
#recBufferLen = 2000
Macro Msg (pMessage, pFlags = #PB_MessageRequester_Ok)
MessageRequester(#UserAgent, pMessage, pFlags)
EndMacro
Define strBuffer.s, strReply.s, strIp.s
Define upnpConnection.i, nEvent.i
Define *recBuffer, recByteLen.i
Define Pos1.i, Pos2.i
Procedure.s FieldQuote (pFieldName.s)
ProcedureReturn Chr(34) + pFieldName + Chr(34)
EndProcedure
If InitNetwork() = 0
Msg("The network could not be initialized")
End
EndIf
*recBuffer = AllocateMemory(#recBufferLen)
If *recBuffer = 0
Msg("Memory allocation failed.")
End
EndIf
strBuffer = "POST /upnp/control/WANIPConn1 HTTP/1.1" + #CRLF$
strBuffer + "Host: " + #Router + #CRLF$
strBuffer + "User-Agent: " + #UserAgent + #CRLF$
strBuffer + "Content-Type: text/xml; charset=" + FieldQuote("utf-8") + #CRLF$
strBuffer + "Content-Length: 275" + #CRLF$
strBuffer + "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" + #CRLF$
strBuffer + "Connection: close" + #CRLF$
strBuffer + #CRLF$
strBuffer + "<?xml version=" + FieldQuote("1.0") + " encoding=" + FieldQuote("utf-8") + "?><s:Envelope xmlns:s=" + FieldQuote("http://schemas.xmlsoap.org/soap/envelope/") + " s:encodingStyle=" + FieldQuote("http://schemas.xmlsoap.org/soap/encoding/") + "><s:Body><u:GetExternalIPAddress xmlns:u=" + FieldQuote("urn:schemas-upnp-org:service:WANIPConnection:1") + " /></s:Body></s:Envelope>"
upnpConnection = OpenNetworkConnection(#Router, 49000, #PB_Network_TCP, 1000)
If upnpConnection = 0
Msg("Could not connect to " + #Router)
End
EndIf
SendNetworkData(upnpConnection, @strBuffer, Len(strBuffer))
Repeat
Delay(60)
nEvent = NetworkClientEvent(upnpConnection)
Select nEvent
Case #PB_NetworkEvent_Disconnect
Break
Case #PB_NetworkEvent_Data
recByteLen = ReceiveNetworkData(upnpConnection, *recBuffer, #recBufferLen)
strReply + PeekS(*recBuffer, recByteLen, #PB_Ascii)
EndSelect
ForEver
CloseNetworkConnection(upnpConnection)
Pos1 = FindString(strReply, #StartToken)
If Pos1
Pos1 + Len(#StartToken)
Pos2 = FindString(strReply, #EndToken)
If Pos2 > Pos1
strIp = Mid(strReply, Pos1, Pos2 - Pos1)
EndIf
EndIf
If strIp
Msg("External IP address: " + strIp)
Else
Msg("Failed to get external IP address.")
Debug strReply
EndIf
- should f.e. not blindly trust the router [to close the connection in time] the way it currently does.(?)
You should be able to see the striking similarities to my post above.
If you can't, I'll be disappointed.
Hi Vera
reading you, is loving you.
Edit1: Supplemented a code comment.
Last edited by Marlin on Sun Dec 21, 2014 6:24 am, edited 1 time in total.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
- George Orwell