Howto send email from bash on DLink DNS-323¶
This can be done only on hacked dns-323 ffp/fun_plug. Many people have problem sending emails from shell scripts using DNS-323, it is possible without installing additional software or writing scritps.
Sending emails right from bash scripts in DNS-323 is quite easy, but it took me some time to make it work.
Software already installed on NAS¶
Sending mails from this device can be done using two programs which are already installed!
Preparation¶
Make sure that you have ffp/funplug installed
Login to your DNS-323 (if there is no shell prompt write 5784468 to get it)
Create two files
/mnt/HD_a2/ffp/start/crontab_update
and/mnt/HD_a2/ffp/bin/do_sth.sh
and make it executable (read whole post)Reboot NAS, or run
/mnt/HD_a2/ffp/start/crontab_update start
You should be done
crontab_update init script for ffp¶
/mnt/HD_a2/ffp/start/crontab_update
chmod +x /mnt/HD_a2/ffp/start/crontab_update
Contents of crontab_update:
#!/ffp/bin/sh
# PROVIDE: crontab_update
# REQUIRE: LOGIN
#
# Email settings are taken from webadmin!
#
. /ffp/etc/ffp.subr
name="crontab_update"
start_cmd="crontab_update_start"
## CHANGES SECTION
# You must CHANGE settings below
SMTP=smtp.mail.com
USER=user@mail.com
SOURCE=user@mail.com
DEST=dest@mail.com
PASS=secret
## END OF CHANGES
crontab_update_start() {
CRONTXT=/tmp/crontab.txt
# start with existing crontab
/bin/crontab -l > $CRONTXT
# add the Rsync job to execute at 2:05 am - also create a logfile
/bin/echo "0 20 * * 1-4 /mnt/HD_a2/ffp/bin/do_sth.sh &" >> $CRONTXT
/bin/echo "15 16 * * 5 /mnt/HD_a2/ffp/bin/do_sth.sh &" >> $CRONTXT
# We are now booting - send test email and enable mail
if [ ! -e /.msmtprc -o ! -e /.muttrc ] ; then
email -m 63 -h $SMTP -p 25 -a 1 -u $USER -w $PASS -s $SOURCE -d $DEST -t
test -e /home/root/.msmtprc && ln -s /.msmtprc /home/root/
test -e /home/root/.muttrc && ln -s /.muttrc /home/root/
fi
# install the new crontab
/bin/crontab $CRONTXT
# clean up tmp crontab
/bin/rm $CRONTXT
}
run_rc_command "$1"
do_sth script¶
Location:
/mnt/HD_a2/ffp/bin/do_sth.sh
Make it executable:
chmod +x /mnt/HD_a2/ffp/bin/do_sth.sh
Contents of: /mnt/HD_a2/ffp/bin/do_sth.sh
#!/bin/sh
#
# Simple script to do something
#
PATH=/mnt/HD_a2/ffp/bin/:$PATH
SRC_PATH='/mnt/HD_a2'
SPACE_REMAINING=`df -h`
RESULT=$?
echo -e "Process ended with result ===> $RESULT\n"\
"Space remaining:\n\n"\
"$SPACE_REMAINING"\
"\n\n"\
"Your DNS-323 (dlink-storage)"\
| mutt -s "Process status ("`date -I`")" dest@mail.com
How this can be improved?¶
Think about cat /tmp/tmp_send.mm | mutt -s dlink-storage_E-Mail_Alert dest@mail.com
How I’ve figured this out?¶
Made strings /sys/crfs/web
and found
email -h %s -p 25 -a 1 -u %s -w %s -s %s -d %s -t
rest was just a
matter of time - to get to know how to use it correct.
Common mistakes¶
Sending email option must be enabled in webclient and tested
Blogger sometimes changes „” to other characters (UTF-8), which Bash doesn’t understand, and whole process after copying files fail - make sure that you have „” and not some other characters in files
If you are using Notepad or other editor without UNIX newline format support, you’re in trouble - Bash also can’t understand this - on Windows try to use Notepad++
Changing email credentials is done by web interface
Additional links¶
Conclusion¶
DNS-323 is very powerful device you can even turn nearly every USB printer into network enabled, it can even change USB scanner into networked scanner which is pretty cool.
Copying¶
I would like to post this article, as a tip on DNS-323 wiki, with link to this post - anyone registered there?