For cost-saving reasons, home and small-business broadband connections often use ADSL virtual dial-up. When the download speed is around 4 Mbps, ADSL broadband does not feel very different from ordinary fiber in everyday use, but it has one obvious problem: there is no fixed public IP address, and each redial may obtain a new dynamic IP. This makes remote maintenance of servers or network devices inconvenient.
If there is a Linux host inside the LAN, you can have it periodically obtain the router’s current public IP address and send it to a specified mailbox by email.
1. Get the Router’s Current Public IP
You can use curl to query the current outbound IP:
curl ifconfig.me
After execution, it returns the public IP address used by the current router when accessing the Internet. Next, send this IP address to the specified mailbox by email.
Use vim GetIP.sh to write a script:
#!/bin/bash
# Program:
# Send Router IP of Internet to certain email
# History:
# 2014-03-08 cmzsteven First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
curl ifconfig.me | mail -s "Router IP of Internet" changmingzhi@163.com
Save and exit:
:wq
Then add execute permission to the script:
chmod +x /root/GetIP.sh
If the system does not have the mail command, you need to install and configure a mail-sending tool first. On CentOS, you can usually check or install mailx:
which mail
rpm -q mailx
Whether the email can be sent successfully also depends on the local MTA or external SMTP configuration. It is recommended to run the script manually first to confirm that the email can be received.
2. Run It on a Schedule
Add this script to the scheduled tasks in crontab. If you are an administrator, you can edit the system-level scheduled task file:
vim /etc/crontab
The system-level /etc/crontab must include the user that executes the command, for example:
1 */1 * * * root /root/GetIP.sh
The configuration above means it runs once at the 1st minute of every hour.
If you use the current user’s scheduled tasks:
crontab -e
then you do not need to write the root column, for example:
1 */1 * * * /root/GetIP.sh
If you want it to run once every day at 1:01, you can write this in /etc/crontab:
1 1 * * * root /root/GetIP.sh
In your own crontab -e, write it as:
1 1 * * * /root/GetIP.sh
Note that when using /n, you should confirm whether the preceding field is still . For example:
* */2 * * * root /root/GetIP.sh
This rule means “run every minute during every 2-hour interval,” so it will send one email every minute during the matching hours. If you only want it to run once every 2 hours, write it as:
1 */2 * * * root /root/GetIP.sh
The remaining work is to configure virtual servers, port forwarding, or NAT translation on the router, which will not be covered here.
3. Schedule Format
The basic format of crontab is as follows:
f1 f2 f3 f4 f5 program
Where:
f1represents the minute.f2represents the hour.f3represents the day of the month.f4represents the month.f5represents the day of the week.programrepresents the program or command to execute.
Common forms are as follows:
- When
f1is, it meansprogramruns every minute; whenf2is, it means it runs every hour, and so on. - When
f1isa-b, it means it runs from minuteathrough minuteb; whenf2isa-b, it means it runs from hourathrough hourb, and so on. - When
f1is/n, it means it runs everynminutes; whenf2is/n, it means it runs everynhours, and so on. - When
f1isa,b,c, it means it runs at minutesa,b, andc; whenf2isa,b,c, it means it runs at hoursa,b, andc, and so on.
Original URL: http://changmingzhi.blog.163.com/blog/static/29678048201428114710683/
