Through my years in marine industrial, knowing the harsh environments where our customers needed some help getting data home to the shipping company – and in that way I developed a Maingateway for a RaspberryPi. But since the inustry have adapted these mini computers, since the case can be almost anything – but also cases for DIN-mount etc.
I did start with downloading a normal debian image for RaspberryPi – and do the standard installation of this image is burning it on to a SD-card
$ apt update && apt dist-upgrade -y && apt install build-essential mlocate net-tools postfix proftpd dialog ntpdate |
This will install out needed packages on to our distribution. Afterwards we can start out RaspberryPi up and login using SSH.
The start Script (/etc/init.d/S99ftp-mail-gateway)
Our most important scripåt is our daemon script for starting the mailgateway – which should look like this:
#!/bin/sh ### BEGIN INIT INFO # Provides: PBJ FTP MAilgateway # Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: PBJ FTP Mailgateway
### END INIT INFO
# Start the ftp-mail gateway….
#
case “$1” in
start)
echo”Starting ftp-mail gateway…”
test-b/dev/mmcblk0p1 && mount/dev/mmcblk0p1/mnt/cf
rsync-ar/mnt/cf/cfg//root/–delete
/usr/bin/ftp-mail &
;;
stop)
echo-n”Stopping ftp-mail gateway…”
#not implemented
exit1
;;
restart|reload)
“$0″stop
“$0″start
;;
*)
echo$”Usage: $0 {start|stop|restart}”
exit1
esac
exit $?
|
The important thing to show – is how we mount the SD-card during the startup, since we do like this:
1 Test to see if there is a SD-Card
2 Then mount it on /mnt/cf
3 rsyncing the /mnt/cf/cfg folder to root
4 Then calling the binaries /usr/bin/ftp-mail
Ftp-Mail (/usr/bin/ftp-mail)
The file that runs it all is the /usr/bin/ftp-mail – which is a bash script and looking liike this.
#!/bin/bash
logfile=/root/log
log()
{
echo`date “+%d-%m-%Y %H:%M:%S:”`”$@”>>$logfile
}
date_cnt=0
cfg=/root/ftp-mail.cfg
log “ftp-mail gateway started”
download=/tmp/download
# Checking download folder
if [ ! -d “$download” ];then
mkdir$download
chmod-R777$download
fi
while true; do
sleep2
sz=`stat-c %s $logfile`
if [ $sz-ge100000 ]; then
mv$logfile$logfile.1
fi
cfgi=0
for cfg in /root/*.cfg; do
[ -e “$cfg” ] || break
letcfgi=cfgi+1
ftpdir=/home/default
ftp_in_dir=/root/ftp_in-$cfgi
smtp=`awk-F” *= *” ‘{if ($1==”smtp”) print $2}’ $cfg`
from=`awk-F” *= *” ‘{if ($1==”from”) print $2}’ $cfg`
to=`awk-F” *= *” ‘{if ($1==”reciever”) print $2}’ $cfg`
testreciever=`awk-F” *= *” ‘{if ($1==”testreciever”) print $2}’ $cfg`
subj=`awk-F” *= *” ‘{if ($1==”subj”) print $2}’ $cfg`
prefix=`awk-F” *= *” ‘{if ($1==”prefix”) print $2}’ $cfg`
ntp=`awk-F” *= *” ‘{if ($1==”ntp”) print $2}’ $cfg`
au=`awk-F” *= *” ‘{if ($1==”au”) print $2}’ $cfg`
ap=`awk-F” *= *” ‘{if ($1==”ap”) print $2}’ $cfg`
rsync -ar /root//mnt/cf/cfg/ –delete –excludetmp
sync
if [ $date_cnt-le0 ]; then
let date_cnt=100
# [ “x$ntp” == “x” ] || rdate -s ${ntp}
[ “x$ntp” == “x” ] || ntpdate -u ${ntp} && ntp_ok=1
test -n “${ntp_ok}” && log “ntp: Time received from $ntp” || log “ntp: Waiting for ntp”
fi
let date_cnt=date_cnt-1
# [ “x$ntp” == “x” ] || test -n “${ntp_ok}” || continue
/usr/bin/ftp-get “$ftp_in_dir” $cfg $cfgi || log “ftp-client: error $?”
exportTZ=UTC
prefix_date=””
[ “x$ntp” == “x” ] || prefix_date=`date +%Y%m%d_`
/usr/bin/ftp-move “$prefix” “$prefix_date”
for f in $ftpdir/* $ftp_in_dir/*
do
[ -e “$f” ] || continue
netstat-t|grepftp|grepESTABLISHED && break
bf=`basename$f`
extension=”${bf##*.}”
if [ “$extension”=”TXT”-o”$extension”=”txt” ];
then
to=$testreciever
fi
log”smtp: Preparing to send: $bf”
rm-rf /root/tmp/attach
mkdir -p /root/tmp/attach
cp -f $f /root/tmp/attach/$bf
makemime -a “Subject: $subj” -c “application/octet-stream” -o /root/tmp/mime /root/tmp/attach/$bf >> $logfile 2>&1 || continue
#add mime body
found=0
boun=`grep boundary /root/tmp/mime |cut-d'”‘ -f2`
rm-f/root/tmp/mime2
while readline
do
if [ “–$boun”=”$line” ]
then
if [ $found-eq0 ]
then
found=1
echo”–$boun”>>/root/tmp/mime2
echo ‘Content-Type: text/plain; charset=iso-8859-1’>>/root/tmp/mime2
echo ”>>/root/tmp/mime2
echo ”>>/root/tmp/mime2
fi
fi
echo $line >> /root/tmp/mime2
done< /root/tmp/mime
[ $found -eq 0 ] && log “mime: internal error: no bound found”
# cat /root/tmp/mime2 |
mailx -s “$subj” -a $f $to >> $logfile 2>&1 || continue
log”$f successfully sent to local postfix, reciever=$to”
rm $f
cp /root/log* /mnt/cfg/
done
done
done
|
Ftp-move (/usr/bin/ftp-move)
is a little script that moving around on some files – for preparing the send with mime
#!/bin/bash
logfile=/root/log
download=/tmp/download
maildir=/root/ftp_in-1
prefix=$1
date=$2
log()
{
echo `date “+%d-%m-%Y %H:%M:%S:”` “$@” >> $logfile
}
for f in $download/*
do
for t in` ls $download`
do
mv $f $maildir/$prefix$date$t
done
done
|
Ftp-get (/usr/bin/ftp-get)
Is the script that collects the data from other devices using a ftp login and download the newest and not collected files only – and add the to the mailqueue – And in that we can also see what have been send already
#!/bin/sh
logfile=/root/log
sended=/root/sended_emails
download=/tmp/download
log()
{
echo`date “+%d-%m-%Y %H:%M:%S:”`”$@”>>$logfile
}
ldir=$1
cfg=/root/ftp-mail.cfg
cfgi=$3
[ -f $cfg ] || exit 10
host=`awk -F” *= *” ‘{if ($1==”ftp_host”) print $2}’ $cfg`
rdir=`awk -F” *= *” ‘{if ($1==”ftp_rdir”) print $2}’ $cfg`
user=`awk -F” *= *” ‘{if ($1==”ftp_user”) print $2}’ $cfg`
pass=`awk -F” *= *” ‘{if ($1==”ftp_pass”) print $2}’ $cfg`
[ -z $host ] && exit
[ -z $ldir ] && exit 20
mkdir -p “$ldir”
echo user $user > /tmp/ftp-$cfgi
echo pass $pass >> /tmp/ftp-$cfgi
echo host $host >> /tmp/ftp-$cfgi
echo rdir $rdir >> /tmp/ftp-$cfgi
rlist=/tmp/ftp_rlist-$cfgi
llist=/tmp/ftp_llist-$cfgi
# test for already sended mails
if [ -f $sended ];then
cat $sended >> $llist
fi
ncftpls -1 -x”l” -f /tmp/ftp-$cfgi “ftp://$host/$rdir” | grep -o ‘[^ ]*$’ > $rlist 2>>$logfile || log “ftp-client: Could not get file list from $host”
# just to make sure eventual current uploading file is complete
sleep 10
# download files not in llist
while read file; do
grep $file $llist > /dev/null && continue
log”ftp-client: Downloading $file”
bfile=`basename$file`
rm -f $ldir/$bfile
cd /tmp/download
ncftpget-v-u$user-p$passftp://$host/$rdir/$file$download/$file>>$logfile2>&1 && echo$file>>$llist && echo$file>>$sended||log”ftp-client: Download failed.”
done < $rlist
# remove files in llist which is no longer in rlist
rm -f $llist
|
Ftp-mail.cfg (/root/ftp-mail.cfg)
The main configuration file is the ftp-mail.cfg in our /root /folder. THis file contains the variables used in this mailgateway
smtp | should only be used while using internal mailserver – not when using Gmail |
reciever | is the default mailreciever – that gets the daily email |
testreciever | is only for testing the system – send a default txt mil |
subj | subject on the sended email |
prefix | If tye attach document should have a prefix |
ntp | time is the most essential inb sending a email |
ftp_host | IP adresse |
ftp_rdir
|
Folder on FTP server to get the new files |
ftp_user
|
FTP username |
ftp_pass
|
FTP Password |
smtp = (Should not be used – when using gmail)
reciever = test@example.com
testreciever = test@example.com
subj = Mailtest fra Mail-dev
prefix =
ntp =
ftp_host = 192.168.0.2
ftp_rdir = data/test
ftp_user = user
ftp_pass = 1234
|