LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looking for a way to automatically send current real IP to email (https://www.linuxquestions.org/questions/linux-newbie-8/looking-for-a-way-to-automatically-send-current-real-ip-to-email-461341/)

anakganteng 07-05-2006 08:04 PM

Looking for a way to automatically send current real IP to email
 
I am currently setting up a server on my home machine running on dynamic IP, behind a router. I could use no-ip.com to keep track of the current IP, however that will expose my IP to the internet.

What I intend to do is to make a script that will automatically send me an email to my web-based email (yahoo.com) on daily basis with its current IP.

The problem is, since the machine is behind a router, I am only able to retrieve the NAT translated IP using ifconfig , where what I need is the real IP that the internet sees.

After looking around, I found that I can get the real IP by ssh to somewhere, and do 'echo $SSH_CLIENT'. However to do so automatically, I would probably need to set up a passwordless ssh connection, which I dont think is the proper way to do so.

So to sum up, I need a way for my machine that is behind NAT to automatically send me email containing its current 'real' IP. Please point me to any program that can do this, or a command that can allow me to retrieve the network 'real' IP

Many thanks for those kind enough to read this lengthy post :p

haertig 07-05-2006 08:18 PM

Just send a totally blank email. Your current WAN IP (your "real IP address") will automatically be added to the email header. You don't have to do a thing to determine it, nor add it to the email manually.

When you receive the email, choose your email client's "View Message Source" (or equivalent) function. Check out the "Received: " headers. The IP address you want will be in the FURTHEST DOWN "Received" header, since as the email transverses various servers, new "Received" lines are prepended to the top of the email. How many total "Received" headers you have will depend on the specific path the email took when being relayed from server to server. The only one you care about is the first header (the furthest down one).

nadroj 07-05-2006 08:24 PM

Quote:

I am currently setting up a server on my home machine...
will this server be used for public services over the internet? ie ftp, http, ssh, etc etc..? if so, then your ip address will be exposed to the internet...everyones IP is.
i also have a dynamic ip address and used to host services, and many times did not know the ip address.. i used dyndns.org to get a freehostname which was linked with my ip address, so all i needed to remember was the hostname. it was automatically updated (through my router) whenever i was reconnected and recieved a new ip address.

your router has a public ip address.. your computer that uses this router does not, it has a private lan ip address. do you want the script to get the ip address of your computer? ie the private one? if so, then i dont see how this would be useful... only the router ip address would be.

your router should have a built in firewall.. so just because your routers IP address is known to the internet doesnt make your network insecure. besides the routers firewall, your server will (or should) have a firewall as well, meaning even more security.

hope this helps.. or let me know if im misunderstanding.

irpstrcr 07-05-2006 10:39 PM

Quick and o so dirty....

PHP Code:

#!/bin/bash
lynx -dump checkip.dyndns.org |grep Current |awk '{print "<pre>" $4 "</pre>\n"}' > /tmp/myip.html
date 
>> /tmp/myip.html
mail 
-"My Home IP Addy" email@addy < /tmp/myip.html 

Not positive on the mail part as I use scp to move the html to one of my outside hosts, tho it should be close enough that you could finger it out if it is wrong.

haertig 07-05-2006 11:08 PM

Below is a test email I just sent to myself. I entered a Subject line of "test email" and nothing else. My real IP address is there by default - I didn't have to manually determine and enter it.

You can see my real IP address in the "Received" line about 1/3 of the way down in the email source listing below. I manually overwrote the IP address with "AAA.BBB.CCC.DDD", but in the actual message my true IP address was listed. You can also see on that same "Received" line my internal IP address is listed as well (not that this is of any use to anyone): 192.168.0.52

The specific "Received" line I'm talking about is this one:
Code:

Received: from cpe-AAA-BBB-CCC-DDD.co.sprintbbd.net ([AAA.BBB.CCC.DDD] helo=[192.168.0.52])
It is the "furthest down" of the two "Received" lines. You can see this "Received" line in context in the full email source listed below:
Code:

From - Wed Jul  5 21:53:03 2006
X-Account-Key: account2
X-UIDL: 1fYkUU4Vn3Nl34c4
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Status:  U
Return-Path: <my_email@earthlink.net>
Received: from pop-tawny.atl.sa.earthlink.net ([207.69.195.67])
by mx-stork.atl.sa.earthlink.net (EarthLink SMTP Server) with ESMTP id 1fYkUU4Vn3Nl34c2
for <my_email@earthlink.net>; Wed, 5 Jul 2006 23:52:40 -0400 (EDT)
Received: from cpe-AAA-BBB-CCC-DDD.co.sprintbbd.net ([AAA.BBB.CCC.DDD] helo=[192.168.0.52])
by pop-tawny.atl.sa.earthlink.net with esmtp (Exim 3.36 #10)
id 1FyKuu-0001v4-00
for my_email@earthlink.net; Wed, 05 Jul 2006 23:52:40 -0400
Message-ID: <44AC891B.1060600@earthlink.net>
Date: Wed, 05 Jul 2006 21:52:59 -0600
From: ME <my_email@earthlink.net>
User-Agent: Debian Thunderbird 1.0.7 (X11/20051017)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: ME <my_email@earthlink.net>
Subject: test email
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-ELNK-Info: spv=0;
X-ELNK-AV: 0
X-ELNK-Info: sbv=0; sbrc=.0; sbf=00; sbw=010;

No need to do anything fancy to determine your IP address. It's there by default in any/every email you send. It can be spoofed, but since you're the one sending the email I doubt you'll go out of your way to spoof yourself!

binary_y2k2 07-05-2006 11:32 PM

If you don't want to look at the message source, or even have to open the massage, just put
Code:

echo "Subject: Your IP is $(lynx -dump checkip.dyndns.org |grep Current |awk '{print $4"}')" > /tmp/myip
in to rc.local and make a cron script with
Code:

sendmail you@yahoo.com < /tmp/myip

haertig 07-05-2006 11:50 PM

Oh! Now I see what you guys are suggesting with that lynx thing!

Another similar alternative (if you like HTML email):
Code:

wget -q -O - checkip.dyndns.org | mail -s "Your Current IP Address" -a "Content-Type: text/html" your_email@your_isp.com

binary_y2k2 07-06-2006 12:03 AM

Yeah, that would work too, maybe better.
I just expanded on the example by irpstrcr

anakganteng 07-06-2006 12:22 PM

SUPERB!!! thanks a lot guys! that really answers my question and more!! learned something today :)

->When you receive the email, choose your email client's "View Message Source" (or equivalent) function. Check out the "Received: " headers. <- ARGH why didnt I think of this before, such a simple solution...

Now I gotta check out whats the lynx all about :D

BTW, yea this server is only for my own use, not for public, and I am looking for my router IP. Sorry I didnt make it clearer...

Again I appreciate your time and for sharing your knowledge.


All times are GMT -5. The time now is 02:39 AM.