Skip to content

Instantly share code, notes, and snippets.

@iamricard
Created March 9, 2013 15:06
Show Gist options
  • Save iamricard/0902caaf36ef0a6650a4 to your computer and use it in GitHub Desktop.
Save iamricard/0902caaf36ef0a6650a4 to your computer and use it in GitHub Desktop.
Client file
#!/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
SENT="successful"
nc -vv $IPserver 8080 < $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
}
checkOK()
{
echo "WAITING FOR OK"
nc -vv -l 8081 > keepgoing
go=`cat keepgoing`
if [ "$go" == "KO" ];then
echo "FILE ERROR."
echo "Abortando conexión."
exit 1
fi
echo "OK RECEIVED"
}
pingfun()
{
echo "PROVIDING SERVER WITH OUR IP"
echo $myIP > myIP
file_send myIP
echo "IP HAS BEEN SERVED"
echo "ping" > pingfile
echo "SENDING PING"
file_send pingfile
echo "PING SENT OK"
echo "WAITING FOR PONG"
nc -vv -l 8081 > pongfile
pongfile=`cat pongfile`
if [ "$pongfile" == "pong" ]; then
echo "PONG RECEIVED OK"
break
else
echo "Couldn't stablish connection. Retrying. Press Ctrl-C to exit."
fi
}
#Displays GNU GPL
echo ""
echo "===================================================================="
echo "||CLIENT.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 ""
#sets up IP address
echo "Which one is your IP?"
/sbin/ifconfig | grep inet | cut -d ' ' -f 12 | cut -d ':' -f 2
read myIP
#asks for the filename
echo "Introduce nombre del archivo: "
read archivo
#checks for the file
if [ ! -e $archivo ]; then
echo "No existe el archivo."
exit 1;
fi
#asks for IP and PORT
echo "IP Server: "
read IPserver
#sends ping
pingfun
#creates variable with original MD5
md5sumfile=`cat $archivo | md5sum | cut -d ' ' -f 1`
echo "$md5sumfile" | cut -d ' ' -f 1 > md5sumfile
#asks for output name and checks size
echo "Introduce el nombre de los ficheros fraccionados (se le sumará \"a\",\"b\"... al final) : "
read output
filesize=$(stat -c%s "$archivo")
#sends md5sum to server
echo "SENDING MD5 SUM"
file_send md5sumfile $IPserver
echo "MD5SUM SENT OK"
#checks whether file is greater than 1024k
if [ $filesize -gt 1048576 ]; then
split $archivo -b 1024k $output
#checks for the total number of files
totalfiles=`ls | grep $output | wc -l`
count=1
echo "$totalfiles" > totalfiles
#sends total number of files
echo "SENDING TOTALFILES"
file_send totalfiles $IPserver
echo "TOTALFILES SENT OK"
LIST=`ls | grep $output`
#starts sending file
for i in $LIST; do
echo "SENDING FILE"
file_send $i $IPserver
echo "FILE $i SENT"
#sends a count
echo "SENDING COUNT"
echo "$count" > count
file_send count $IPserver
echo "Enviando archivo: $count/$totalfiles"
count=$((count + 1))
echo "COUNT SENT OK"
#sends individual md5
md5sumi=`cat $i | md5sum | cut -d ' ' -f 1`
echo $md5sumi > md5sumi
echo "SENDING INDIVIDUAL MD5"
file_send md5sumi $IPserver
echo "INDIVIDUAL MD5 SENT OK"
#checks OK or KO
checkOK
done
else
totalfiles=`ls | $output* | wc -l`
nc $IPserver $PORTserver < totalfiles
sleep $delay
nc $IPserver $PORTserver < $archivo
sleep $delay
echo "Enviando $archivo"
nc $IPserver $PORTserver < count
sleep $delay
nc $IPserver $PORTserver < md5sumfile
sleep $delay
fi
endcommand="FINISH"
echo "$endcommand" > endcommand
file_send endcommand $IPserver
echo "transferencia finalizada, borrando archivos temporales"
rm endcommand count md5sumfile totalfiles md5sumi $output* log keepgoing pingfile pongfile myIP
echo "archivos temporales borrados"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment