On Linux, you can use a simple shell command to get the IP address of the current dial-up interface and send it to a specified email address. The example below applies to situations where an ADSL dial-up connection creates a ppp0 interface.
NOW_IP=`/sbin/ifconfig ppp0 | grep inet | awk {'print $2'} | cut -f2 -d:`
echo $NOW_IP | mail -s "IP" your@emailaddress
Where:
ifconfig ppp0: view information for theppp0interface;grep inet: filter the line containing the IP address;awk {'print $2'}andcut -f2 -d:: extract the IP address field;mail -s "IP" your@emailaddress: send the result to the specified email address withIPas the email subject.
If your system already uses the newer ip command, you can also get the IPv4 address of ppp0 like this:
NOW_IP=$(ip -4 addr show ppp0 | awk '/inet / {print $2}' | cut -d/ -f1)
echo "$NOW_IP" | mail -s "IP" your@emailaddress
Before using it, make sure the local machine can send email correctly. You can test it first with:
echo "test" | mail -s "mail test" your@emailaddress
If you do not receive the email, first check whether the mail command exists and whether the local mail sending service is configured correctly.
For ADSL dial-up, you can add the commands above to the end of /etc/ppp/ip-up.local so the system runs them automatically each time dialing succeeds:
vi /etc/ppp/ip-up.local
After saving, make sure the script has execute permission:
chmod +x /etc/ppp/ip-up.local
You can also run it on a schedule by adding the script to crontab. For example, to run it every 10 minutes:
*/10 * * * * /path/to/send-ip.sh
For specific settings, see: CentOS 定时将路由器的外网 IP 发送到指定电子邮箱中.
