AnalisiServer
Vai alla navigazione
Vai alla ricerca
Generare un file server.csv:
hostname,ip postgreslab01,192.168.1.220
Verrà usato l'ip per la connessione e il nome per il file di output nella sotto directory: output
Creare un file con lo script del collegamento ai server mediante certificati e rileva le informazioni:
vi check_server_info.sh
#!/bin/bash SERVER_LIST="server.csv" SSH_USER="root" SSH_KEY="$HOME/.ssh/id_rsa" OUTPUT_DIR="output" LOG_FILE="${OUTPUT_DIR}/log.txt" mkdir -p "$OUTPUT_DIR" > "$LOG_FILE" exec 3< "$SERVER_LIST" while IFS=',' read -r name ip <&3; do name=$(echo "$name" | xargs) ip=$(echo "$ip" | xargs) if [[ -z "$name" || -z "$ip" || "$name" == "hostname" ]]; then continue fi echo "Controllo connessione a $name ($ip)..." ssh -i "$SSH_KEY" -o BatchMode=yes -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$SSH_USER@$ip" 'exit' 2>/dev/null if [ $? -ne 0 ]; then echo "❌ Connessione fallita a $ip ($name), salto." echo "$name,$ip,FAILED" >> "$LOG_FILE" continue fi echo "✅ Connessione riuscita a $ip ($name). Raccolgo informazioni..." echo "$name,$ip,SUCCESS" >> "$LOG_FILE" OUTPUT_FILE="${OUTPUT_DIR}/${name}.txt" echo "=== Informazioni per $name ($ip) ===" > "$OUTPUT_FILE" ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$SSH_USER@$ip" bash << 'EOF' | sed '/^$/d' >> "$OUTPUT_FILE" echo "--- CPU Info ---" lscpu | grep 'Model name' echo "--- RAM Totale ---" free -h | grep Mem echo "--- SWAP ---" free -h | grep Swap echo "--- Sistema Operativo ---" cat /etc/os-release | grep PRETTY_NAME echo "--- Ultima Patch Installata ---" if command -v rpm &>/dev/null; then rpm -qa --last | head -n 1 elif command -v dpkg &>/dev/null; then ls -lt /var/lib/dpkg/info/ | head -n 1 else echo "Gestore pacchetti non riconosciuto" fi echo "--- Ultimo Riavvio ---" who -b echo "--- Elenco Dischi ---" lsblk echo "--- Top 5 Processi per Uso CPU ---" ps -eo pid,comm,%cpu --sort=-%cpu | head -n 30 echo "--- Top 5 Processi per Uso RAM ---" ps -eo pid,comm,%mem --sort=-%mem | head -n 30 if [ "$EUID" -eq 0 ]; then echo "Sei root. Mostro porte con informazioni sui processi (con -p)." if command -v ss >/dev/null 2>&1; then ss -tunlp elif command -v netstat >/dev/null 2>&1; then netstat -tunlp else echo "SS e netstat non sono installati." fi else echo "Non sei root. Mostro porte senza informazioni sui processi (senza -p)." if command -v ss >/dev/null 2>&1; then ss -tunl elif command -v netstat >/dev/null 2>&1; then netstat -tunl else echo "SS e netstat non sono installati." fi fi EOF echo "📄 File ${OUTPUT_FILE} creato." done exec 3<&- echo "📝 Log completo: $LOG_FILE"