How To Get Internet IP???

vbtester
05-25-2002, 09:15 AM
If I am behind a router, and need to determine my external IP address, that is the IP address which my router uses to connect to the internet, how would I do this?

Lets assume my computer is 192.125.11.4, and my router is 192.125.123.123, how would the first computer at 192.125.11.4, determine the IP address the router is using to connect to the internet.

Please advise
DC

dcl3500
05-25-2002, 10:34 AM
If your router has an HTML based control panel like the linksys routers (at least mine does) then you could open the appropriate page in the browser control and parse the page's source to grab the IP address. On the linksys routers the page would be like 192.xxx.xxx.xxx/status.htm

vbtester
05-25-2002, 02:02 PM
Thanks for your reply.

I need to programmatically be able to determine the IP address on all machines which my program is run on.

I cannot guarantee that a client is going to be using a linksys router. What would i do in the case that they have an Asante router or some other brand.

Secondly, to connect to the router, the client would need to know the IP address and Port which the router is using to allow the HTML login to access that Status.html page you mentioned.

Lets assume the user does not know what port the router communicates on for logging in, and also assume that the client has a different router than yourself or I.

In this situation, how can we go about determining the WAN IP?

Please advise.
DC
:eek:

Thinker
05-25-2002, 02:07 PM
If the router is doing DHCP/NAT, without interfacing directly to the
router, you can't. It is a firewall, and what goes on inside the wall
shouldn't need to know what is going on outside the wall. (I am
not saying your program might not have a good reason for
needing this info, but your program will have to negotiate with the
router for it.)

I guess there might be some kind of server on the internet you
could send a request to and it would return the IP.

vbtester
05-25-2002, 04:14 PM
I had considered using some kind of server on the Internet, but the problem with this follows:

1) I would have to host and maintain this server. I would need to make sure that the server never went down or the client apps would no longer work.

2) My ISP constantly changes my IP Address, so I would be unable to run a dedicated server for resolving peoples IP addresses.

Is it possible to have an HTML document with some Scripting determine a users IP address then ship it back via some socket? Im not sure this would work because I think I would need some sort of daemon running at all times. However my thinking is, I could upload a webpage, then instruct my app to connect to that webpage, the webpage would somehow determine the IP address and ship it back to the Visual Basic program. Or I could just make my VB App download that webpage. Im not sure if this would work, or how I should go about dealing with this problem. Any suggestions are welcomed :o)

NOTE: What's interesting about this. This is a really difficult problem to solve, I have yet to see or find a solution anywhere, I have been doing research on this topic for the last 2 weeks. Everyone I contact is unsure about how to handle this problem. :o) I hope someone can help change this :o)

Thanks for your reply.

Thinker
05-25-2002, 04:43 PM
If you can get some kind of active HTML server that can do ASP,
PHP, or CGI/Perl, then it seems likely that it could be scripted to
return the IP address as a small HTML page. That should be quite
easy to work with. The main problem is getting the server access.
Others have posted in the past about some pretty cheap web
hosting services that support some kind of server-side scripting.

vbtester
05-25-2002, 05:29 PM
Interesting, so it seems the only way to solve this problem is using a webserver of some sort.

So it would seem that for every client app i release, I would be responsible for making sure that webserver was running and sending IP address back.

Is there no other way to do this? Its hard to beleive it's so complicated to find your Internet Address.

Please let me know if you think this is the only way to sovle this problem.

Thanks again for your posts.

Thinker
05-26-2002, 08:39 AM
I have prefixed every suggestion based on the idea that your
router is doing DHCP/NAT. Once again, if this is the case, then
your concept of whose internet address is whose is wrong. None
of the client computers connected to the router have an 'internet'
IP address. The only piece of equipment that does is the router.
This means that the client program has to have something to ask.
As has already been said, this means asking the router, or some
web site that sees what IP the router has assigned. Since this IP
only belongs to the router, you can't think there will just be some
automatic way for the client to get access to it.

vbtester
05-26-2002, 09:04 AM
I had a recent conversation with somone on MIRC. They told me that they had encountered the same problem as this a while back.

