Skip to content

Instantly share code, notes, and snippets.

@bprashanth
Last active February 26, 2025 11:32
Show Gist options
  • Save bprashanth/c26dcd5760065b785c3667e9e18ac8fd to your computer and use it in GitHub Desktop.
Save bprashanth/c26dcd5760065b785c3667e9e18ac8fd to your computer and use it in GitHub Desktop.
Scrape ips from netstat for established ssh connections and reverse lookup the geolocation / auth method
#!/bin/bash
echo -e "\n**Current SSH Connections:**\n"
echo -e "IP Address\t\tAuth Method\t\tCountry"
# Get all active SSH connection IPs
netstat -napt | grep "ESTABLISHED.*ssh" | awk '{print $5}' | cut -d':' -f1 | sort -u | while read -r ip; do
# Check if the IP used a password
if sudo grep -E "Accepted password|Accepted publickey|Accepted keyboard-interactive|Accepted gssapi" /var/log/auth.log* | grep -q "$ip"; then
auth_method=$(sudo grep -E "Accepted password|Accepted publickey|Accepted keyboard-interactive|Accepted gssapi" /var/log/auth.log | grep "$ip" | awk '{print $9}')
else
auth_method="Unknown"
fi
# Get geolocation of the IP
country=$(whois "$ip" | grep -i "country" | awk '{print $2}' | head -n 1)
if [[ -z "$country" ]]; then
country="Unknown"
fi
# Print results
echo -e "$ip\t\t$auth_method\t\t$country"
done
echo -e "\n Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment