Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johan-bjareholt/2ca77a0a27c40ccab81fc8522d4e3591 to your computer and use it in GitHub Desktop.
Save johan-bjareholt/2ca77a0a27c40ccab81fc8522d4e3591 to your computer and use it in GitHub Desktop.
findlocs.sh
#!/bin/bash
# 1. ss
# 2. Remove header
# 3. Only print ip
# 4. RegEx away port number
IPS=$(ss --tcp | tail -n +2 | awk '{ print $5 }' | sed -e 's/\(.*\):.*/\1/g')
COUNTRIES=""
for IP in $IPS; do
# 1. Geoiplookup
# 2. filter invalid ips and ips that cannot be found
# 3. Select only country code
# 4. Remove comma in country code
COUNTRY=$(geoiplookup $IP | grep -vwE '(can|not)' | awk '{print $4}' | tr ',' ' ')
COUNTRIES="$COUNTRIES $COUNTRY"
done
echo $COUNTRIES | tr ' ' '\n' | sort | uniq -c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment