Skip to content

Instantly share code, notes, and snippets.

@iamricard
Created March 9, 2013 14:21
Show Gist options
  • Save iamricard/c0774a9f81ff106568fa to your computer and use it in GitHub Desktop.
Save iamricard/c0774a9f81ff106568fa to your computer and use it in GitHub Desktop.
Server version
#!/bin/bash
#SERVER FILE
#Copyright 2013 Ricard Solé
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
file_send()
{
while true; do
nc -vv $targetIP 8081 < $1 2>log
log=`cat log`
if echo $log | grep -q "succeeded!" ; then
echo "Connection established."
break
fi
echo "Couldn't stablish connection. Retrying."
done
}
pongfun()
{
echo "WAITING FOR IP DISH"
nc -vv -l 8080 > targetIP
echo "IP HAS BEEN EATEN"
echo "pong" > pongfile
echo "WAITING FOR PING"
nc -vv -l 8080 > pingfile
pingfile=`cat pingfile`
if [ "$pingfile" == "ping" ]; then
echo "PING RECEIVED OK"
echo "ASSIGNING TARGET IP ADDRESS"
targetIP=`cat targetIP`
echo "TARGET IP IS: $targetIP"
echo "IP HAS BEEN DIGESTED"
else
echo "exit 5"
exit 5
fi
if [ "$pingfile" == "ping" ]; then
echo "SENDING PONG"
file_send pongfile
echo "PONG SENT OK"
else
echo "Unwanted connection. Exiting."
exit 4
fi
}
verifymd5()
{
#receive md5sum
nc -vv -l 8080 > md5sumorigin
md5sumorigin+=(`cat md5sumorigin`)
sleep $delayserver
#does md5sum of partial files and compares
md5sum+=(`cat received.part$i | md5sum | cut -d ' ' -f 1`)
if [ ! ${md5sumorigin[$i]} -eq ${md5sum[$i]} ]; then
echo "Error del fichero $i"
exit 1
fi
}
sendOK()
{
echo "SENDING OK"
echo "OK" > OKfile
file_send OKfile
echo "OK SENT"
}
sendKO()
{
echo "SENDING KO"
echo "KO" > KOfile
file_send < KOfile
echo "KO SENT"
}
#Displays GNU GPL
echo ""
echo "===================================================================="
echo "||SERVER.sh Copyright (C) 2013 Ricard Solé ||"
echo "||This program comes with ABSOLUTELY NO WARRANTY; ||"
echo "||This is free software, and you are welcome to redistribute it ||"
echo "||under certain conditions. ||"
echo "===================================================================="
echo ""
#sends pong
pongfun
#receives global md5sum
nc -vv -l 8080 > md5sumreceived
md5sumreceived=`cat md5sumreceived`
sleep $delayserver
i=1
testend=""
#receives total # of files
nc -vv -l 8080 > totalfiles
totalfiles=`cat totalfiles`
sleep $delayserver
while true; do
#receives file
nc -vv -l 8080 > received.part$i
echo "FILE RECEIVE OK"
testend=`cat received.part$i`
if [ "$testend" == "FINISH" ];then
echo "ENDCOMMAND RECEIVED"
rm received.part$i
echo "EXITING"
break
fi
echo "Recibiendo archivo $i/$totalfiles"
sleep $delayserver
#checks sync
nc -vv -l 8080 > checknum
checknumber=`cat checknum`
sleep $delayserver
echo "ASSIGN CHECKNUM OK"
#exits if fails sync
#if [ ! "$checknumber" = "$i" ]; then
# echo "Error de sincronización."
# exit 1
#fi
#receive md5sum
nc -vv -l 8080 >> md5sumorigin
md5sumclient=`cat md5sumorigin | tail -1`
sleep $delayserver
echo "RECEIVE INDIVIDUAL MD5SUM OK"
#does md5sum of partial files and compares
md5sumserver=`cat received.part$i | md5sum | cut -d ' ' -f 1`
if [ ! "$md5sumclient" = "$md5sumserver" ]; then
echo "Error del fichero $i."
echo "Abortando conexión."
sendKO
rm md5sumreceived totalfiles received.part* checknum md5sumorigin
exit 1
fi
echo "MD5SUM COMPARISON OK"
sendOK
echo "i = $i"
i=$((i + 1))
done
echo "Transferencia finalizada."
echo "¿Qué nombre quieres ponerle al archivo?"
read original
#joins parts
cat received.part* > $original
#checks md5sum total
newmd5=`cat $original | md5sum | cut -d ' ' -f 1`
amountreceived=`ls received.part* | wc -l`
if [ $totalfiles == $amountreceived ]; then
if [ $newmd5 == $md5sumreceived ]; then
echo "Transferencia satisfactoria."
else
echo "Los MD5 no concuerdan. Archivo corrupto."
fi
else
echo "Faltan archivos."
fi
rm md5sumreceived totalfiles received.part* checknum md5sumorigin log KOfile OKfile pingfile pinglog pongfile targetIP
echo "archivos temporales borrados"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment