返回列表 發帖

Script to send WAN IP address to Email box

本帖最後由 tomleehk 於 2016-5-9 21:26 編輯

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:

[Approach 1-MailSend package]
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:
  1.     if [ ! -f /tmp/ipaddress.bak ]
  2.     then
  3.     sleep 5s
  4.     ifstatus wan | grep '"address"' > /tmp/ipaddress.txt
  5.     sleep 5s
  6.     if [ -s /tmp/ipaddress.txt ]
  7.     then
  8. 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>
  9.     rm /tmp/ipaddress.bak
  10.     mv /tmp/ipaddress.txt /tmp/ipaddress.bak
  11.     fi
  12.     fi
複製代碼
Restart router to test and verify the result.


[Approach 2-msmtp package]
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
  1. # Example for a system wide configuration file
  2. host smtp-mail.outlook.com
  3. port 587
  4. auth on
  5. user <email id>
  6. password <email password>

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

  11. # Use TLS.
  12. tls on
  13. tls_starttls on
  14. tls_certcheck off

  15. # Syslog logging with facility LOG_MAIL instead of the default LOG_USER.
  16. syslog LOG_MAIL
複製代碼
3) Add the followings to the end of your /etc/hotplug.d/iface/20-firewall script:
  1.     if [ ! -f /tmp/email.bak ]
  2.     then
  3.     sleep 5s
  4.     ifstatus wan | grep '"address"' > /tmp/ipaddress.txt
  5.     sleep 5s
  6.     if [ -s /tmp/ipaddress.txt ]
  7.     then
  8.     string1="Subject:WAN IP"
  9.     string2=$(ifstatus wan | grep '"address"')
  10.     sleep 30s
  11.     string3=$(date +"%Y-%m-%d %T %Z")
  12.     string=$string1$string2" "$string3
  13.     echo -e $string"\n\n" > /tmp/email.txt
  14.     echo -e $string3"\n\n" >> /tmp/email.txt
  15.     echo -e $string2"\n\n" >> /tmp/email.txt
  16.     sendmail <target email> < /tmp/email.txt
  17.     rm /tmp/email.bak
  18.     mv /tmp/email.txt /tmp/email.bak
  19.     rm /tmp/ipaddress.bak
  20.     mv /tmp/ipaddress.txt /tmp/ipaddress.bak
  21.     fi
  22.     fi
複製代碼
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.

返回列表