LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-14-2004, 10:33 PM   #1
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Rep: Reputation: 31
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?
 
Old 07-14-2004, 10:41 PM   #2
ppuru
Senior Member
 
Registered: Mar 2003
Location: Beautiful BC
Distribution: RedHat & clones, Slackware, SuSE, OpenBSD
Posts: 1,791

Rep: Reputation: 50
you can run traceroute

/sbin/traceroute <IP of a known external host/website>
 
Old 07-14-2004, 10:58 PM   #3
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Original Poster
Rep: Reputation: 31
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!?!
 
Old 07-15-2004, 12:33 AM   #4
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
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
 
Old 07-15-2004, 01:41 AM   #5
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Original Poster
Rep: Reputation: 31
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!
 
Old 07-15-2004, 01:48 AM   #6
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Original Poster
Rep: Reputation: 31
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
 
Old 07-15-2004, 01:49 AM   #7
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
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.
 
Old 07-15-2004, 01:51 AM   #8
Joey.Dale
Member
 
Registered: Jun 2003
Location: Tampa, Fl
Distribution: Gentoo, Slackware
Posts: 828

Rep: Reputation: 39
http://dynamic.zoneedit.com/checkip.html

-Joey
 
Old 07-15-2004, 09:51 AM   #9
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Original Poster
Rep: Reputation: 31
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!
 
Old 07-15-2004, 12:42 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 07-15-2004, 02:16 PM   #11
t3___
Member
 
Registered: Sep 2003
Posts: 240

Rep: Reputation: 30
telnet into the router maybe... or it may have an html managment piece..
 
Old 07-20-2004, 11:21 PM   #12
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Original Poster
Rep: Reputation: 31
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.
 
Old 07-20-2004, 11:49 PM   #13
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
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.
 
Old 07-21-2004, 03:21 AM   #14
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Rep: Reputation: 50
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.
 
Old 07-21-2004, 06:47 PM   #15
JohnLocke
Member
 
Registered: Jun 2004
Location: Denver, Colorado
Distribution: Ubuntu
Posts: 240

Original Poster
Rep: Reputation: 31
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache - serving to external hosts from a computer on my internal network Khang Linux - Networking 4 01-24-2005 05:10 PM
IP forwarding issue -- IP address of internal network menonrr Linux - Networking 4 09-14-2004 07:46 AM
SuSe 7.0 not communicating with Network (internal or external) jmnovak Linux - Networking 1 02-27-2002 11:52 AM
Sample Working ipchain that allows http,ftp,ssh for internal and external network munisp Linux - Networking 1 11-09-2001 05:49 PM
accessing box from internal network but not from external JereBear Linux - Networking 12 05-12-2001 01:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration