Jump to content
Dennis445

How can I use nslookup in my app.

Recommended Posts

I folks I am new here.

 

I am looking for help on nslookup within a small Delphi app. What I am looking to accomplish is getting my external IP/Internet (Dynamic) address and having this program send me an email when it changes.

 

I am developing software on Linux and need to be able to connect to this pc when I am away. This app will be running on a windows pc.

 

This is the command line "nslookup myip.opendns.com resolver1.opendns.com" I need to extract the IP address from. 

 

I am not really sure how this can be done, is there a way to do this with out nslookup or a way to parse the information into a string?

 

Thank you in advance.

Edited by Dennis445

Share this post


Link to post

So this is what I found out so far that works. I haven't added any error correction yet.

 

I am just using Indy components.

 

//when the for is created I get the initial ip address and store it in a variable then set the timer to active.

 

procedure TForm1.FormCreate(Sender: TObject);
begin
idhttp := TIdHTTP.Create(nil);
compare := idhttp.get('https://myexternalip.com/raw');   //great site that just displays the ip address so no parsing required.
memo1.text := idhttp.get('https://myexternalip.com/raw');
Timer1.Enabled := true;
end;

 

//on the timer event I compare the current ip address with the stored variable and if it changes I set the new ip address to the stored variable.

 

procedure TForm1.Timer1Timer(Sender: TObject);
var
  ipaddress : string;

begin
 idhttp := TIdHTTP.Create(nil);
 ipaddress := idhttp.get('https://myexternalip.com/raw');
 if ipaddress = compare then
 memo1.lines.Add('No Change!')

 else
   compare := ipaddress;

end;

 

next part is adding error correction and the mail client using Indy comps.

also I will add a setting menu so the user can add and store their information.

 

I hope this helps someone or if someone want to expand on this please share your knowledge.

Share this post


Link to post

You could also use https://myip.dnsomatic.com/  to get the IP, but if this is used for your own purposes it might be better to host it on your own PHP server.

Like this

#php 7.x
<?php
$ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
echo "The user IP Address is - ". $ip;
?>

 

Edited by Rollo62

Share this post


Link to post

Hi Rollo62, once I get it all up and running the way I need it to be I probably will use php.

 

Attila Kovacs, The way I need it to be temporally is to have a small program check what the current IP Address is and if it changes send me an email/text when it happens.

 

On another note I migrated the code to my development pc with Win 11 on it and ran into an Indy problem with OpenSSL, I was able to fix it with installing Win32OpenSSL-0.9.8g_redist.msi.

 

At some point when I have it all sorted out and with error checking and added the email portion of it I will share what I have learned.

  • Like 1

Share this post


Link to post

What is your actual question now?  

 

Maybe you can explain some more on your actual problem ? 

It seems you have to fill in your external ip address somewhere, and because this can change you email it to yourself/somebody? 

Can't you solve things with DDNS ? 

 

Share this post


Link to post

I think I solved my question. I was just trying to be helpful with what I have discovered. I did a lot of searching and there wasn't much information on this.

 

Sorry if this goes out of scope.

Share this post


Link to post
On 11/22/2021 at 3:00 PM, Dennis445 said:

So this is what I found out so far that works. I haven't added any error correction yet.

Nitpicking:

  • You are leaking every TIdHTTP object you create.
  • You are querying an HTTPS URL, but you are not assigning any SSLIOHandler object to the TIdHTTP.IOHandler property.  While TIdHTTP can create a default SSLIOHandler object for you, just note that it only supports TLS 1.0 right now.  So, if you ever need to update your code to handle TLS 1.1 or 1.2, you are going to have to assign the TIdHTTP.IOHandler manually.
  • Your FormCreate() is requesting the URL twice.  You already have the text from the 1st query saved in your compare variable, so just assign that variable as-is to your TMemo, you don't need to query the URL again until the TTimer elapses.
  • Your Timer is logging when the IP does not change, but does not log when it does change, or what the new IP is.

Share this post


Link to post

Remy Lebeau,

 

Nitpicking is great how else will I learn.

 

Thank you for the advice.

 

Will freeing up the idhttp clean that up? or any advice on how to do it better.

 

I corrected the problem with using the url twice in the formcreate.

 

and I did a <= for the timer as well as added the send mail procedure to it.

 

I have to learn how to use the TIdHTTP.IOHandler.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×