Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's my IP through a router? 1

Status
Not open for further replies.

gcaramia

Programmer
Oct 30, 2003
185
IT
Does exist a component or some code that can tell me my ip when i'm connected to internet through a router?

Thanks
Giovanni Caramia
 
G:

I have some code that does it brute force by doing a get on the http status page of the router.

If no one has a more elegant solution, I will be glad to post the code here.



Regards and HTH,
JGS
 
Wow, I didn't know that was possible, I would love to see and play with that bit of coding!

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
As you wish... I had to "chop" this out of the program so I hope it's clear enough...

Code:
type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    RichEdit1: TRichEdit;
    Button1: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

Var
  Form1 : TForm1;
  PostDataStream : TStringStream;

Procedure TForm1.Button1Click(Sender : TObject);
Var
  IPAddr : String;
  I : Integer;
Begin
  IdHTTP1.Request.Username := 'userid';
  IdHTTP1.Request.Password := 'password';
  IdHTTP1.Request.ProxyUsername := 'userid';
  IdHTTP1.Request.ProxyPassword := 'password';
  try
    PostDataStream := TStringStream.Create('');
    // Put the IE http address here that shows when your router is displaying the
    // internet IP.  Mine is a NetGear WGT624 V2 ... so this is it's addr Note: The NetGear is case sensitive...
    IDHTTP1.Get('[URL unfurl="true"]http://192.168.100.1/RST_st_dhcp.htm',[/URL] PostDataStream);
    // If not caught by exception, we're o.k.
    RichEdit1.Clear;
    RichEdit1.Text := PostDataStream.DataString;
    PostDataStream.Free;
  except
    on E: exception do // On Exception, just flag it
      Begin
        PostDataStream.Free;
      End;
  end;

  If (RichEdit1.Lines.Count > 0) Then
    Begin
      I := 0;
      While (I < RichEdit1.Lines.Count) Do
        Begin
          // Parse lines for line that contains IP
          If (Pos('xxxxx', RichEdit1.Lines.Strings[I]) > 0)    // if beginning str there
             and
             (Pos('yyyyy', RichEdit1.Lines.Strings[I] > 0) Then // if trailing str there
            Begin
              IPAddr := Copy(RichEdit1.Lines.Strings[I], x, y);
              Break;
            End
          Else
            Inc(I);
        End;
       If (I < RichEdit1.Lines.Count) Then
         Begin
           // Do something with IPAddr
         End
       Else
         Begin
           // Not Found in html
         End;  
     End;  
End;

:
:

Regards and HTH,
JGS
 
Thanks sijgs.

I solved using public resolver.
For everyone that need it, here is all the unit i wrote.
(Put on a form: Button - edit - Indy Clients HTTP)
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, StdCtrls, strutils;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    IdHTTP1: TIdHTTP;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var dtstream : tstringstream;
    s : string;
    p : integer;
begin
  dtstream := tstringstream.create('');
  idhttp1.Get('[URL unfurl="true"]http://www.whatismyip.com',[/URL] dtstream);
  s := dtstream.DataString;
  p := pos('Your ip is',s);
  s := copy(s,p+11,30);
  p := pos(' ',s);
  s := leftstr(s,p);
  edit1.Text := s;
end;

end.

Happy new year
Giovanni Caramia
 
G:

The only problem I see is that "WhatsmyIP" could be down.

Also, if this program runs for any length of time, you may want to do a dtstream.free to stop memory leaks.

Nice job.

JGS
 
JGS

>The only problem I see is that "WhatsmyIP" could be down.

I posted a little piece of real code.
I have a list of ip resolver as whatsmyip, so i first ping the address and try to get; if it does not respond go to the next ...

>Also, if this program runs for any length of time, you may want to do a dtstream.free to stop memory leaks.

Yes of course! Sorry i forgot in my posted code. Thanks.

Good day
Giovanni Caramia
 
I do it like this.

Create a account on no-ip.com
Do a ping or trace or whatever to your own no-ip address

Then u have your public ip address....

No-ip is stable.


KungTure-RX.jpg

//Nordlund
 
Good Morning!

Funny you should ask for this. I have created an app that does this very thing.

I call it Wan Ip Mailer. It sends a request to and parses out the IP address.

It will send an email to one or more addresses with the current WAN IP Address.

I have it go to my ISP mailbox and to my Yahoo mailbox. That way, if I can access a computer, I can get the current address. Then I can use it to VNC into my PC.

The app is free and includes source code.

Leave a message and an email address if you would like me to send it to you.

Cya...Dan'l

Every day is a Great Day!
 
I just did a example using sijgs technique.
This version retrieve the data from D-Link 804V

Add a button and paste this code into it.
Add 4 TEdit's at the same time and name them IPEdit, UsernameEdit, PasswordEdit, ExternalIpEdit

Code:
procedure TForm1.Button1Click(Sender: TObject);

  function GetString(SearchStart, SearchStop, SearchString: String): String;
  var
    selStart, selStop: Integer;
    s: String;
  begin
    Result := '';
    selstart := PosEx(SearchStart, SearchString, 1) + Length(SearchStart);
    selstop := PosEx(SearchStop, SearchString, selstart);

    Result := Copy(SearchString, selstart, selstop-selstart);
  end;

var
  s: String;
begin
  IdHTTP1.Request.Username := UsernameEdit.Text;
  IdHTTP1.Request.Password := PasswordEdit.Text;

  try
    // Get root-page
    s := IDHTTP1.Get('[URL unfurl="true"]http://'+IPEdit.Text+'/device_status.htm');[/URL]
    s := GetString('document.location.href="', '"', s);

    // Get sub-page
    s := IDHTTP1.Get('[URL unfurl="true"]http://'+IPEdit.Text+'/'+s);[/URL]
    s := GetString('WAN IP:', '<BR>', s);

    ExternalIPEdit.Text := s;
  except on e: Exception do
    begin
      ExternalIPEdit.Text := '0.0.0.0';
      MessageDlg(E.Message, mtError, [mbOK], 0);
    end;
  end;

end;

/Cheers...

KungTure-RX.jpg

//Nordlund
 
I was wondering ... since you can get your own remote IP, is there also a way to get your own DNS? Like mine for example is "cc27155-a.assen1.dr.home.nl", is there a way to get this from my own computer? (I know there is a idDNSResolver component but I cant get it to work for me, always get: "Connection refused by peer" for some reason quite beyond me, even why I try it on Google.com)

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
This one was a little bit trickier.
I understand you want to know what DNS your client machine has, not the router DNS.

Sometimes, you have more than one network card, or even a virtual network card. You may even have bound several ip numbers to one network card.

One of the solutions is that you extract your ipnumber (Not the router ip number) from another point like the example above, but in this case you NAT number has to be extracted.

You now know what network card you are using, the next step is to scan the registry to fins out which network card has this ip number, then you can extract the bound DNS server(s).


If you want to extract the router DNS, that's a piece of cake. Use same technique as sijgs and I used. INstead of searching for WAN IP, you has to search for DNS:
(Or whatever else in the router)


//Cheers

KungTure-RX.jpg

//Nordlund
 
Bobbafet, you can retreive your DNS using consolle command 'ipconfig /all'; if you like resolve the name you can 'tracert xxx.xxx.xxx.xxx' where xxx.... is DNS-IP you have retreived from ipconfig.

Cheers
Giovanni Caramia
 
Ok, but how would I get it from the console into my Windows app?

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
You can execute a script.
Example:
1°)
Create a batch file 'myipconf.bat' with:
ipconfig.exe /all >c:\OutputDir\myip.txt

2°)
In your application shellexecute myipconf.bat:

Code:
uses 
ShellApi
..
shellexecute(Handle,'open','c:\xx\myipconf.bat','','',sw_Hide);

3°)
Retreive your information from inside c:\OutputDir\myip.txt


Same for tracert. In this last case you may want to create the batch file from inside your application (because of ip-number you must include) before execute it.

Happy Day
Giovanni Caramia
 
Cool dude, I'm going to check that out as soon as I come home again!

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top