Solved

Programmatically detecting my router's IP address

Posted on 2004-10-04
13
672 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
  • 5
  • 2
  • 2
  • +3
13 Comments
 
LVL 7

Expert Comment

by:LRHGuy
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 31

Expert Comment

by:moorhouselondon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 31

Expert Comment

by:moorhouselondon
Comment Utility
Sorry, that should be

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

0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now