当前位置:网站首页>[TCP] TCP connection status JSON output on the server

[TCP] TCP connection status JSON output on the server

2022-07-05 10:53:00 HunterMichaelG

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
    }
}

原网站

版权声明
本文为[HunterMichaelG]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051017487036.html