#!/bin/bash
# Description: 统计并获取TCP连接状态
# Author: yqc

# Define variables
Tcp_State=$1

# Define function "Tcp_State_Statistic"
Tcp_State_Statistic(){
	State_Num=$(netstat -tan | awk 'NR>1 {++v[$6]} END {for(k in v) print k,v[k]}' | grep ${Tcp_State} | cut -d' ' -f2)
	if [[ -z $State_Num ]]; then
		State_Num=0
	fi
	echo $State_Num
}

# Notification or Use function "Tcp_State_Statistic"
if [[ $# != 1 ]]; then
	echo "Usage: bash $0 <LISTEN|SYN_SENT|SYN_RCVD|ESTABLISHED|FIN_WAIT1|CLOSE_WAIT|FIN_WAIT2|LAST_ACK|TIME_WAIT>"
else
	Tcp_State_Statistic $1
fi
