28 Nov 2009

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

  1. Make sure that you have ffp/funplug installed
  2. Login to your DNS-323 (if there is no shell prompt write 5784468 to get it)
  3. 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)
  4. Reboot NAS, or run /mnt/HD_a2/ffp/start/crontab_update start
  5. You should be done

crontab_update init script for ffp

Location: /mnt/HD_a2/ffp/start/crontab_update
Make it executable: 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?

4 comments:

  1. Hi, in your crontab_update you should do this:

    # 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

    Otherwise, if you have the wrong email settings a symlink will be make which will prevent the files from being written to once you have the correct settings. To get around this I had to delete the stale symlinks in /home/root and run it again. Thanks for the great info!

    ReplyDelete
  2. Cool - I was looking for this for quite a while. Little mistake: In "Preparation" point 4:

    Reboot NAS, or run /mnt/HD_a2/ffp/start/crontab_update start

    The 'start' parameter is missing.

    Also you should note that of course the parameters such as "smtp.mail.com", "dest@mail.com" etc. have to be replaced with one's own and for SMTP servers without login, both user and password can be set to "admin". The comment in your script makes it sound as if they are taken automatically from the web interface.

    ReplyDelete
  3. @Anonymous: Thanks, making fix.

    @Chris: Thanks for your comment, currently I don't own a DNS-323 anymore, so I can't check it. But as far as I remember DNS-323 took settings from web interface (file: "/.msmtprc" and "/.muttrc") and send mails using this. I'll write proper comments in script and make bugfixes.

    ReplyDelete
  4. Nice! I'll test this! Would be great to have the DNS-323 to send an email with the IP-address it got when starting!

    ReplyDelete