#!/bin/bash
# Output file
SERVER_IP=$(hostname -I | awk '{print $1}')
SERVER_IP_CLEAN=$(echo "$SERVER_IP" | sed 's/\./_/g')
# Set the output file name based on the server's IP address
OUTPUT_FILE="system_check_$SERVER_IP_CLEAN.txt"
# Collect system information
echo "System Check Information" > $OUTPUT_FILE
echo "----------------------------------" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 1. Check CPU / Memory / Disk usage
echo "1. CPU / Memory / Disk Usage:" >> $OUTPUT_FILE
echo "-----------------------------------" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# CPU Usage
echo "CPU Usage:" >> $OUTPUT_FILE
top -bn1 | grep "Cpu(s)" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# CPU
echo "CPU:" >> $OUTPUT_FILE
lscpu >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# Memory Usage
echo "Memory Usage:" >> $OUTPUT_FILE
free -h >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# Disk Usage
echo "Disk Usage:" >> $OUTPUT_FILE
df -h >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 2. Check OS Version
echo "2. Operating System Version:" >> $OUTPUT_FILE
echo "------------------------------------" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
echo "OS Version: $(uname -o)" >> $OUTPUT_FILE
echo "Kernel Version: $(uname -r)" >> $OUTPUT_FILE
echo "OS Distribution Info: $(cat /etc/centos-release)" >> $OUTPUT_FILE
echo "OS Distribution Info: $(cat /etc/os-release)" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 3. Check Installed Software & Versions
echo "3. Installed Software & Versions:" >> $OUTPUT_FILE
echo "---------------------------------------" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# List installed packages and versions using rpm
echo "Installed Software:" >> $OUTPUT_FILE
# rpm -qa >> $OUTPUT_FILE
if command -v dpkg &> /dev/null; then
echo "using dpkg: " >> $OUTPUT_FILE
apt-mark showmanual >> $OUTPUT_FILE
elif command -v dnf &> /dev/null; then
echo "using dnf: " >> $OUTPUT_FILE
dnf history userinstalled >> $OUTPUT_FILE
else
echo "using rpm: " >> $OUTPUT_FILE
rpm -qa >> $OUTPUT_FILE
fi
echo "" >> $OUTPUT_FILE
# 4. Check Inbound Connections (Listening Ports)
echo "4. Inbound Connections (Listening Ports):" >> $OUTPUT_FILE
echo "--------------------------------------------" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# ss -tuln >> $OUTPUT_FILE
sudo netstat -tuln | grep LISTEN >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 5. Check Outbound Connections
echo "5. Outbound Connections:" >> $OUTPUT_FILE
echo "----------------------------" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# ss -tnp | grep ESTAB >> $OUTPUT_FILE
sudo netstat -tupn | grep ESTABLISHED >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 6. Check IP Tables
echo "6. Check IP Tables:" >> $OUTPUT_FILE
echo "IP Tables:" >> $OUTPUT_FILE
echo "----------------------------" >> $OUTPUT_FILE
iptables -t nat -L -n -v >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 7. MOUNT INFO
echo "7. MountPoint Info:" >> $OUTPUT_FILE
echo "----------------------------" >> $OUTPUT_FILE
mount | column -t >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 8. HOST FILE
echo "8. Host File:" >> $OUTPUT_FILE
echo "----------------------------" >> $OUTPUT_FILE
cat /etc/hosts >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 9. User and group information
echo "9. User and group information:" >> $OUTPUT_FILE
echo "----------------------------" >> $OUTPUT_FILE
echo "ALL Users:" >> $OUTPUT_FILE
cut -d: -f1 /etc/passwd >> $OUTPUT_FILE
#cat /etc/passwd
echo "" >> $OUTPUT_FILE
echo "Groups:" >> $OUTPUT_FILE
cut -d: -f1 /etc/group >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# 10. Ulimit configuration
echo "10. Ulimit configuration:" >> $OUTPUT_FILE
echo "----------------------------" >> $OUTPUT_FILE
ulimit -a >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# Final message
echo "System check information has been saved to