Translated some comments, added variable for configuring folder with snaps

master
Mateusz Chochół 8 years ago
parent 1288b4bb09
commit 7b179692d9
  1. 24
      statesnap.sh

@ -1,40 +1,51 @@
#!/bin/bash #!/bin/bash
# Definicje kolorków # CONFIG
SNAP_FOLDER="~/server_states"
# Bash color definitions
GREEN='\033[00;92m' GREEN='\033[00;92m'
BLUE='\033[00;94m' BLUE='\033[00;94m'
RST='\033[0m' RST='\033[0m'
# print color message
function msg() { function msg() {
echo -e "$BLUE * $GREEN$1$RST" echo -e "$BLUE * $GREEN$1$RST"
} }
# colorify diff output
function colorDiff { function colorDiff {
awk '/^>/ { print "\033[31m"$0"\033[0m"; } /^</ { print "\033[32m"$0"\033[0m"; } /^[^<>]/ { print $0; }' awk '/^>/ { print "\033[31m"$0"\033[0m"; } /^</ { print "\033[32m"$0"\033[0m"; } /^[^<>]/ { print $0; }'
} }
# print horizontal line
function HL { function HL {
echo -e "$BLUE============================================================================$RST" echo -e "$BLUE============================================================================$RST"
} }
# print and format unique processes names
function procList { function procList {
ps aux | awk '{ print $11 }' | sort | uniq | egrep -v '^\[' ps aux | awk '{ print $11 }' | sort | uniq | egrep -v '^\['
} }
# 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]}'
} }
# print firewall rules (standard + NAT table)
function firewallRules { function firewallRules {
(/sbin/iptables -L -n; echo -e "\n#NAT\n"; /sbin/iptables -L -nt nat) (/sbin/iptables -L -n; echo -e "\n#NAT\n"; /sbin/iptables -L -nt nat)
} }
# print routing table
function routingTable { function routingTable {
/sbin/ip route /sbin/ip route
} }
# make status snap
function makeSnap { function makeSnap {
FOLDER=~/server_states/state_$(date '+%Y%m%d_%H%M%S') FOLDER=$SNAP_FOLDER/state_$(date '+%Y%m%d_%H%M%S')
mkdir -p $FOLDER mkdir -p $FOLDER
procList > $FOLDER/ps.out procList > $FOLDER/ps.out
portList > $FOLDER/netstat.out portList > $FOLDER/netstat.out
@ -45,8 +56,9 @@ function makeSnap {
msg "Written state snapshot to $FOLDER" msg "Written state snapshot to $FOLDER"
} }
# comapring states to previous snap
function compare { function compare {
FOLDER=~/server_states/$(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 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\nOpened ports diff$RST"; HL; portList | diff - $FOLDER/netstat.out | colorDiff
@ -55,6 +67,7 @@ function compare {
echo -e "$GREEN\nRouting table diff$RST"; HL; routingTable | diff - $FOLDER/routing.out | colorDiff echo -e "$GREEN\nRouting table diff$RST"; HL; routingTable | diff - $FOLDER/routing.out | colorDiff
} }
# Checking if root privileges
function nonRootExit { function nonRootExit {
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
msg "This command must be run as root" msg "This command must be run as root"
@ -62,6 +75,7 @@ function nonRootExit {
fi fi
} }
# Printing help
function printHelp { function printHelp {
echo -e "State Snap 0.3"; HL echo -e "State Snap 0.3"; HL
echo -e "Use: $0 [PARAMETRS]...\n" echo -e "Use: $0 [PARAMETRS]...\n"
@ -70,12 +84,12 @@ function printHelp {
exit 0; exit 0;
} }
# przy braku argumentów wyświetl pomoc # Print help if no arguments
if [ $# -lt 1 ]; then if [ $# -lt 1 ]; then
printHelp printHelp
fi fi
# główny kod # Program starts here
case $1 in case $1 in
diff) diff)
nonRootExit nonRootExit

Loading…
Cancel
Save