betlog

networkPorts

Oct 31st, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.46 KB | None | 0 0
  1. #!/bin/bash
  2. # betlog - 2019-10-31--20-31-42
  3. #
  4. logFile="/home/user/networkPorts.log"
  5. commandBase="netstat --verbose --tcp --udp --protocol=inet,inet6,econet,ddp --program --extend --extend"
  6. #-----------------
  7. autoRefresh=1
  8. clearScreen=1
  9. writeLog=1
  10. #
  11. useRoot=0
  12. rootString=""
  13. # useRoot=0
  14. # rootString="sudo "
  15. #
  16. # useNumeric=1
  17. # numericString=" --numeric"
  18. useNumeric=0
  19. numericString=""
  20. #
  21. useWide=0
  22. wideString=""
  23. # useWide=1    
  24. # wideString=" --wide"
  25. #
  26. useListening=0
  27. listeningString=""
  28. # useListening=1
  29. # listeningString=" --listening"
  30. # listeningString=" --listening --all"
  31. #
  32. # ==============================================
  33. [[ ! -f $(which netstat) ]] && sudo apt install net-tools
  34. userName="$USER"
  35. logStamp="$(date +%Y-%m-%d--%H-%M-%S)"
  36. log="${logFile%/*}/${logStamp}_${logFile##*/}"
  37. f_do () {
  38.     [[ $clearScreen -eq 1 ]] && clear
  39.     stamp="$(date +%Y-%m-%d--%H-%M-%S)"
  40.     commandString=${rootString}${commandBase}${numericString}${wideString}${listeningString}
  41.     echo "-----------------------------"
  42.     echo "$stamp"    
  43.     if [[ $writeLog -eq 1 ]];then
  44.         echo -ne "\\n$stamp\\n" >>"$log"
  45.         $commandString 2> /dev/null | tee --append "$log"
  46.     else
  47.         $commandString 2> /dev/null
  48.     fi
  49.     echo "-----------------------------"
  50.     [[ $writeLog -eq 1 ]] && echo "Logging to: $log" || echo "Logging off"    
  51.     echo "$commandString"    
  52. }
  53. f_do
  54. while :; do
  55.     echo -ne "a)\\tauto\\t\\t[$(echo $autoRefresh|sed 's/1/ON /; s/0/OFF/')]"
  56.     echo -ne "\\nc)\\tclear\\t\\t[$(echo $clearScreen|sed 's/1/ON /; s/0/OFF/')]"
  57.     echo -ne "\\no)\\tlog\\t\\t[$(echo $writeLog|sed 's/1/ON /; s/0/OFF/')]"
  58.     echo -ne "\\nr)\\troot\\t\\t[$(echo $useRoot|sed 's/1/ON /; s/0/OFF/')]"
  59.     echo -ne "\\nn)\\tnumeric\\t\\t[$(echo $useNumeric|sed 's/1/ON /; s/0/OFF/')]"
  60.     echo -ne "\\nl)\\tlistening\\t[$(echo $useListening|sed 's/1/ON /; s/0/OFF/')]"
  61.     echo -ne "\\nw)\\twide\\t\\t[$(echo $useWide|sed 's/1/ON /; s/0/OFF/')]"
  62.     echo -ne "\\n*)\\trefresh"
  63.     echo -ne "\\nesc)\\texit\\n"
  64.     echo "-----------------------------"
  65.     [[ $autoRefresh -eq 1 ]] && cmd="read -s -n 1 -t 1.0 input" || cmd="read -s -n 1 input"
  66.     $cmd
  67.     case $input in
  68.         a)
  69.             autoRefresh=$((1-autoRefresh));
  70.             f_do;
  71.             ;;
  72.         c)
  73.             clearScreen=$((1-clearScreen));
  74.             f_do;
  75.             ;;
  76.         o)
  77.             writeLog=$((1-writeLog));
  78.             f_do;
  79.             ;;
  80.         r)
  81.             useRoot=$((1-useRoot));
  82.             [[ $useRoot -eq 0 ]] && sudo -- || sudo -v;
  83.             [[ $useRoot -eq 0 ]] && userName="$USER" || userName="ROOT";
  84.             [[ $useRoot -eq 1 ]] && rootString="sudo " || rootString="";
  85.             f_do;
  86.             ;;
  87.         n)
  88.             useNumeric=$((1-useNumeric));
  89.             [[ $useNumeric -eq 1 ]] && numericString=" --numeric" || numericString="";
  90.             f_do;            
  91.             ;;
  92.         w)
  93.             useWide=$((1-useWide));
  94.             [[ $useWide -eq 1 ]] && wideString=" --wide" || wideString="";
  95.             f_do;            
  96.             ;;
  97.         l)
  98.             useListening=$((1-useListening));
  99.             [[ $useListening -eq 1 ]] && listeningString=" --listening --all" || listeningString="";
  100. #             [[ $useListening -eq 1 ]] && listeningString=" --listening" || listeningString="";            
  101.             f_do;            
  102.             ;;
  103.         $'\e')
  104.             [[ $useRoot -eq 1 ]] && sudo -- &> /dev/null;
  105.             exit;
  106.             ;;
  107.         *)
  108.             f_do;
  109.             ;;
  110.     esac
  111. done
Advertisement
Add Comment
Please, Sign In to add comment