1.使用netstat查看tcp命令:
netstat -an | grep ^tcp :获取以tcp开头的数据
2.使用ss查看tcp命令:
ss -an | grep ^tcp :获取以tcp开头的数据
3. netstat -an | grep ^tcp | head -1 :获取以tcp开头的数据且打印第一行
4. ss -an | grep ^tcp | head -1 :获取以tcp开头的数据且打印第一行
5.ss获取tcp连接状态(人工查看)命令:
ss -an |grep ^tcp |awk '{tcp_connect_status[$2]++} END{for(i in tcp_connect_status) {print i,tcp_connect_status[i]}}'
6编写结合zabbix的文件
vim zabbix_tcp_connect_status.sh
#!/bin/bash LISTEN(){ ss -an|grep "^tcp" |grep "LISTEN"|wc -l #使用ss命令获取以tcp开头的内容且包含LISTEN 的行数 } $1 #位置变量,传入值,调用相应的函数
:wq! :保存
chmod a+x zabbix_tcp_connect_status.sh :赋可执行权限
./zabbix_tcp_connect_status.sh LISTEN :调用函数LISTEN
7.获取tcp所有(11中)状态的脚本
#!/bin/bash CLOSED(){ ss -an|grep "^tcp" |grep "CLOSED"|wc -l } LISTEN(){ ss -an|grep "^tcp" |grep "LISTEN"|wc -l } SYN_RCVD(){ ss -an|grep "^tcp" |grep "SYN[-_]RCVD"|wc -l #[-_]表示匹配一个_或者一个- } SYN_SENT(){ ss -an|grep "^tcp" |grep "SYN[-_]SENT"|wc -l } ESTABLISHED(){ ss -an|grep "^tcp" |grep "ESTAB"|wc -l } FIN_WAIT_1(){ ss -an|grep "^tcp" |grep "FIN[-_]WAIT[-_]1"|wc -l } FIN_WAIT_2(){ ss -an|grep "^tcp" |grep "FIN[-_]WAIT[-_]2"|wc -l } TIME_WAIT(){ ss -an|grep "^tcp" |grep "TIME[-_]WAIT"|wc -l } CLOSING(){ ss -an|grep "^tcp" |grep "CLOSING"|wc -l } CLOSE_WAIT(){ ss -an|grep "^tcp" |grep "CLOSE[-_]WAIT"|wc -l } LAST_ACK(){ ss -an|grep "^tcp" |grep "LAST[-_]ACK"|wc -l } $1