作者: reistlin
来源: http://www.reistlin.com/blog/359
更新时间: 2011.08
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

linux.png

googlecode1.gifgooglecode2.gif
[https://reistlin.googlecode.com/svn/trunk/shell/tcp_status_stat.sh]

#!/bin/bash

# name: tcp status stat v0.2
# author: reistlin
# website: www.reistlin.com
# date: 2011.08.12

# Function
cmd() {
	netstat -nt | grep $1 | awk '{print $5}' | awk -F ":" '{print $1}' | grep -v ^$ | sort | uniq -c | sort -nr | head
}

# Case
case "$1" in
	# SYN RECV
        SYN_RECV)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
		cmd $1
        shift
        ;;
        # ESTABLISHED 
        ESTABLISHED)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
        # FIN_WAIT1
        FIN_WAIT1)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
        # FIN_WAIT2
        FIN_WAIT2)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
        # CLOSE_WAIT
        CLOSE_WAIT)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
        # LAST_ACK
        LAST_ACK)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
        # CLOSING
        CLOSING)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
        # TIME_WAIT
        TIME_WAIT)
                echo "=============================="
                echo "$1 Source IP Top10"
                echo "=============================="
                cmd $1
        shift
        ;;
	# Summary (Default)
	*)
		echo "=============================="
		echo "TCP Sataus Stat (Summary)"
		echo "=============================="
		netstat -nt | awk '{print $6}' | grep -v ^$ | sort | uniq -c | sort -nr

		echo "=============================="
		echo "Source IP Top10 (Summary)"
		echo "=============================="
		netstat -nt | awk '{print $5}' | awk -F ":" '{print $1}'  | sort | uniq -c | sort -nr | head
	shift
	;;
esac

标签: shell