Skip to content

Instantly share code, notes, and snippets.

@millerjp
Last active March 11, 2016 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millerjp/60aaabb1d0eac023f80b to your computer and use it in GitHub Desktop.
Save millerjp/60aaabb1d0eac023f80b to your computer and use it in GitHub Desktop.
Workout and count which hosts are connecting on the cassandra native port
#!/bin/bash
# Scipt to count connected hosts on port, preform nslookup of hosts and output details
#
HOSTNAME=`hostname`
PORT=$1
IP_HOSTS_COUNT=`netstat -tn | 'next unless(/^tcp\s+\d+\s+\d+\s+\S+?:${PORT}\s+(\S+)?:/); print "$1\n";' | sort | uniq -c | awk '{print "tohost\t",$1,"\t"$2,"\tfromhost\n"}'`
while read -r line; do
toline="${line/tohost/$HOSTNAME}"
from_ip=`echo ${toline} | awk '{print $3}'`
from_host=`nslookup ${from_ip} | grep 'name =' | awk '{print $4}'`
if [ -z "$from_host" ]; then
from_host="unknown"
fi
fromline="${line/fromhost/$from_host}"
echo $fromline
done <<< "$IP_HOSTS_COUNT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment