#!/bin/bash
# fail2ban management script for remote server

SSH_HOST="192.168.1.2"
SSH_PORT="22422"
SSH_USER="junv"

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== Fail2ban Status ===${NC}\n"

# Get overall status
echo -e "${YELLOW}Overall Status:${NC}"
ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} "sudo fail2ban-client status"

echo -e "\n${YELLOW}SSH Jail Detailed Status:${NC}"
ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} "sudo fail2ban-client status sshd"

echo -e "\n${YELLOW}Recent Ban Activity:${NC}"
ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} "sudo tail -20 /var/log/fail2ban.log | grep -E 'Ban|Unban'"

echo -e "\n${YELLOW}Current Banned IPs:${NC}"
ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} "sudo fail2ban-client status sshd | grep 'Banned IP list' | sed 's/.*://'"

echo -e "\n${GREEN}=== iptables Rules (fail2ban) ===${NC}"
ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} "sudo iptables -L -n | grep -A 10 f2b"

echo -e "\n${GREEN}=== Quick Commands ===${NC}"
echo "  Unban IP:    ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'sudo fail2ban-client set sshd unbanip <IP>'"
echo "  Reload:      ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'sudo fail2ban-client reload'"
echo "  Check jail:  ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'sudo fail2ban-client status sshd'"
echo "  View logs:   ssh -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'sudo tail -f /var/log/fail2ban.log'"