He told me that it's possible to obtain the routers Internet IP Address using some API call.

He was trying to find the code, but lost it :( He told me that accessing the Internet would be one way to obtain the IP, but he said it can also be done using an API. How much of that is actually t rue, im not sure.

So lets go with your logic. This means I need to connect to a website. Is it possible to have an HTML webpage using some small script detect the IP address? Or do I need to have a webserver running some IP resolving application?

Please let me know what you would do in this situation.

Thanks again for your post :)

Agent
05-26-2002, 09:10 AM
Click (http://claytonalcock.com/mailform.php). I don't know if it shows the IP you want but, yes, you can use PHP to get your IP.

Thinker
05-26-2002, 09:12 AM
I can't tell you what I would do because I can't think of a reason
why a program on the inside of a firewall would need this info.
As I already said, that doesn't mean there isn't a reason, but
whatever you try to do with that address will just be getting
around what the router is doing, something that circumvents the
firewall protection that NAT provides.

Thinker
05-26-2002, 09:15 AM
Yes Agent, this is the IP that he would be after.

Agent
05-26-2002, 09:22 AM
So, am I right when I say this:

He has been disallowed access to the IP he wants.
He wants the IP now.

???

Thinker
05-26-2002, 09:29 AM
No, just that he is trying to find a direct way for his program to
get the IP address directly from the router rather than having to
set up a web hosting situation that he would have to maintain for
all his clients.

Agent
05-26-2002, 10:14 AM
Well, you would need a PHP interpreter of some sort to
parse your scripts. Maybe a web hosting service if you
have one (you need a PHP enabled one).

You would use the function GetEnv() (http://www.php.net/manual/en/function.getenv.php) to get the
environment variable. To get the host, you would
use the function GetHostByAddr().

<?
$ip1 = getenv(HTTP_X_FORWARDED_FOR);
$host1 = gethostbyaddr($ip1);

$ip2 = getenv(REMOTE_ADDR);
$host2 = gethostbyaddr($ip2);

Print $ip1 : $host1 || $ip2 $host2;
?>I think I got that right.

Derek Stone
05-26-2002, 11:38 AM
I'm fairly sure you can resolve this problem without an external server. It involves the use of the Winsock API, which to many seems daunting, but its far from complicated. Basically what you do is call gethostbyname, which will return a long value that you copy into a HOSTENT structure. The structure will have a list of your IP addresses, one of which will be external. The code below should get you started:


Dim HostName As String * 256
Dim HostAddress As Long
Dim Host As HOSTENT
Dim HostIP As Long

gethostname(HostName, 256)

HostName = Trim$(HostName)

HostAddress = gethostbyname(HostName)

CopyMemory Host, HostAddress, LenB(Host)
CopyMemory HostIP, Host.hAddrList, 4


Of course this code isn't complete and you'll need to declare all of the structures and API functions.

I'll see if I can find a better example.

Good Luck
-CL

ChiefRedBull
05-26-2002, 01:31 PM
If you wana horrible slimy method, simply download http://www.whatismyip.com and parse it out.. eurgh.

Agent
05-26-2002, 01:59 PM
Dag on Chief! What will they think of next!?

http://www.mybackitches.com ???
http://www.ineedfoodhowaboutpizza.com ???

ChiefRedBull
05-26-2002, 02:17 PM
LOL, lets hope so...

www.getmeabeerwhileianswerthisquestion.com... :D

vbtester
05-26-2002, 06:11 PM
Chief, I beleive this would be a good solution if I could be guarentted that they would never change the format of thier webapge. I could parse an HTML document no problem.

But consider what will hapen to my application if they changed their webpage name, style or documentation.

My program would no longer work.

Im hearing a few people say they have solved this problem using API, but its amazing no one knows how to do it :) I even checked planetsourcecode and they had no code using an API.

If a few people have implemted this, Im dieing to see a working copy. I would be in heaven if someone could show me how to solve this problem with out using a webserver of some sort.

I bow before the human genious who can show me how to use this API :) :)

