Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # betlog - 2019-10-31--20-31-42
- #
- logFile="/home/user/networkPorts.log"
- commandBase="netstat --verbose --tcp --udp --protocol=inet,inet6,econet,ddp --program --extend --extend"
- #-----------------
- autoRefresh=1
- clearScreen=1
- writeLog=1
- #
- useRoot=0
- rootString=""
- # useRoot=0
- # rootString="sudo "
- #
- # useNumeric=1
- # numericString=" --numeric"
- useNumeric=0
- numericString=""
- #
- useWide=0
- wideString=""
- # useWide=1
- # wideString=" --wide"
- #
- useListening=0
- listeningString=""
- # useListening=1
- # listeningString=" --listening"
- # listeningString=" --listening --all"
- #
- # ==============================================
- [[ ! -f $(which netstat) ]] && sudo apt install net-tools
- userName="$USER"
- logStamp="$(date +%Y-%m-%d--%H-%M-%S)"
- log="${logFile%/*}/${logStamp}_${logFile##*/}"
- f_do () {
- [[ $clearScreen -eq 1 ]] && clear
- stamp="$(date +%Y-%m-%d--%H-%M-%S)"
- commandString=${rootString}${commandBase}${numericString}${wideString}${listeningString}
- echo "-----------------------------"
- echo "$stamp"
- if [[ $writeLog -eq 1 ]];then
- echo -ne "\\n$stamp\\n" >>"$log"
- $commandString 2> /dev/null | tee --append "$log"
- else
- $commandString 2> /dev/null
- fi
- echo "-----------------------------"
- [[ $writeLog -eq 1 ]] && echo "Logging to: $log" || echo "Logging off"
- echo "$commandString"
- }
- f_do
- while :; do
- echo -ne "a)\\tauto\\t\\t[$(echo $autoRefresh|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\nc)\\tclear\\t\\t[$(echo $clearScreen|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\no)\\tlog\\t\\t[$(echo $writeLog|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\nr)\\troot\\t\\t[$(echo $useRoot|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\nn)\\tnumeric\\t\\t[$(echo $useNumeric|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\nl)\\tlistening\\t[$(echo $useListening|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\nw)\\twide\\t\\t[$(echo $useWide|sed 's/1/ON /; s/0/OFF/')]"
- echo -ne "\\n*)\\trefresh"
- echo -ne "\\nesc)\\texit\\n"
- echo "-----------------------------"
- [[ $autoRefresh -eq 1 ]] && cmd="read -s -n 1 -t 1.0 input" || cmd="read -s -n 1 input"
- $cmd
- case $input in
- a)
- autoRefresh=$((1-autoRefresh));
- f_do;
- ;;
- c)
- clearScreen=$((1-clearScreen));
- f_do;
- ;;
- o)
- writeLog=$((1-writeLog));
- f_do;
- ;;
- r)
- useRoot=$((1-useRoot));
- [[ $useRoot -eq 0 ]] && sudo -- || sudo -v;
- [[ $useRoot -eq 0 ]] && userName="$USER" || userName="ROOT";
- [[ $useRoot -eq 1 ]] && rootString="sudo " || rootString="";
- f_do;
- ;;
- n)
- useNumeric=$((1-useNumeric));
- [[ $useNumeric -eq 1 ]] && numericString=" --numeric" || numericString="";
- f_do;
- ;;
- w)
- useWide=$((1-useWide));
- [[ $useWide -eq 1 ]] && wideString=" --wide" || wideString="";
- f_do;
- ;;
- l)
- useListening=$((1-useListening));
- [[ $useListening -eq 1 ]] && listeningString=" --listening --all" || listeningString="";
- # [[ $useListening -eq 1 ]] && listeningString=" --listening" || listeningString="";
- f_do;
- ;;
- $'\e')
- [[ $useRoot -eq 1 ]] && sudo -- &> /dev/null;
- exit;
- ;;
- *)
- f_do;
- ;;
- esac
- done
Advertisement
Add Comment
Please, Sign In to add comment