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? |
you can run traceroute
/sbin/traceroute <IP of a known external host/website> |
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 |
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']); http://demonbane.org/ip.php |
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! |
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 :) |
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.
|
|
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!
|
Quote:
http://www.lawrencegoetz.com/programs/ipinfo/ This one spots me correctly. Cheers, Tink |
telnet into the router maybe... or it may have an html managment piece..
|
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> 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. |
Quote:
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. |
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. |
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 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 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! |
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. |
Quote:
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. |
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 Code:
put /var/www/scripts/dynIP.txt htdocs/dynIP.txt Code:
#!/bin/sh and then added: Code:
ln -s /var/www/scripts/serverpop.sh /etc/cron.hourly/serverpop.sh 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. |