電訊茶室's Archiver

tomleehk 發表於 2016-5-7 21:54

Script to send WAN IP address to Email box

[i=s] 本帖最後由 tomleehk 於 2016-5-9 21:26 編輯 [/i]

If you hope to send your router's WAN IP address to an email box every time the router is restarted, below is what you need to do:

[color=Blue][b][Approach 1-MailSend package][/b][/color]
1) Use OpenSSH to access default server IP 192.168.1.1 and input the followings to install mailsend package
    opkg update
    opkg install mailsend
2) Add the followings to the end of your /etc/hotplug.d/iface/20-firewall script:[code]
    if [ ! -f /tmp/ipaddress.bak ]
    then
    sleep 5s
    ifstatus wan | grep '"address"' > /tmp/ipaddress.txt
    sleep 5s
    if [ -s /tmp/ipaddress.txt ]
    then
mailsend -to <target_email> -from <source_email> -starttls -port 587 -auth -smtp <smtp server URL> -sub "WAN IP" -msg-body /tmp/ipaddress.txt  +cc +bc -v -user <source_email> -pass <password>
    rm /tmp/ipaddress.bak
    mv /tmp/ipaddress.txt /tmp/ipaddress.bak
    fi
    fi
[/code]Restart router to test and verify the result.


[color=Blue][b][Approach 2-msmtp package][/b][/color]
1) Use OpenSSH to access default server IP 192.168.1.1 and input the followings to install msmtp package
    opkg update
    opkg install msmtp
2) Edit /etc/msmtprc, sample below[code]# Example for a system wide configuration file
host smtp-mail.outlook.com
port 587
auth on
user <email id>
password <email password>

# Construct envelope-from addresses of the form "user@oursite.example".
auto_from off
#maildomain oursite.example
from <email id>

# Use TLS.
tls on
tls_starttls on
tls_certcheck off

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER.
syslog LOG_MAIL
[/code]3) Add the followings to the end of your /etc/hotplug.d/iface/20-firewall script:[code]    if [ ! -f /tmp/email.bak ]
    then
    sleep 5s
    ifstatus wan | grep '"address"' > /tmp/ipaddress.txt
    sleep 5s
    if [ -s /tmp/ipaddress.txt ]
    then
    string1="Subject:WAN IP"
    string2=$(ifstatus wan | grep '"address"')
    sleep 30s
    string3=$(date +"%Y-%m-%d %T %Z")
    string=$string1$string2" "$string3
    echo -e $string"\n\n" > /tmp/email.txt
    echo -e $string3"\n\n" >> /tmp/email.txt
    echo -e $string2"\n\n" >> /tmp/email.txt
    sendmail <target email> < /tmp/email.txt
    rm /tmp/email.bak
    mv /tmp/email.txt /tmp/email.bak
    rm /tmp/ipaddress.bak
    mv /tmp/ipaddress.txt /tmp/ipaddress.bak
    fi
    fi[/code]Remark : If you need to use a second Email box to send outgoing email in parallel, create an extra configuration file, say  /etc/msmtprc2 and add the below line
    sendmail -C/etc/msmtprc2   <target email2> < /tmp/email.txt
at appropriate position.

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.