Solved

Programmatically detecting my router's IP address

Posted on 2004-10-04
13
693 Views
Last Modified: 2010-04-05
Hello...
  How can I programmatically obtain my router's IP address? I have 2 computers on this router, and when I do a :

localIP:= TcpClient1.LocalHostAddr;    

(as suggested by someone in an old question of mine), it just returns the local router-assigned IP address - 192.168.x.xxx.

I need to be able to get the main IP address of my router, ie. 24.xxx.xxx.xxx

Thanks
   Shawn
0
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +3
13 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12223635
I have a LinkSys and what I had to do what use an http component to grab the linksys status page, then parse out the router's IP address. It's fairly easy when you use an http component, and you know the local address of your router, and the page to request.
0
 

Author Comment

by:aztec
ID: 12223645
Thanks but I need something more "universal" for my app. Not all my users are going to be using Linksys routers.

Thanks
   Shawn


0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 50 total points
ID: 12224248
one dirty solution - create a PHP file:

<?
  $ip = getenv("REMOTE_ADDR");
  echo $ip;
?>

upload this page somewhere on the web and when you open it with TidHTTP component - you can see you external IP. This is "universal", but the disadvange is that it requires Internet connection...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12224322
How about using TRACERT?  

Not sure whether there is a Delphi component that does that, but if the worst comes to the worst you can Exec the DOS command to a text file, then parse that.  The Router public address is going to be the hop after the internal address.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12225392
It's no use...

Your client connects to your server through the router. For this connection, your client doesn't first go from Router to your ISP then back to your router to finally go to your server. Nope, no way. Why would he? Your client connects directly to your server through the router. That outside IP number never gets involved into this equation.

If you do want your "outside" IP number to appear in the server, you must connect to your server through the outside world. Thus, don't connect to it's local IP number or it's machine name. Set up the router that it will send all incoming network traffic to your server. Then connect your client to the outside (24.xxx.xxx.xxx) IP number to force a connection to your server from the outside.

Compare it like walking from your kitchen to your living room. You could walk through the inside of your house and get there, without ever looking outside. Or you go through the kitchen door to the outside, walk around your house and enter through the front door. It's the latter thing that you're trying to do...
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12225797
If you do

tracert   borland.com

(any domain will do)

this will give you a report of all the hops along the way, including the router's public ip address.  If this report is parsed: you know what the Gateway IP address is (you can get this from the Registry), it's going to be the entry after that.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12225807
Sorry, that should be

tracert   borland.com >c:\some directory\tracert.log

0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12226808
Well, if he uses: "tracert server" or "tracert 10.0.1.20" or "tracert 192.168.1.101" then his tracert log will look like this:
------------------------------------------------
Tracing route to <server> over a maximum of 30 hops

  1   <10 ms   <10 ms   <10 ms  <server>

Trace complete.
------------------------------------------------
With <server> being whatever he provided as servername, of course... And no, it doesn't look useful either.
The problem with the router is that it has two sides. The inside and the outside. As long as you make connections from the inside to the inside, it is like walking in your house. You never get to see anything from the outside.

If you want to see the outside IP number, Ivanov_G just gave a good solution. A simple webpage that will return your IP number to you. If you make it appear as if your client is connecting to your server from the outside, then you'll have to tell the client to connect to the outside IP number, and tell the router to direct all incoming connections to the server that you've set up.

There is NO other option.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12228685
When I type in the following line (verbatim):-

tracert   borland.com

I get a listing which shows quite clearly my gateway public IP address. It is, in fact, the first entry.  It is in square brackets, the content of which is easily parsed, if redirected into a file.

Ivanov_G's solution is fine.  It depends on the mindset of the asker.  If the asker is more at home with DOS and parsing techniques than with PHP, my method will maybe have more appeal.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12228767
One other point.

Doing a tracert on a domain will clearly not work if there are problems with DNS.  In practice, I would use a known external dotted IP address rather than a domain name for reliability reasons.
0
 
LVL 1

Expert Comment

by:tommyliu
ID: 12273756
Dear Shawn,

   I want to know your router is support http admin mode, that means can I use web browser to admin the router ???

Tommy
0
 

Author Comment

by:aztec
ID: 12279016
Thanks Ivanov (sorry for the slow response). I think this will work well. Where do I find the TidHTTP component?

Thanks
   Shawn

0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12284283
It is part from Indy components. If you are with Delphi 7, you will have them integrated. If not - here is the download address:
http://www.indyproject.org/download/Files/Indy9.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dev Express grid collapse 2 47
Error E2158 compiling with Delphi XE10 Seattle 2 124
Delphi...Split view - idea? 1 114
how to resize animated Gif image in delphi ? 1 45
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question