changed output text formating, netowrk port info from ss instead of netstat

master
czoczo 7 years ago
parent 50df94afe5
commit b1756cd3f4
  1. 64
      statesnap.sh

@ -1,36 +1,48 @@
#!/bin/bash #!/bin/bash
################
## CONFIG ##
################ ### CONFIG BEGIN ###
# Folder to save snaps in # Folder to save snaps in
SNAP_FOLDER=~/server_states SNAP_FOLDER=~/server_states
# Array with files to snapshot # Array with files to snapshot
declare -a CFGBKP=( declare -a CFGBKP=(
"/etc/motd"
"/etc/hosts" "/etc/hosts"
) )
# Bash color definitions # Bash color definitions
GREEN='\033[00;92m' C1='\033[00;97m' # white
BLUE='\033[00;94m' C2='\033[00;95m' # light magenta
RST='\033[0m' RST='\033[0m'
### CONFIG END ###
# print color message # print color message
function msg() { function msg() {
echo -e "$BLUE * $GREEN$1$RST" echo -e "\n$C2 * $C1$1$RST"
} }
# colorify diff output # print section
function colorDiff { function section() {
awk '/^>/ { print "\033[31m"$0"\033[0m"; } /^</ { print "\033[32m"$0"\033[0m"; } /^[^<>]/ { print $0; }' declare -i X1 X2 X3 WIDTH
WIDTH=60 # total width
X2=${#1}+2 # length of $foo and 2 whitespaces
X1=(WIDTH-X2)/2 # length of first part
X3=$WIDTH-X1-X2 # length of last part
echo -e "$C1"
for ((i=1;i<=$X1;i++)); do echo -n "+"; done
echo -en " $C2$1$C1 +++ (\033[31m missing $C1/\033[32m new$C1 ) "
for ((i=1;i<=$X3;i++)); do echo -n "+"; done
echo -e "$RST"
} }
# print horizontal line # colorify diff output
function HL { function colorFilter {
echo -e "$BLUE============================================================================$RST" awk '/^>/ { print "\033[31m"$0"\033[0m"; } /^</ { print "\033[32m"$0"\033[0m"; }'
} }
# print and format unique processes names # print and format unique processes names
@ -40,7 +52,8 @@ function procList {
# print and format opened TCP/UDP ports and process names # print and format opened TCP/UDP ports and process names
function portList { function portList {
netstat -lpn | egrep "(tcp.*LISTEN|udp)" | sort | uniq | awk '{split($NF,arr,"/"); print $1 " " $4 " " arr[2]}' #netstat -lpn | egrep "(tcp.*LISTEN|udp)" | sort | uniq | awk '{split($NF,arr,"/"); print $1 " " $4 " " arr[2]}'
ss -lpn | egrep "(tcp.*LISTEN|udp)" | sort | uniq | awk '{split($NF,arr,"\""); print $1 " " $5 " " arr[2]}' | column -t
} }
# print firewall rules (standard + NAT table) # print firewall rules (standard + NAT table)
@ -76,14 +89,14 @@ function makeSnap {
function compare { function compare {
FOLDER=$SNAP_FOLDER/$(ls ~/server_states/ | tail -n 1) FOLDER=$SNAP_FOLDER/$(ls ~/server_states/ | tail -n 1)
msg "Comparing with snap $FOLDER" msg "Comparing with snap $FOLDER"
echo -e "$GREEN\nProcesses diff$RST"; HL; procList | diff - $FOLDER/ps.out | colorDiff section "Proccesses diff"; procList | diff - $FOLDER/ps.out | colorFilter
echo -e "$GREEN\nOpened ports diff$RST"; HL; portList | diff - $FOLDER/netstat.out | colorDiff section "Opened ports diff"; portList | diff - $FOLDER/netstat.out | colorFilter
echo -e "$GREEN\nFirewall rules diff$RST"; HL; firewallRules | diff - $FOLDER/iptables.out | colorDiff section "Firewall rules diff"; firewallRules | diff - $FOLDER/iptables.out | colorFilter
echo -e "$GREEN\nMounts diff$RST"; HL; mount | diff - $FOLDER/mount.out | colorDiff section "Mounts diff"; mount | diff - $FOLDER/mount.out | colorFilter
echo -e "$GREEN\nRouting table diff$RST"; HL; routingTable | diff - $FOLDER/routing.out | colorDiff section "Routing table diff"; routingTable | diff - $FOLDER/routing.out | colorFilter
for i in "${CFGBKP[@]}" for FILE in "${CFGBKP[@]}"
do do
echo -e "$GREEN\n$i config diff$RST"; HL; diff "$i" "$FOLDER/cfg$i" | colorDiff section "$FILE diff"; diff "$FILE" "$FOLDER/cfg$FILE" | colorFilter
done done
} }
@ -97,11 +110,10 @@ function nonRootExit {
# Printing help # Printing help
function printHelp { function printHelp {
echo -e "$GREEN State Snap 0.4$RST"; HL echo -e "Usage: $0 [command]...\n"
echo -e "Use: $0 [PARAMETRS]...\n" printf " $C1%-6s$RST %s\n" "snap" "- Make server status snapshot"
printf " $GREEN%-6s$RST %s\n" "snap" "- Make server status snapshot" printf " $C1%-6s$RST %s\n" "diff" "- Make a diff comparison against latest snapshot"
printf " $GREEN%-6s$RST %s\n" "diff" "- Make a diff comparison against latest snapshot" printf " $C1%-6s$RST %s\n\n" "clear" "- Delete all previous snapshots"
printf " $GREEN%-6s$RST %s\n\n" "clear" "- Delete all previous snapshots"
exit 0; exit 0;
} }

Loading…
Cancel
Save