#!/bin/sh # Function # This function is used to get network info quick and easy. # # Usage # netinfo Command # # Parameters # $1 = The type of information we want to display. # Valid Values: # # "count" # Use netstat command to calculate and count the number of connections each IP address makes to the server. # # "countest" # Check on ESTABLISHED connections instead of all connections, and displays the connections count for each IP. # # "count80" # Show and list IP address and its connection count that connect to port 80 on the server. # Port 80 is used mainly by HTTP web page request. # # Author # Russ Tanner, russ@russtanner.com # Portions derrived from mydigitallife.info: # http://www.mydigitallife.info/2007/12/13/how-to-find-and-check-number-of-connections-to-a-server/ # # Version History # 1.0 7/15/2009 Tested by Russ Tanner ### ############################# # Configuration Variables ############################# # Get the basename for this script BaseName=`basename $0` # The standard Usage message Usage="Usage: $BaseName count|countest|count80" ############################# # Error Traps ############################# # Error Trap: Too few arguments, exit now if [ $# -lt 1 ]; then echo -e "Error: Too few arguments.\n$Usage" exit 1 fi ############################# # Code ############################# # Vector to Correct Code... case "$1" in # "count" count) echo "Displaying connection counts for all IPs." netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n ;; # "countest" countest) echo "Displaying connection counts for all established connections." netstat -ntu | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr ;; # "count80" count80) echo "Displaying connections counts for port 80." netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1 ;; # Catch all *) echo -e "Unrecognized argument.\n$Usage" exit 1 ;; esac # Clean up echo "Done."