美文网首页网络
【tcp】服务器上tcp连接状态json形式输出

【tcp】服务器上tcp连接状态json形式输出

作者: Bogon | 来源:发表于2022-07-04 00:05 被阅读0次

    print_tcp_conection.sh

    #!/bin/bash
    
    STATS=(UNKNOWN:0 ESTABLISHED:0 SYN_SENT:0 SYN_RECV:0 FIN_WAIT1:0 FIN_WAIT2:0 TIME_WAIT:0 CLOSED:0 CLOSE_WAIT:0 LAST_ACK:0 LISTEN:0 CLOSING:0)
    CONN="$(awk '{print $4}' /proc/net/tcp /proc/net/tcp6 | grep -v st | sort | uniq -c | sed -e s/' 0'/' '/g -e s/'A'/'10'/g -e s/'B'/'11'/g | awk '{print $2":"$1}')"
    
    if [ ! -z "$CONN" ];then
      for s in $CONN
      do
        STATS[${s%%:*}]=${STATS[${s%%:*}]%%:*}":"${s#*:}
      done
    fi
    
    echo ${STATS[@]} | sed -e s/' '/',"'/g -e s/':'/'":'/g -e s/^/'{"tcp_conn":{"'/g -e s/'WAIT_'/'WAIT'/g -e s/'$'/'}}'/g
    
    
    # sh print_tcp_conection.sh
    
    {"tcp_conn":{"UNKNOWN":0,"ESTABLISHED":4,"SYN_SENT":0,"SYN_RECV":0,"FIN_WAIT1":0,"FIN_WAIT2":0,"TIME_WAIT":0,"CLOSED":0,"CLOSE_WAIT":0,"LAST_ACK":0,"LISTEN":6,"CLOSING":0}}
    
    
    # sh print_tcp_conection.sh | python -m json.tool
    
    {
        "tcp_conn": {
            "CLOSED": 0,
            "CLOSE_WAIT": 0,
            "CLOSING": 0,
            "ESTABLISHED": 4,
            "FIN_WAIT1": 0,
            "FIN_WAIT2": 0,
            "LAST_ACK": 0,
            "LISTEN": 6,
            "SYN_RECV": 0,
            "SYN_SENT": 0,
            "TIME_WAIT": 0,
            "UNKNOWN": 0
        }
    }
    
    

    相关文章

      网友评论

        本文标题:【tcp】服务器上tcp连接状态json形式输出

        本文链接:https://www.haomeiwen.com/subject/rrlqbrtx.html