kivaari icon

Client.sh Aufgabe 2

kivaari | PRO | 12/06/16 12:06:42 PM UTC | 0 ⭐ | 299 👁️ | Never ⏰ | []
Bash |

2.27 KB

|

None

|

0 👍

/

0 👎

#!/bin/bash
 
#ip=$1
ip=localhost
port=1337
n=3
test -p fifo || mkfifo fifo
echo -e "0\n0\n0" > clientvariables.txt
 
function myrecv() {
    read input
    status=$(echo $input | cut -d ' ' -f 1)
    c=$(echo $input | cut -d ' ' -f 2)
    expstatus=$(echo $input | cut -d ' ' -f 3)
    if [[ "$status" == "USERACCEPTED" ]]; then
        sed -i "1s/.*/$c/" clientvariables.txt
        if [[ "$expstatus" == "no" ]]; then
                    sed -i "3s/.*/expired/" clientvariables.txt
        else 
            sed -i "3s/.*/notexpired/" clientvariables.txt
        fi
    elif [[ "$status" == "PASSWORDACCEPTED" ]]; then
        sed -i "2s/.*/yes/" clientvariables.txt
    else
        echo $input
    fi
}
 
function createpw() {
    pw=$1
    for ((i=1;i<=n;i++)); do
            tmp=$(echo $pw | shasum | cut -d ' ' -f 1)
        pw=$tmp
    done
    mysend "$pw $n"
}
 
function mysend() {
    if [ -z "$1" ]; then
            echo "No parameter passed to send"
    else
            echo $1 | nc -w 1 $ip $port | myrecv
    fi
}
 
while true;
do
    echo "====== Lab: Shell Programming (BS) ======"
    echo -e "\n\n\tr\tRegister\n\tl\tLogin\n\tq\tQuit\n"
    read -p "your choice: " input
 
    if [ $input = "r" ]; then
        clear
        echo "====== Lab: Shell Programming (BS) ======"
        echo -e "\n\n"
        echo "----- R E G I S T E R -----"
        mysend "REGISTER"
        read -p "login: " newlogin
        mysend $newlogin
        read -p "password: " newpassword
        createpw $newpassword
    elif [ $input = "l" ]; then
        clear
        echo "====== Lab: Shell Programming (BS) ======"
            echo -e "\n\n"
            echo "----- L O G I N -----"
        mysend "LOGIN"
        read -p "login: " login
        mysend $login
        pwstatus=$(sed -n 3p clientvariables.txt)
        #read -p "password: " userpassword
        cnt=$(sed -n 1p clientvariables.txt)
        if [[ "$pwstatus" == "expired" ]]; then
            exec 4<&0
            read -u 4 -p "password expired, please enter a new one: " newpassword
            createpw $newpassword
        else
            exec 3<&0
            read -u 3 -p "password: " userpassword
            k=$(expr $n - $cnt)
            for ((i=1;i<=k;i++)); do
                        tmp=$(echo $userpassword | shasum | cut -d ' ' -f 1)
                userpassword=$tmp
                done
            mysend $userpassword
 
        fi
        read -p "file name: " filename
        mysend $filename
    elif [ $input = "q" ]; then
        echo "Quitting..."
        exit 0
    else
        echo "Invalid option. Exiting..."
    fi
done

Comments