Thinker
05-26-2002, 06:44 PM
I can't guarantee 100% that it isn't possible via API, but I would
bet $10 on it right now that it isn't (once again, only if the router
is doing NAT, something you have yet to confirm).

BillSoo
05-26-2002, 07:40 PM
When I want to know what *my* IP address is, I use winipcfg. You could probably shell this app, then try and extract the IP from it....or there may be some kind of command switch you could use....

vbtester
05-26-2002, 08:54 PM
Yes the router acts as a firewall, and even if it didnít we need to assume that it does, if im to guarantee that my program will run on all client machines.

The whole purpose of needing to get this IP address is so people who are behind a router can determine their IP address so that they are able to run a WEB or FTP server. I know you've been questioning my reason for needing this functionality. So thatís the reason :)

To setup and maintain some remote server is a hassle. Itís just hard to believe that the only way to do this is VIA a remote server. There have been a few people to tell me that this can be done using an API. As a matter of fact some one posted some sample code in this message thread. I had one person tell me that a long time ago they actually did solve this exact problem using an API. I would love to see this!

To maintain and pay for a server every month is not necessarily feasible. There has to exist a method of doing this with out needing to run a web server.

In the event that the only possible way to solve this problem is with a Web Server, im hoping I can use a simple HTML page with some java / vb script to detect and display the IP address. I hope this is possible!?!?!?! I dont want to have to run IIS in order to make this app work.

I have been pulling my hair out with this problem and would love to get it solved :)

Thanks again for your posts!

Thinker
05-26-2002, 09:45 PM
The whole purpose of needing to get this IP address is so people who are behind a router can determine their IP address so that they are able to run a WEB or FTP server. I know you've been questioning my reason for needing this functionality. So thatís the reason
In the context of DHCP/NAT/router/firewall, this information is
useless. A firewall by definition blocks all outside connections.
Even if you got the outside IP, no one would be able to make
either an HTTP or FTP connection to a server behind the firewall.
The only way this can happen is to configure the router specifically
to allow it, which then makes it totally unnecessary for any app
running on any server inside the firewall to know this IP.

vbtester
05-26-2002, 11:12 PM
I think your not understanding the point of the app. I understand that the router by default will block all outside connections. However, you are able to set a PC to beable to be accessed outside of the routers firewall.

Haveing said that, if you want to run an FTP Server on a PC behine the router you would need to know the Internet IP so that people would be able to connect.

Therefore it is NOT usless for the app insdie the firewall to beable to know the Internet IP address.

What if I wanted to make an app that would tell me every time my IP address changed?? Then I would need this app to bealbe to detect the Internet IP.

There is a very good reason for my thinking on this, and there is a definite reason why I need to beable to figure out how to do this. I compeltely understand how a firewall works on routers, but I also understand its possible to run a WEB or FTP server on a computer which is behinde the router. Therefore in summary it comes crutial to know the Internet IP of the router, with out it your WEB/FTP server is useless.

Thanks again for your post.

Thinker
05-26-2002, 11:22 PM
Knowing that the router has to be configured for this, I don't
understand your reluctance to try to communicate directly with
the router as dcl3500 first suggested.

Something else about what you are saying bothers me.
Haveing said that, if you want to run an FTP Server on a PC behine the router you would need to know the Internet IP so that people would be able to connect.
What if I wanted to make an app that would tell me every time my IP address changed?? Then I would need this app to bealbe to detect the Internet IP.
If the IP is dynamic, how would anyone outside find out the IP to
connect? HTTP and FTP servers don't work very well on dynamic
IP addresses.

Oh well, I have just been saying the same thing over and over
and it doesn't seem to be getting through so I think I will bow out
and let someone else try to help.

ChiefRedBull
05-27-2002, 04:56 AM
Have a bash at the GetIpAddrTable API. It might do it, it might not, looks like it could do though, seeing as the router IP must be known to windows, or else how does it route stuff towards the router? This api grabs a list of all the installed ips, so you can read them.

vbtester
05-27-2002, 07:51 AM
Thanks for that API post. Unfortunatly it was only able to obtain a list of the local IP addresses. I need a way to get the routers IP. I agree that windows must some how know that IP address if it sends and receives from the Internet.

On the ohter than the router handles all the incomming and outgoing internet traffic. So.... im wondering if windows really does know what the Internet IP address is.....

Thanks again for your post.

Banjo
05-27-2002, 02:00 PM
No, Windows has no idea what Internet facing IP is being used for its communications. That's the whole point. It only knows its local IP and the IP of the gateway; in this case the router. When a client machine establishes a TCP connection the router remembers which client is connected to which socket and uses that information to route the return data to the correct internal IP.

ccb
05-28-2002, 12:06 PM
I know that if it would work one of our "VB-gods" would have known and already mentioned that possiblity but:
what about checking "netstat.exe" ?
if it shows you some connections then I would have a prog that does the same and more

vbtester
05-28-2002, 01:00 PM
I have checked netstat and it only displays the information regarding IP address connected behinde and from the router. It doesn not actually display the IP address the router is using to connect to the internet.

Not to mention netstat is really SLOW :)

Thanks for the idea.

By the way... So everyone knows, its looking like the only way to solve this problem is using a website of some kind. Having said that, does anyone know if its possible to have a simple HTML document with some JAVA/ VB script running to dispaly the users IP address?

Thanks for your post!

Thinker
05-28-2002, 04:31 PM
The problem is, you need something running on the server, not in
the client browser. This means a web server that can execute
some kind of script. Either PHP, Perl, or VBScript/ASP would be the
top choices.

vbtester
05-29-2002, 03:08 PM
Okay, so lets assume that I am paying for some webspace somewhere, and that they support ASP or JSP.

Would i need explicit administror permission to run a script that would return the IP address?

Or could i simply upload a webpage? Actually im not too sure how this whole architecture works.

I upload a file like dummy.jsp or dummy.asp? Lets say i upload a file called dummy.jsp. How would i actually access that script file from an HTML page, and most importantly, do i need any kind of administrative permission to upload and use dummy.jsp?

Please advise,
Thanks again for your posts :)

ChiefRedBull
05-29-2002, 03:15 PM
Once you have an account on a webserver somewhere, as long as it supports ASP or PHP or allows you to run your own CGI, then all you have to do if FTP your file into your directory. This is done simply via a program like CuteFTP or AceFTP. Place it in the root directory, and call it "index.html". Then when you navigate to the webserver URL (you'll be told what your URL is when you sign up), the page will appear.

vbtester
05-30-2002, 08:58 AM
Thanks for your post.

I am familiar with using FTP, and HTML. One thing is a little confusing. If I program my IP detection script using JSP it will be saved as filename.jsp, likewise if i program in ASP it will be saved as filename.asp.

Having said that, would I not need to upload 2 files? index.html and filename.jsp / .asp ???

Or, do I simply write the ASP/JSP code inside of index.html
If i cannot do this, and I have to upload 2 files, then how do I link my ASP/JSP code so that their results are displayed when the user visists index.html

Please advise. ;)

ChiefRedBull
05-30-2002, 11:39 AM
Well, every time you access a folder URL on the web (http://www.server.com, or http://www.server.com/folder for example) the webserver finds the default file for that folder, and sends it back. It's up to your host as to what that file is, but it can be anything you want, so if you ask them nicely, they may well change it for you, or allow you access to the necessary files so you can change it yourself.

vbtester
05-30-2002, 06:04 PM
so, if i understand what your saying.... I dont need index.html?
I can just pass back the .jsp or .asp file to my VB application?

If so, how is it possible that this file will contain the IP address? I need to get a website to somehow pass back a file, or to dispaly a webpage which can be parsed for the IP address.

Please let me know how this should be done.

ChiefRedBull
05-31-2002, 05:44 AM
I think you've got confused somewhere along the way. Lemme try and explain this again.

All you need is a hosting account somewhere on the web. Some free hosts support CGI, some don't. You'll have to look around for one that does. If you can't find any that suit your needs, you'll have to pay for something. Many hosts that require payment also require you to have a domain name.
Then, you sign up for an account (paying if necessary). This allocates you space on the server, to which you can upload any file you want (not porn or warez though probably) via and FTP client. Some freebies have built in web uploaders and don't allow FTP though.
Now, you have serveral options in how you write your script. It can either be a CGI (Written in perl most likely), or PHP, or ASP using VBS. Whichever you choose is up to you, and up to whatever your host supports.
Get help with writing the script in the Web Programming board.
Once you've written the script you need to upload it to your host. How you do this depends on the host. Then, you can access it through a browser, using the URL that you were given when you igned up.

vbtester
05-31-2002, 01:52 PM
Thank-you :)

Okay I have understood everything you said perfectly. However one question remains :(

How does this data, (from the SCRIPT) enter a users computer? For example, I have done all that you said, and I have programmed the SCRIPT. It has been saved and uploaded as script.jsp.

That script determines a persons IP address and displays it for them. Now the BIG question is how does this IP address get into my VB app.

When a .jsp script is ran, is it ran standalone? IE. Does it generate the .HTML file which contans the results of the script? If so, I would need to download the results page (.html file) into my VB program so that I could parse it for an IP address.

Does this sound right to you? If the script.jsp file actually generates the HTML file (which contans the IP address) then I know understand perfectly how to do this.

Please advise :)

ChiefRedBull
06-01-2002, 02:14 AM
Well, try viewing your page in a browser. Then click on View-->Source. See what happens.

Isn't jsp a Java Server Page? A small java applet that is converted to HTML on the server side? Why are you using Java? If you mean JavaScript, then that is written with an HTML file, but won't be of any use anyway, seeing as it won't do what you want. Java is a bit overkill, i would haev thought a simple two or three line Perl CGI would've been fine.

vbtester
06-01-2002, 09:37 AM
Yes sorry about the confusion :)

You are 100% correct. I need to use CGI. My problem is i cant get the CGI to work, im getting "Internal Server Error"

I have a file called test.cgi. Permissions have been set to 755, and here is the code:

#!/usr/local/bin/perl

print $ENV{"REMOTE_ADDR"}

But it dont work :(

All i need is some CGI to return a webpage containing the IP address. I dont even need the <HTML> tags, just the IP address. Do you know the CGI syntax and commands needed to do this? I was able to do this using ASP, but LINUX servers dont run ASP :(

Thanks again for your post!

ChiefRedBull
06-01-2002, 09:43 AM
A few things - you are missing a ";" at the end of the print line. This is necessary. You also have to make sure that your server is set up to do this properly.. Firstly, you might not be allowed to run CGI, secondly, the path to the perl interpreter might be different to the one you are using, thirdly, you need to use the CGI module really (I think this is madatory, I'm not really sure) because you should always print out a proper HTTP header before anything else to make sure the client browser parses it properly. Also, if you use the Carp module, then the server will output useful error messages instead of just the dreaded 500.

You can probably get a free IP detector script off the web somewhere. Try www.cgiarchive.com

This should now be in the web programming board.

vbtester
06-01-2002, 10:41 AM
You are correct. This is not longer a VB topic. I will post the final working answer here when i figure it out... just so all who have helped can see how to actually do this :)

Thanks again!

SteveMosley
06-21-2003, 04:46 AM
Hi,
This is my first submission, the question being asked was very similar to something I was working on myself. I can't claim to be the originator of the code (downloaded elsewhere) but it will do the job you require.
Using the windows TRACERT command will respond with your router's WAN IP in the list, This VB bit of code does the same thing so you can pick out your IP from the list (it should be the first external IP in the list)
Give this a try - it should solve your problem.

vbtester
06-21-2003, 09:02 AM
2 problems with your suggestion.

1) There would be no guarentee as to where in the tracert list the WAN IP would be, so it would be almost impossible to Parse.

2) Secondly the tracert command did not show my WAN IP address anywhere in the list.

Thanks for your post anyway.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum