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
################
## CONFIG ##
################
### CONFIG BEGIN ###
# Folder to save snaps in
SNAP_FOLDER=~/server_states
# Array with files to snapshot
declare -a CFGBKP=(
"/etc/motd"
"/etc/hosts"
)
# Bash color definitions
GREEN='\033[00;92m'
BLUE='\033[00;94m'
C1='\033[00;97m' # white
C2='\033[00;95m' # light magenta
RST='\033[0m'
### CONFIG END ###
# print color message
function msg() {
echo -e "$BLUE * $GREEN$1$RST"
echo -e "\n$C2 * $C1$1$RST"
}
# colorify diff output
function colorDiff {
awk '/^>/ { print "\033[31m"$0"\033[0m"; } /^</ { print "\033[32m"$0"\033[0m"; } /^[^<>]/ { print $0; }'
# print section
function section() {
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
function HL {
echo -e "$BLUE============================================================================$RST"
# colorify diff output
function colorFilter {
awk '/^>/ { print "\033[31m"$0"\033[0m"; } /^</ { print "\033[32m"$0"\033[0m"; }'
}
# print and format unique processes names
@ -40,7 +52,8 @@ function procList {
# print and format opened TCP/UDP ports and process names
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)
@ -76,14 +89,14 @@ function makeSnap {
function compare {
FOLDER=$SNAP_FOLDER/$(ls ~/server_states/ | tail -n 1)
msg "Comparing with snap $FOLDER"
echo -e "$GREEN\nProcesses diff$RST"; HL; procList | diff - $FOLDER/ps.out | colorDiff
echo -e "$GREEN\nOpened ports diff$RST"; HL; portList | diff - $FOLDER/netstat.out | colorDiff
echo -e "$GREEN\nFirewall rules diff$RST"; HL; firewallRules | diff - $FOLDER/iptables.out | colorDiff
echo -e "$GREEN\nMounts diff$RST"; HL; mount | diff - $FOLDER/mount.out | colorDiff
echo -e "$GREEN\nRouting table diff$RST"; HL; routingTable | diff - $FOLDER/routing.out | colorDiff
for i in "${CFGBKP[@]}"
section "Proccesses diff"; procList | diff - $FOLDER/ps.out | colorFilter
section "Opened ports diff"; portList | diff - $FOLDER/netstat.out | colorFilter
section "Firewall rules diff"; firewallRules | diff - $FOLDER/iptables.out | colorFilter
section "Mounts diff"; mount | diff - $FOLDER/mount.out | colorFilter
section "Routing table diff"; routingTable | diff - $FOLDER/routing.out | colorFilter
for FILE in "${CFGBKP[@]}"
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
}
@ -97,11 +110,10 @@ function nonRootExit {
# Printing help
function printHelp {
echo -e "$GREEN State Snap 0.4$RST"; HL
echo -e "Use: $0 [PARAMETRS]...\n"
printf " $GREEN%-6s$RST %s\n" "snap" "- Make server status snapshot"
printf " $GREEN%-6s$RST %s\n" "diff" "- Make a diff comparison against latest snapshot"
printf " $GREEN%-6s$RST %s\n\n" "clear" "- Delete all previous snapshots"
echo -e "Usage: $0 [command]...\n"
printf " $C1%-6s$RST %s\n" "snap" "- Make server status snapshot"
printf " $C1%-6s$RST %s\n" "diff" "- Make a diff comparison against latest snapshot"
printf " $C1%-6s$RST %s\n\n" "clear" "- Delete all previous snapshots"
exit 0;
}

Loading…
Cancel
Save