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:- 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
複製代碼 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- # 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
複製代碼 3) Add the followings to the end of your /etc/hotplug.d/iface/20-firewall script:- 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
複製代碼 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. |