Advertisement
kivaari

Client.sh Aufgabe 2

Dec 6th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.27 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #ip=$1
  4. ip=localhost
  5. port=1337
  6. n=3
  7. test -p fifo || mkfifo fifo
  8. echo -e "0\n0\n0" > clientvariables.txt
  9.  
  10. function myrecv() {
  11.     read input
  12.     status=$(echo $input | cut -d ' ' -f 1)
  13.     c=$(echo $input | cut -d ' ' -f 2)
  14.     expstatus=$(echo $input | cut -d ' ' -f 3)
  15.     if [[ "$status" == "USERACCEPTED" ]]; then
  16.         sed -i "1s/.*/$c/" clientvariables.txt
  17.         if [[ "$expstatus" == "no" ]]; then
  18.                     sed -i "3s/.*/expired/" clientvariables.txt
  19.         else
  20.             sed -i "3s/.*/notexpired/" clientvariables.txt
  21.         fi
  22.     elif [[ "$status" == "PASSWORDACCEPTED" ]]; then
  23.         sed -i "2s/.*/yes/" clientvariables.txt
  24.     else
  25.         echo $input
  26.     fi
  27. }
  28.  
  29. function createpw() {
  30.     pw=$1
  31.     for ((i=1;i<=n;i++)); do
  32.             tmp=$(echo $pw | shasum | cut -d ' ' -f 1)
  33.         pw=$tmp
  34.     done
  35.     mysend "$pw $n"
  36. }
  37.  
  38. function mysend() {
  39.     if [ -z "$1" ]; then
  40.             echo "No parameter passed to send"
  41.     else
  42.             echo $1 | nc -w 1 $ip $port | myrecv
  43.     fi
  44. }
  45.  
  46. while true;
  47. do
  48.     echo "====== Lab: Shell Programming (BS) ======"
  49.     echo -e "\n\n\tr\tRegister\n\tl\tLogin\n\tq\tQuit\n"
  50.     read -p "your choice: " input
  51.  
  52.     if [ $input = "r" ]; then
  53.         clear
  54.         echo "====== Lab: Shell Programming (BS) ======"
  55.         echo -e "\n\n"
  56.         echo "----- R E G I S T E R -----"
  57.         mysend "REGISTER"
  58.         read -p "login: " newlogin
  59.         mysend $newlogin
  60.         read -p "password: " newpassword
  61.         createpw $newpassword
  62.     elif [ $input = "l" ]; then
  63.         clear
  64.         echo "====== Lab: Shell Programming (BS) ======"
  65.             echo -e "\n\n"
  66.             echo "----- L O G I N -----"
  67.         mysend "LOGIN"
  68.         read -p "login: " login
  69.         mysend $login
  70.         pwstatus=$(sed -n 3p clientvariables.txt)
  71.         #read -p "password: " userpassword
  72.         cnt=$(sed -n 1p clientvariables.txt)
  73.         if [[ "$pwstatus" == "expired" ]]; then
  74.             exec 4<&0
  75.             read -u 4 -p "password expired, please enter a new one: " newpassword
  76.             createpw $newpassword
  77.         else
  78.             exec 3<&0
  79.             read -u 3 -p "password: " userpassword
  80.             k=$(expr $n - $cnt)
  81.             for ((i=1;i<=k;i++)); do
  82.                         tmp=$(echo $userpassword | shasum | cut -d ' ' -f 1)
  83.                 userpassword=$tmp
  84.                 done
  85.             mysend $userpassword
  86.  
  87.         fi
  88.         read -p "file name: " filename
  89.         mysend $filename
  90.     elif [ $input = "q" ]; then
  91.         echo "Quitting..."
  92.         exit 0
  93.     else
  94.         echo "Invalid option. Exiting..."
  95.     fi
  96. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement