Friday, 6 November 2015

Concurrent connection in apache

#!/bin/bash

## Purpose:
## This script provide number of connection established with the port and type of connection


## Variable Declaration

LINE='#######################################################################################################'
tput clear
RED='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'

## Help Menu

help()
        {
        echo -e '\033[33;36m
        USAGE:'${NC}' '$0' OPTIONS [ m | p | h ]

        -m Accepts multiport --> '$0' 80 443
        -p Accept single port --> '$0' 80
        '
        }



## Packet function

packet()
        {

        echo $LINE
        echo -e "\t\t\t${RED} PORT => ${1} $NC"
        total=`netstat -antupe |  perl -ane 'if($F[4]=~/:'${1}'/) {print}' | tee /tmp/netstat.log | wc -l`
        echo
        echo  -e "Total number of request for the  Port: ${1}  => ${GREEN}  ${total} ${NC}\n"
        echo "Detailed request for the port number: ${1}"
        cat /tmp/netstat.log | perl -ane 'print $F[5]."\n"' | uniq -c
        perl -e 'print "\n" foreach(0..2)'
        echo $LINE

        }

##Main

while getopts "p:m:h" opt
do
        case $opt in

                p)
                shift
                [[ $# -ne 1 ]] && echo "Pass single arguement " && exit
                packet $OPTARG;;
                m)
                shift
                [[ $# -lt 2 ]] && echo -e "not enough arguement\n use -p instead for single port" && exit 7
                for i in $*
                do
                        packet ${i}
                        echo
                done;;
                h)
                help;;
                *)
                echo "Generating help menu"
                help;;

        esac
done