Advertisement
CSenshi

check_all_stateless.sh

Feb 19th, 2020
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. TOS=2s
  4. PING_TOS=4
  5.  
  6. BYPASS_DIR='/home/cs168/public'
  7. STARTER_DIR='Starter_files'
  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 freeuni.edu.ge  -- PASS' | tee -a ${TMP}
  28.     timeout ${TOS} dig @8.8.8.8 freeuni.edu.ge >> ${RES_TXT}
  29.     echo 'Should Pass : ' >> ${TMP}
  30.     if grep -q 'server found'   "${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.google.com -- DROP' | tee -a ${TMP}
  42.     timeout ${TOS} dig @8.8.8.8 mail.google.com >> ${RES_TXT}
  43.     LC="$(wc -l ${RES_TXT})"
  44.     echo 'Should Drop : ' >> ${TMP}
  45.     if  [ "${LC}" == "0 ${RES_TXT}" ]; then
  46.         SCORE=$((SCORE+1))
  47.         echo 'True'>> ${TMP}
  48.     else
  49.         echo 'False'>> ${TMP}
  50.     fi
  51.     cat ${RES_TXT} >> ${TMP}
  52.     echo '' >> ${TMP}
  53.  
  54.     # test 3: should drop
  55.     > ${RES_TXT}
  56.     echo 'test 3: dig mail.ru -- DROP' | tee -a ${TMP}
  57.     timeout ${TOS} dig mail.ru >> ${RES_TXT}
  58.     LC="$(wc -l ${RES_TXT})"
  59.     echo 'Should Drop : ' >> ${TMP}
  60.     if  [ "${LC}" == "0 ${RES_TXT}" ]; then
  61.         SCORE=$((SCORE+1))
  62.         echo 'True'>> ${TMP}
  63.     else
  64.         echo 'False'>> ${TMP}
  65.     fi
  66.     cat ${RES_TXT} >> ${TMP}
  67.     echo '' >> ${TMP}
  68.  
  69.     ### ICMP
  70.     # test 4: should drop
  71.     > ${RES_TXT}
  72.     echo 'test 4: ping 8.8.8.8  -- DROP' | tee -a ${TMP}
  73.    
  74.     ping 8.8.8.8 >> ${RES_TXT} &
  75.     PING_PID=$!
  76.     sleep ${PING_TOS}
  77.     sudo kill -2 ${PING_PID}
  78.        
  79.     echo 'Should Drop : ' >> ${TMP}
  80.     if grep -q '0 received' "${RES_TXT}"; then
  81.         SCORE=$((SCORE+1))
  82.         echo 'True'>> ${TMP}
  83.     else
  84.         echo 'False'>> ${TMP}
  85.     fi
  86.     cat ${RES_TXT} >> ${TMP}
  87.     echo '' >> ${TMP}
  88.  
  89.     # test 5: should pass
  90.     > ${RES_TXT}
  91.     echo 'test 5: ping 185.179.191.81  -- PASS' | tee -a ${TMP}
  92.    
  93.     ping 185.179.191.81 >> ${RES_TXT} &
  94.     PING_PID=$!
  95.     sleep ${PING_TOS}
  96.     sudo kill -2 ${PING_PID}
  97.        
  98.     echo 'Should pass : ' >> ${TMP}
  99.     if grep -q '0 received' "${RES_TXT}"; then
  100.         echo 'False'>> ${TMP}
  101.     else
  102.         SCORE=$((SCORE+1))
  103.         echo 'True'>> ${TMP}
  104.     fi
  105.     cat ${RES_TXT} >> ${TMP}
  106.     echo '' >> ${TMP}
  107.  
  108.     # test 6: should pass
  109.     > ${RES_TXT}
  110.     echo 'test 6: ping localhost  -- PASS' | tee -a ${TMP}
  111.    
  112.     ping localhost >> ${RES_TXT} &
  113.     PING_PID=$!
  114.     sleep 2
  115.     sudo kill -2 ${PING_PID}
  116.        
  117.     echo 'Should pass : ' >> ${TMP}
  118.     if grep -q '0 received' "${RES_TXT}"; then
  119.         echo 'False'>> ${TMP}
  120.     else
  121.         SCORE=$((SCORE+1))
  122.         echo 'True'>> ${TMP}
  123.     fi
  124.     cat ${RES_TXT} >> ${TMP}
  125.     echo '' >> ${TMP}
  126.  
  127.     ### TCP
  128.     # test 7: should pass
  129.     > ${RES_TXT}
  130.     echo "test 7: echo 'GET / HTTP/1.1\r\n' | nc 185.179.191.81 80  -- PASS" | tee -a ${TMP}
  131.    
  132.     echo 'GET / HTTP/1.1\r\n' | timeout ${TOS} nc 185.179.191.81 80 >> ${RES_TXT}
  133.        
  134.     LC="$(wc -l ${RES_TXT})"
  135.     echo 'Should Pass : ' >> ${TMP}
  136.     if  [ "${LC}" == "0 ${RES_TXT}" ]; then
  137.         echo 'False'>> ${TMP}
  138.     else
  139.         SCORE=$((SCORE+1))
  140.         echo 'True'>> ${TMP}
  141.     fi
  142.     cat ${RES_TXT} >> ${TMP}
  143.     echo '' >> ${TMP}
  144.  
  145.     # test 8: should drop
  146.     > ${RES_TXT}
  147.     echo "test 8: echo 'GET / HTTP/1.1\r\n' | nc 91.189.89.115 443 -- DROP" | tee -a ${TMP}
  148.    
  149.     echo 'GET / HTTP/1.1\r\n' | timeout ${TOS} nc 91.189.89.115 443 >> ${RES_TXT}
  150.        
  151.     LC="$(wc -l ${RES_TXT})"
  152.     echo 'Should Drop : ' >> ${TMP}
  153.     if  [ "${LC}" == "0 ${RES_TXT}" ]; then
  154.         SCORE=$((SCORE+1))
  155.         echo 'True'>> ${TMP}
  156.     else
  157.         echo 'False'>> ${TMP}
  158.     fi
  159.     cat ${RES_TXT} >> ${TMP}
  160.     echo '' >> ${TMP}
  161.  
  162.  
  163.     ### UDP
  164.     # test 9: should drop
  165.     > ${RES_TXT}
  166.     echo 'test 9: dig example.com @199.7.91.13 -- DROP' | tee -a ${TMP}
  167.     timeout ${TOS} dig example.com @199.7.91.13 >> ${RES_TXT}
  168.     LC="$(wc -l ${RES_TXT})"
  169.     echo 'Should Drop : ' >> ${TMP}
  170.     if  [ "${LC}" == "0 ${RES_TXT}" ]; then
  171.         SCORE=$((SCORE+1))
  172.         echo 'True'>> ${TMP}
  173.     else
  174.         echo 'False'>> ${TMP}
  175.     fi
  176.     cat ${RES_TXT} >> ${TMP}
  177.     echo '' >> ${TMP}
  178.    
  179.     # test 10: should pass
  180.     > ${RES_TXT}
  181.     echo 'test 10: dig example.com @192.58.128.30 -- DROP' | tee -a ${TMP}
  182.     timeout ${TOS} dig example.com @192.58.128.30 >> ${RES_TXT}
  183.     LC="$(wc -l ${RES_TXT})"
  184.     echo 'Should Pass : ' >> ${TMP}
  185.     if  [ "${LC}" == "0 ${RES_TXT}" ]; then
  186.         echo 'False'>> ${TMP}
  187.     else
  188.         SCORE=$((SCORE+1))
  189.         echo 'True'>> ${TMP}
  190.     fi
  191.     cat ${RES_TXT} >> ${TMP}
  192.     echo '' >> ${TMP}
  193.    
  194.     # sleep 1
  195.     sudo pkill python
  196. }
  197.  
  198.  
  199. prepare_workspace(){
  200.     TAG=$1
  201.    
  202.     # empty temporary file
  203.     > ${TMP}
  204.    
  205.     # cd to bypass mode dir
  206.     cd ${BYPASS_DIR}
  207.     # launch bypass mode to have git access
  208.     sudo ./main.py --mode bypass > /dev/null &
  209.     # save PID
  210.     EX_ID=$!
  211.     # cd to workspace
  212.     cd -
  213.  
  214.     # checkout to stateless branch
  215.     git checkout tags/${TAG} >> ${TMP} &>> ${TMP}
  216.     # kill bypass mode
  217.     sudo pkill python
  218.  
  219.     # check if exists
  220.     GIT_ERR_MSG="error: pathspec 'tags/${TAG}' did not match any file(s) known to git."
  221.     if grep -q  "${GIT_ERR_MSG}" "${TMP}"; then
  222.         HAS_DONE=0
  223.     fi
  224.    
  225.    
  226. }
  227.  
  228. for DIR in ./*/;     # list directories in the form "/tmp/dirname/"
  229. do
  230.     SCORE=0
  231.    
  232.     # evaluate dir name
  233.     DIR=${DIR%*/}      # remove the trailing "/"
  234.     DIR=${DIR##*/}
  235.     cd ${DIR}
  236.     echo "Testing : ${DIR}"
  237.    
  238.     # Write student info into file
  239.     echo "-----------------------------------" >> ../${LOG_FILE}
  240.     echo -e "Student: ${DIR} \n" >> ../${LOG_FILE}
  241.    
  242.     # copy starter files
  243.     cp ../../${STARTER_DIR}/* .
  244.  
  245.     # create temporary file for loging student's work
  246.     TMP=../tmp.txt
  247.    
  248.     ### STATELESS ###
  249.     HAS_DONE=1
  250.     TAG='stateless'
  251.     prepare_workspace ${TAG}
  252.  
  253.     if [ ${HAS_DONE} -eq 0 ]; then
  254.         echo "${TAG} Homework Not Done..." >> ../${LOG_FILE}
  255.     else
  256.         test_firewall
  257.     fi
  258.    
  259.     # Write temporary file to LOG file
  260.     cat ${TMP} >> ../${LOG_FILE}
  261.  
  262.     # Write Results
  263.     echo '' >> ../${LOG_FILE}
  264.     echo "Score : ${SCORE}/${MAX_SCORE}" >> ../${LOG_FILE}
  265.     echo "Total score:  $(((SCORE*100 /MAX_SCORE )))" >> ../${LOG_FILE}
  266.    
  267.     echo ""
  268.     cd ..
  269. done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement