Skip to content

Instantly share code, notes, and snippets.

@Someguy123
Created May 31, 2021 01:09
Show Gist options
  • Save Someguy123/1fcf779f3162ddde93dc2de89cf95aea to your computer and use it in GitHub Desktop.
Save Someguy123/1fcf779f3162ddde93dc2de89cf95aea to your computer and use it in GitHub Desktop.
IP Extraction + IP Stats Functions for ZSH - intended for parsing bitcoind/litecoind/swiftcashd peer stats on the CLI
#!/usr/bin/env zsh
######################################################################################################
#
# IP Extraction + IP Stats Functions for ZSH
# Written by Someguy123 ( https://github.com/Someguy123 || https://www.privex.io )
# Date: 2021-05-31
# License: X11 / MIT
#
# Usage:
# copy paste the commands at the start, to install the deps needed.
#
# copy paste the functions into a zsh shell session
#
# pipe peer addresses with their ports into the ip_stats function,
# for example:
#
# swiftcash-cli getpeerinfo | jq -r '.[].addr' | ip_stats
#
######################################################################################################
# install python3.7 or newer - 3.8 is available on most modern distros such as ubuntu 18.04+
apt update
apt install -y python3.8 python3.8-dev python3.8-venv
# install geolocate deps for python3.8
sudo -H python3.8 -m pip install -U privex-helpers attrs geoip2 rich colorama
# install privex-utils to get the geolocate util
git clone https://github.com/Privex/utils.git
cd utils
sudo -H ./install.sh
# download the maxmind geoip2 databases
sudo mkdir -p /usr/local/var/GeoIP/
sudo rsync -avch --progress "rsync://se1.files.privex.io:/cdn/extras/GeoIP/*.mmdb" /usr/local/var/GeoIP/
# update the shebang for geolocate to use python3.8
sudo sed -Ei "s#^/usr/bin/env python3$#/usr/bin/env python3.8#" /usr/local/bin/geolocate
extract_v6() {
grep -E '^\[' | sed -rn 's/^\[([a-fA-F0-9\:]+)\].*/\1/p'
}
extract_v4() {
grep -Ev '^\[' | sed -rn 's/([0-9]+.[0-9]+.[0-9]+.[0-9]+):[0-9]+/\1/p'
}
get_country() {
geolocate "$1" | grep "Country:" | awk '{ print $2 }'
}
get_asn() {
geolocate "$1" | grep "ISP:" | awk '{ $1=""; print $0; }'
}
ip_stats() {
ipx="$(cat)"
v4_ips=($(extract_v4 <<< "$ipx"))
v6_ips=($(extract_v6 <<< "$ipx"))
declare -A v4_asns
declare -A v6_asns
declare -A v4_countries
declare -A v6_countries
for ip in "${v4_ips[@]}"; do
if grep -Eq "^(192\.168|10)\."; then continue; fi
_asn="$(get_asn $ip 2> /dev/null)" _country="$(get_country $ip 2> /dev/null)"
if [[ -z "$v4_asns["$_asn"]" ]]; then v4_asns["$_asn"]=0; fi
v4_asns["$_asn"]=$(( v4_asns["$_asn"] + 1 ))
if [[ -z "$v4_countries["$_country"]" ]]; then v4_countries["$_country"]=0; fi
v4_countries["$_country"]=$(( v4_countries["$_country"] + 1 ))
done
for ip in "${v6_ips[@]}"; do
if grep -Eq "^(64\:)|(fe80\:)"; then continue; fi
_asn="$(get_asn $ip 2> /dev/null)" _country="$(get_country $ip 2> /dev/null)"
if [[ -z "$v6_asns["$_asn"]" ]]; then v6_asns["$_asn"]=0; fi
v6_asns["$_asn"]=$(( v6_asns["$_asn"] + 1 ))
if [[ -z "$v6_countries["$_country"]" ]]; then v6_countries["$_country"]=0; fi
v6_countries["$_country"]=$(( v6_countries["$_country"] + 1 ))
done
echo "v4 count: ${#v4_ips}"
echo "v6 count: ${#v6_ips}"
echo "v4 ASNs:"
for a_name a_count in ${(kv)v4_asns}; do
echo " ${a_name} ::\t ${a_count}"
done
echo "v4 Countries:"
for a_name a_count in ${(kv)v4_countries}; do
echo " ${a_name} ::\t ${a_count}"
done
echo "v6 ASNs:"
for a_name a_count in ${(kv)v6_asns}; do
echo " ${a_name} ::\t ${a_count}"
done
echo "v6 Countries:"
for a_name a_count in ${(kv)v6_countries}; do
echo " ${a_name} ::\t ${a_count}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment