# 每天21点自动统计tcp连接数脚本

\#!/bin/bash

\#sudo chmod +x /home/shuncom/net\_conn\_count.sh  
\#crontab -e | 0 21 \* \* \* /home/shuncom/net\_conn\_count.sh

\# 获取当前时间  
timestamp=$(date +"%Y-%m-%d %H:%M:%S")

\# 获取 TCP 6011 端口的连接数  
conn\_count=$(netstat -an | grep ':6011' | grep -i tcp | wc -l)

\# 获取网卡 eth0 的接收和发送数据包数  
\# 第3列为接收包，第11列为发送包  
read rx\_packets tx\_packets &lt;&lt;&lt; $(cat /proc/net/dev | awk '/eth0:/ {gsub(/:/,""); print $3, $11}')

\# 写入日志文件  
echo "$timestamp TCP 6011连接数: $conn\_count, eth0接收包: $rx\_packets, 发送包: $tx\_packets" &gt;&gt; /home/shuncom/net\_conn\_count.log

[![image.png](https://bost.lesrec.cn/uploads/images/gallery/2025-05/scaled-1680-/c0Eimage.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-05/c0Eimage.png)