LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Detecting the external IP address of an internal network: (https://www.linuxquestions.org/questions/linux-newbie-8/detecting-the-external-ip-address-of-an-internal-network-205197/)

JohnLocke 07-14-2004 10:33 PM

Detecting the external IP address of an internal network:
 
I'm running a local 192.168.1.1/24 network behind a cable modem / router / firewall combo. I'd like to find a way from a box on the local side to find out what the external / web IP address for my cable modem / router is.

I've got port forwarding turned on so I can run my linux (MDK 9.2) box as a web server and can connect via ssh, however, the local service only supplies DHCP addresses. I own a domain with a static IP. I'd like to detect my current DHCP address with a cron about once an hour (possibly turn that up depending on traffic) and sftp a file to my domain that contains my current IP address. The step I'm missing is how to detect that IP address from a local network. Is there a command I don't know for this?

ppuru 07-14-2004 10:41 PM

you can run traceroute

/sbin/traceroute <IP of a known external host/website>

JohnLocke 07-14-2004 10:58 PM

Tried that ...
 
This is what I get:
Code:

1  192.168.1.1 (192.168.1.1)  7.598 ms  2.015 ms  1.957 ms
 2  * * *
 3  12.244.26.97 (12.244.26.97)  11.070 ms  12.688 ms  10.977 ms
.......... etc.

Now ... I'm assuming number 2 there /ought/ to be the IP address I'm looking for, but it shows up at stars!?!

Demonbane 07-15-2004 12:33 AM

This may be an overkill but if you have a webhost that supports php this is what you can do:
Code:

printf($_SERVER['REMOTE_ADDR']);
example:
http://demonbane.org/ip.php

JohnLocke 07-15-2004 01:41 AM

Php / Apache ...
 
Now I'll just have to figure out how to
1) run that from a cron job
2) capture the output and ftp it to the static domain
3) configure my apache server to actually /work/ with php

:)

I'm not there yet but it looks like this would work!

JohnLocke 07-15-2004 01:48 AM

Nevermind ... I'm an idiot and/or it's late
 
I got that part up, but it gave me my router's IP address from the local net side ... aka:
192.168.1.1

I'm trying to get the external IP, aka, what the rest of the world types in as an IP address to hit my router (which is then forwarded on my local network to this computer)

But I got the php script up and running at least :)

Demonbane 07-15-2004 01:49 AM

It'll be quite easy to capture the output, however the code is pointless if you run it on the local server, you need to find an external host.

Joey.Dale 07-15-2004 01:51 AM

http://dynamic.zoneedit.com/checkip.html

-Joey

JohnLocke 07-15-2004 09:51 AM

Point taken
 
Thanks for the help! I'll use an external host of some kind (probably my own static domain) and fire off a request for that to check my ip and write its own file!

Tinkster 07-15-2004 12:42 PM

Quote:

Originally posted by Joey.Dale
http://dynamic.zoneedit.com/checkip.html

-Joey

Got MY ip compeletely wrong ... :)

http://www.lawrencegoetz.com/programs/ipinfo/

This one spots me correctly.


Cheers,
Tink

t3___ 07-15-2004 02:16 PM

telnet into the router maybe... or it may have an html managment piece..

JohnLocke 07-20-2004 11:21 PM

Ok, here's a question (sorry I've been gone a while) ... /why/ do I have to run this on a remote host? I might be missing something simple here.

Here's the php code I put together:
Code:

<html>
<style type="text/css"><!--
body {background-color: #000000; color: #ffffff;}
td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
.e {background-color: #ccccff; font-weight: bold; color: black;}
.h {background-color: #9999cc; font-weight: bold; color: black;}
.v {background-color: #cccccc; color: black;}
//--></style>
<title>Con Info</title>
<table border="0" cellpadding="3">
 <tr class="h"><th colspan=2>PERTINANT CONNECTION INFORMATION</th></tr>
 <tr class="h"><th>Variables</th><th>Value</th></tr>
<?php
echo "<td class=\"e\">HTTP Host</td>";
echo "<td class=\"v\">" . $_SERVER["HTTP_HOST"] . "</td>";
echo "</tr><tr>";
echo "<td class=\"e\">User Agent</td>";
echo "<td class=\"v\">" . $_SERVER["HTTP_USER_AGENT"] . "</td>";
echo "</tr><tr>";
echo "<td class=\"e\">Boot Image</td>";
echo "<td class=\"v\">" . $_ENV["BOOT_IMAGE"] . "</td>";
echo "</tr>";
?>
</table>
</body>
</html>

Now, it seems to me that this detects the server's (aka, my local webserver that this code is running on) IP address. No? If so, why can't I fire off a request /from/ my server /to/ my server to run this code and find out what my current HTTP_HOST variable is?

Check it out at:
http://67.164.179.221/test.php
... at least until my IP changes (probably not for a while).

Second question is /how/ do I fire off that request (and at this point, it would be a simple thing to put a script like this on my static IP, but the information that is captured is server information in this case, so I'm not sure why it's not easier to put this on my local dynamic IP and test internally). Is there an example out there or manual or something? I'm not even sure where to start.

Demonbane 07-20-2004 11:49 PM

Quote:

Now, it seems to me that this detects the server's (aka, my local webserver that this code is running on) IP address. No? If so, why can't I fire off a request /from/ my server /to/ my server to run this code and find out what my current HTTP_HOST variable is?
This code won't work properly when you connect to your web server locally because it will return the local ip address which the http request was received. It'll work if you connect to it through the external ip, however the problem is the external ip is the info you're trying to retreive.
If you have a remote web server, just use the one line code I provided in the previous post and use your local server to download it through http.

acummings 07-21-2004 03:21 AM

Hi,

#!/bin/bash
curl -s http://www.whatismyip.com/ | grep '<TITLE>' | sed -e 's/.*TITLE>.*is //' -e 's/ W.*//'

That there bash script works very fine here. I just fire it off from the commandline.

I''m on a LAN box, Fedora Core 1 lan ip 192.168.0.103. Smoothwall Express 2 firewall router box is my gateway. ip dynamically assigned by ISP.

--
For me, the ip that script returns matches the ip that's returned by (web browser shortcut) or address bar thus:

http://checkip.dyndns.org:8245/
--

A clever script I thought. forgot exactly where I got it. I think someone shared it on these forums in other post..

Yes, these forums. Here it is:

The thread id is: 202173

http://www.linuxquestions.org/questi...p.comF+7C+grep

http://www.linuxquestions.org/questi...hreadid=202173

I'd ike to hear what you think of it.

--
Alan.

JohnLocke 07-21-2004 06:47 PM

Nice easy little script there, acummings .. here's what I've done with it:

On my static IP, I put a tiny php file out there called "serverinfo.php"
Code:

<?php
echo $_SERVER["REMOTE_ADDR"];
?>

Now, from the box in my house that has the dynamic IP, I run:
Code:

curl -s http://(mywebsiteonstaticIP)/serverinfo.php
Which gloriously returns my dynamic IP. I wanted something of my own out there so if it stops working (aka, if "what's my ip" goes down, I won't have to rebuild), I can fix it myself and won't be reliant on others.

simple ... now that you've helped me figure out what all this stuff is!

Here's the question, though, and I think demonbane, you were trying to tell me this, but I still don't quite get it.

I also have a script out there called "info.php" that just returns the following:
Code:

<?php
phpinfo(INFO_VARIABLES);
?>

Now, when I look at this, the _SERVER["HTTP_CLIENT_IP"] variable has the correct IP and _SERVER["REMOTE_ADDR"] has an incorrect IP ... in fact, I have no idea what that IP actually is.

However, when I use this curl script, it brings back the correct ip ... the one that was reflected on the HTTP_CLIENT_IP var.

I guess I'm wondering what these variables are actually pulling.

As an example:

I use any web browser and go to my static IP
> http://(mywebsitewithstaticIP)/serverinfo.php
I get back some number that I don't know ... say 67.198.37.58
Now, I do this:
# curl -s http://(mywebsitewithstaticIP)/serverinfo.php
> correct IP address
#

what's going on there? Why does browsing to the php file return one IP for REMOTE_ADDR and using curl returns a different one?

Question 2:

Now I've got this working ... what should I be looking at to create something that will either
a) ftp a file to my static IP containing my dynamic IP that I've just received
b) write to a file on that static IP site (btw, controlled by someone else ... I simply have ftp access)

Another phrasing of that question: is there a way to hard code an ftp transfer of a file? I could make a script that opens ftp at the correct address ... but once ftp is open, I don't know how to use a script to write in password and username or the file transfer "put" process.

........90% there

Thanks for all the help so far!

JohnLocke 07-21-2004 08:27 PM

Ok, I figured out part of the first question:
Answer: Locke is an idiot ...

I was accessing serverinfo.php from my windows box on the web browser while using SSH to access serverinfo.php from my linux box with the curl statement. So REMOTE_ADDR is the right variable and I now understand why.

acummings 07-21-2004 11:43 PM

Quote:

Originally posted by JohnLocke
[ snip ]
Question 2:

Now I've got this working ... what should I be looking at to create something that will either
a) ftp a file to my static IP containing my dynamic IP that I've just received
b) write to a file on that static IP site (btw, controlled by someone else ... I simply have ftp access)

Another phrasing of that question: is there a way to hard code an ftp transfer of a file? I could make a script that opens ftp at the correct address ... but once ftp is open, I don't know how to use a script to write in password and username or the file transfer "put" process.
Perl LWP or thereabouts in Perl can do the likes of that. I'm not advanced enough so as to know how to do what you want though.

ip update client

http://www.google.com/search?hl=en&l...rl&btnG=Search

Don't have to be written in Perl though. I came across some written in C language, for instance.

http://www.novajo.ca/easydns.html

A freely avail one written in Perl. Perhaps look at its source code for howto ideas?

http://www.dnsexit.com/Direct.sv?cmd=ipClients

more of 'em.

Which dyn dns service do you subscribe to? as, if ya haven't, ya might check there too.

I'd put money on it that differences of how it's done exist as far as the updater from one to another for instance no-ip.com versus dyndns
--

I've been wanting a static IP for some time now. But haven't leapt into it, not yet.

I'd be curious to hear your experience. Gotta have/subscribe to one of them dyn dns services, right? Like no-ip.com or dyndns or ?

I get confused when I go to look at all of their web sites. They range from free on up in price. Free could have ads in some way.

Rather than me make all the mistakes -- I'd like to benefit from someone been down that road. Web site helpful on it etc.?

Thanks. Alan.

JohnLocke 07-22-2004 04:08 PM

She is solved and working!

So others can know what I did ... here it is:

First I created a script that I called "serverinfo.php" and uploaded that to my static IP server:
Code:

<?php
echo $_SERVER["REMOTE_ADDR"];
?>

I next created an ftp script of events, called "ftp.scr":
Code:

put /var/www/scripts/dynIP.txt htdocs/dynIP.txt
bye

Lastly, I created a script, "serverpop.sh":
Code:

#!/bin/sh
curl -s http://www.ralinventions.com/serverinfo.php > dynIP.txt
cat ftp.scr | ftp ftp02.powweb.com > /dev/null

(yes, that's my current static IP, and yes, the website is currently a hack job ... give it time, I'm just starting. I needed /something/ to hold the domain! The full site should be up by the end of next month ... a cool preview is at www.ralinventions.com/ral.wmv ... but it's big)

and then added:
Code:

ln -s /var/www/scripts/serverpop.sh /etc/cron.hourly/serverpop.sh
Done! I now have a text file that is uploaded to my server containing my dynamic IP on my local server. Now I can use this as a web storage medium in the least.

Thanks to everyone that was so much help in pointing me to what I needed to learn!

-------------------------------------------------

acummings / Alan ... as to which service I'm subscribed to, I use (as you can see above) PowWeb. Located conveniently at www.powweb.com. They've given us (I'm going to approximate as I don't remember exactly) 1Gb of space, a rediculously large amount of bandwidth to use, about 120 pop email addresses with smtp enabled for another 300 or so. The server can use apache, php, sql, as well as standards now like j and javascript, html/xml, cgi. Unlike the free servers, you're free to do what you like with it (and there's no adds). You can (and we do) have sections up that aren't linked anywhere on your site ... even host multiple sites under the same domain but with separate starting pages.

The only thing I've run into so far that I don't like is they don't use sftp ... which would have made my ftp question trivial. But ftp will work fine for the limited amount of file moving I need to do.

BTW, they give credit for references, so if you look at this one and decide you like it, mention my domain as a reference and we'll get a couple bucks in our account ;)


All times are GMT -5. The time now is 04:49 AM.