Created
December 23, 2019 01:19
-
-
Save fredgido/729470e7fc972798cb32a20f20469ada to your computer and use it in GitHub Desktop.
file server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "$$ Started process">&2; | |
if [[ $1 != "RUNSERVER" ]] ; then | |
echo "Started client mode" >&2; | |
[ ! $2 -ge 1025 ] && [ ! $2 -ge 1025 ] && echo "Invalid port number, must be 1025 to 65535" && exit 1; | |
[[ ! $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && echo "Invalid ip format" && exit 1; | |
socat TCP4:$1:$2 EXEC:"$0 RUNSERVER $$"; | |
if [ ! $? -eq 0 ] ; then | |
echo "could not connect check host/ip and port and server" | |
fi | |
exit 1; | |
fi | |
echo "$$ Started connection">&2; | |
while true ; do | |
echo "$$ Pick LIST GET PUT" > /proc/$2/fd/1; | |
read i </proc/$2/fd/0; | |
echo "$$ Read $i case" >&2; | |
case ${i^^} in | |
LIST ) | |
echo "LIST" | |
read redi ; | |
echo -e "$redi" | tr "\t" "\n" >&2; | |
;; | |
GET ) | |
echo "GET" | |
echo "$$ Insert filename" >&2; | |
read string </proc/$2/fd/0; | |
echo "$string"; | |
echo "$$ Learned name is $string" >&2; | |
read size ; | |
echo "$$ Learned size is $size bytes" >&2; | |
[ $size -ge 0 ] && head -c $size <&0 >${string##*/}; | |
[ ! $size -ge 0 ] && echo "$$ Invalid file" >&2;; | |
PUT ) | |
echo "PUT" | |
read string </proc/$2/fd/0; | |
echo "$string "; | |
if [ -f $string ]; then | |
stat "$string" --format "%s" ; | |
echo "$$ Sent size is $(stat "$string" --format "%s")" >&2; | |
cat <"$string"; | |
else | |
echo "$$ File does not exist" >&2; | |
echo "-1"; | |
fi;; | |
*) | |
echo "$$ Invalid" >&2; | |
break;; | |
esac | |
done | |
echo "$$ Ending connection">&2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ $1 != "RUNSERVER" ]] ; then | |
echo "Started server mode" >&2; | |
[ ! $1 -ge 1025 ] && [ ! $1 -ge 1025 ] && echo "Invalid port number, must be 1025 to 65535" && exit 1; | |
[ ! -d $2 ] && echo "Directory $2 DOES NOT exists." && exit 1; | |
ScriptPath=$PWD | |
cd $2; | |
socat TCP4-LISTEN:$1,reuseaddr,fork EXEC:"$ScriptPath/$0 RUNSERVER"; | |
exit 1; | |
fi | |
echo "$$ Started connection">&2; | |
while true ; do | |
read i ; | |
echo "$$ Read $i case" >&2; | |
case $i in | |
LIST) | |
ls -a | tr "\n" "\t" ; | |
echo ""; | |
;; | |
GET) | |
read string ; | |
echo "$$ Read name is $string" >&2; | |
if [ -f $string ]; then | |
stat "$string" --format "%s" ; | |
echo "$(stat "$string" --format "%s") is file size sent" >&2; | |
cat <"$string"; | |
else | |
echo "$$ File does not exist" >&2; | |
echo -1; | |
fi;; | |
PUT) | |
read name ; | |
echo "$$ Got name $name" >&2; | |
read size ; | |
echo "$$ Learned size is $size bytes" >&2; | |
[ $size -ge 0 ] && head -c $size <&0 >${name##*/}; | |
[ $size -ge 0 ] && echo "$$ Recieve ended" >&2; | |
[ ! $size -ge 0 ] && echo "$$ File does not exist" >&2;; | |
*) | |
echo "$$ Invalid" >&2; | |
break;; | |
esac | |
done | |
echo "$$ Ending connection">&2; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment