- #!/bin/bash
- function chksum (){
- echo -n "$1" | md5sum | awk '{print $1}'
- }
- function rx_data (){
- Filename="$1"
- if [ -f $Filename.b64 ] ; then
- rm $Filename.b64
- fi
- echo > /dev/ttyUSB0
- (sleep 1 && echo "/bin/sh /xfer.sh $Filename" > /dev/ttyUSB0) &
- read MD5Sum
- MD5Sum=$(echo $MD5Sum | tr -d '\r')
- echo "Expected sum is $MD5Sum"
- (sleep 1 && echo > /dev/ttyUSB0) &
- while read x ; do
- x=$(echo $x | tr -d '\r')
- if [[ "$x" == "EOF" ]] ; then
- break
- fi
- Payload=$(echo "$x" | awk -F: '{print $1}')
- SumActual=$(echo "$x" | awk -F: '{print $2}' )
- Sum=$(chksum "$Payload")
- if [[ "$Sum" == "$SumActual" ]] ; then
- # Valid data, write it somewhere
- echo $Payload >> $Filename.b64
- # Ack data
- echo $Sum > /dev/ttyUSB0
- echo -n "."
- else
- echo "FAIL" > /dev/ttyUSB0
- echo -n 'x'
- fi
- done < /dev/ttyUSB0
- cat $Filename.b64 | base64 -d > $Filename
- rm $Filename.b64
- ActualSum=$(md5sum $Filename | awk '{print $1}')
- if [[ "$ActualSum" == "$MD5Sum" ]] ; then
- return 0
- else
- return 1
- fi
- }
- File="$1"
- if rx_data "$File" ; then
- echo "File transferred OK!"
- else
- echo "File appears corrupt."
- echo "Retry?"
- fi
recv.sh
From Lousy Meerkat, 5 Years ago, written in Plain Text, viewed 254 times.
URL https://paste.steamr.com/view/f02c109c
Embed
Download Paste or View Raw
— Expand Paste to full width of browser