Advertisement
CSenshi

check_all_stateful.sh

Feb 21st, 2020
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.92 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. TOS=3s
  4.  
  5. BYPASS_DIR='/home/cs168/public'
  6. STARTER_DIR='Starter_files'
  7. HTTP_LOG='http.log'
  8.  
  9. # full log file
  10. LOG_FILE="Students.log"
  11. > ${LOG_FILE}
  12.  
  13. MAX_SCORE=10
  14.  
  15. test_firewall(){
  16.     SCORE=0
  17.     RES_TXT='test_tmp.txt'
  18.  
  19.     #launch python script
  20.     chmod +x main.py
  21.     sudo ./main.py --mode firewall --rule rules.conf &
  22.     sleep 1
  23.    
  24.     ### DNS
  25.     # test 1 : shoudl pass
  26.     > ${RES_TXT}
  27.     echo 'test 1: dig @8.8.8.8 mail.google.com   -- DENY' | tee -a ${TMP}
  28.     timeout ${TOS} dig @8.8.8.8 mail.google.com  >> ${RES_TXT}
  29.     echo 'Should Deny : ' >> ${TMP}
  30.     if grep -q '169.229.49.130' "${RES_TXT}"; then
  31.         SCORE=$((SCORE+1))
  32.         echo 'True'>> ${TMP}
  33.     else
  34.         echo 'False'>> ${TMP}
  35.     fi
  36.     cat ${RES_TXT} >> ${TMP}
  37.     echo '' >> ${TMP}
  38.  
  39.     # test 2: should drop
  40.     > ${RES_TXT}
  41.     echo 'test 2: dig @8.8.8.8 mail.ru -- DENY' | tee -a ${TMP}
  42.     timeout ${TOS} dig @8.8.8.8 mail.ru >> ${RES_TXT}
  43.     echo 'Should Deny : ' >> ${TMP}
  44.     if grep -q '169.229.49.130' "${RES_TXT}"; then
  45.         SCORE=$((SCORE+1))
  46.         echo 'True'>> ${TMP}
  47.     else
  48.         echo 'False'>> ${TMP}
  49.     fi
  50.     cat ${RES_TXT} >> ${TMP}
  51.     echo '' >> ${TMP}
  52.  
  53.     # test 3: should pass
  54.     > ${RES_TXT}
  55.     echo 'test 2: dig @8.8.8.8 google.com -- PASS' | tee -a ${TMP}
  56.     timeout ${TOS} dig @8.8.8.8 google.com >> ${RES_TXT}
  57.     LC="$(wc -l ${RES_TXT})"
  58.     echo 'Should Pass : ' >> ${TMP}
  59.     if grep -q "169.229.49.130" "${RES_TXT}"; then
  60.         echo 'False'>> ${TMP}
  61.     elif grep -q ";; ANSWER SECTION:" "${RES_TXT}"; then
  62.         SCORE=$((SCORE+1))
  63.         echo 'True'>> ${TMP}
  64.     else
  65.         echo 'False'>> ${TMP}
  66.     fi
  67.     cat ${RES_TXT} >> ${TMP}
  68.     echo '' >> ${TMP}
  69.  
  70.  
  71.     ### TCP
  72.     # test 4: should deny
  73.     > ${RES_TXT}
  74.     echo "test 4: wget 151.101.65.80 -q -O - -- DENY" | tee -a ${TMP}
  75.    
  76.     timeout ${TOS} wget 151.101.65.80 -q -O - >> ${RES_TXT}
  77.     RET=$?
  78.    
  79.     LC="$(wc -l ${RES_TXT})"
  80.     echo 'Should Deny : ' >> ${TMP}
  81.     # timeout err code is 124
  82.     if [ "${RET}" == "124" ]; then
  83.         echo "False: timeout (didn't deny)" >> ${TMP}
  84.     elif  [ "${LC}" == "0 ${RES_TXT}" ]; then
  85.         SCORE=$((SCORE+1))
  86.         echo 'True'>> ${TMP}
  87.     else
  88.         echo 'False'>> ${TMP}
  89.     fi
  90.     cat ${RES_TXT} >> ${TMP}
  91.     echo '' >> ${TMP}
  92.  
  93.     # test 5: should deny
  94.     > ${RES_TXT}
  95.     echo "test 5: wget https://code.jquery.com/jquery-3.3.1.min.js -- DENY" | tee -a ${TMP}
  96.    
  97.     timeout ${TOS} wget https://code.jquery.com/jquery-3.3.1.min.js >> ${RES_TXT} 2>> ${RES_TXT}
  98.     RET=$?
  99.    
  100.     LC="$(wc -l ${RES_TXT})"
  101.     echo 'Should Deny : ' >> ${TMP}
  102.     # timeout err code is 124
  103.     if [ "${RET}" == "124" ]; then
  104.         SCORE=$((SCORE+1))
  105.         echo 'True'>> ${TMP}
  106.     else
  107.         echo 'False'>> ${TMP}
  108.     fi
  109.     cat ${RES_TXT} >> ${TMP}
  110.     echo '' >> ${TMP}
  111.  
  112.     ### HTTP LOG
  113.     # test 6: should log
  114.     > ${RES_TXT}
  115.     echo "test 6: curl http://api.openweathermap.org/data/2.5/weather -- LOG" | tee -a ${TMP}
  116.    
  117.     timeout ${TOS} curl http://api.openweathermap.org/data/2.5/weather >> ${RES_TXT} 2>> ${RES_TXT}
  118.     RET=$?
  119.    
  120.     LC="$(wc -l ${RES_TXT})"
  121.     echo 'Should Log : ' >> ${TMP}
  122.     # timeout err code is 124
  123.     if grep -q "api.openweathermap.org" "${HTTP_LOG}"; then
  124.         SCORE=$((SCORE+1))
  125.         echo 'Log1: True'>> ${TMP}
  126.     else
  127.         echo 'Log1: False'>> ${TMP}
  128.     fi
  129.    
  130.     if grep -q "GET" "${HTTP_LOG}" && grep -q "HTTP/1.1" "${HTTP_LOG}"; then
  131.         SCORE=$((SCORE+1))
  132.         echo 'Log2: True'>> ${TMP}
  133.     else
  134.         echo 'Log2: False'>> ${TMP}
  135.     fi
  136.  
  137.     cat ${RES_TXT} >> ${TMP}
  138.     echo '' >> ${TMP}
  139.    
  140.    
  141.     # sleep 1
  142.     sudo pkill python
  143. }
  144.  
  145.  
  146. prepare_workspace(){
  147.     TAG=$1
  148.    
  149.     # empty temporary file
  150.     > ${TMP}
  151.    
  152.     # cd to bypass mode dir
  153.     cd ${BYPASS_DIR}
  154.     # launch bypass mode to have git access
  155.     sudo ./main.py --mode bypass > /dev/null &
  156.     # save PID
  157.     EX_ID=$!
  158.     # cd to workspace
  159.     cd -
  160.  
  161.     # checkout to stateless branch
  162.     git checkout tags/${TAG} >> ${TMP} &>> ${TMP}
  163.     # kill bypass mode
  164.     sudo pkill python
  165.  
  166.     # check if exists
  167.     GIT_ERR_MSG="error: pathspec 'tags/${TAG}' did not match any file(s) known to git."
  168.     if grep -q  "${GIT_ERR_MSG}" "${TMP}"; then
  169.         HAS_DONE=0
  170.     fi
  171.    
  172.    
  173. }
  174.  
  175. for DIR in ./*/;     # list directories in the form "/tmp/dirname/"
  176. do
  177.     SCORE=0
  178.    
  179.     # evaluate dir name
  180.     DIR=${DIR%*/}      # remove the trailing "/"
  181.     DIR=${DIR##*/}
  182.     cd ${DIR}
  183.     echo "Testing : ${DIR}"
  184.    
  185.     # Write student info into file
  186.     echo "-----------------------------------" >> ../${LOG_FILE}
  187.     echo -e "Student: ${DIR} \n" >> ../${LOG_FILE}
  188.    
  189.     # copy starter files
  190.     cp ../../${STARTER_DIR}/* .
  191.  
  192.     # create temporary file for loging student's work
  193.     TMP=../tmp.txt
  194.    
  195.     ### STATELESS ###
  196.     HAS_DONE=1
  197.     TAG='stateful'
  198.     prepare_workspace ${TAG}
  199.  
  200.     if [ ${HAS_DONE} -eq 0 ]; then
  201.         echo "${TAG} Homework Not Done..." >> ../${LOG_FILE}
  202.     else
  203.         test_firewall
  204.     fi
  205.    
  206.     # Write temporary file to LOG file
  207.     cat ${TMP} >> ../${LOG_FILE}
  208.  
  209.     # Write Results
  210.     echo '' >> ../${LOG_FILE}
  211.     echo "Score : ${SCORE}/${MAX_SCORE}" >> ../${LOG_FILE}
  212.     echo "Total score:  $(((SCORE*100 /MAX_SCORE )))" >> ../${LOG_FILE}
  213.    
  214.     echo ""
  215.     cd ..
  216. done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement