获取某个端口的tcp连接数(nginx)

1.


lsof -i:80|grep nginx|wc-l 得到的结果减去监听进程数

或者

netstat -nat|grep -i "80"|grep ESTABLISHED|wc -l

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 

2.

import psutil
import os
def get_connections():

        tcp_conns = 0
        list=psutil.process_iter()
        for p in list:
            name = '?'
            if p.name()=='nginx':
                #print p.pid
                pid=p.pid
                ps=psutil.Process(pid)
                #print ps.connections()
                conn=ps.connections()
                for c in conn:
                    print c.status
                    print  c.laddr[1]
                    if c.status=='ESTABLISHED' and c.laddr[1]==80:
                        tcp_conns=tcp_conns+1
      
        print tcp_conns




 netstat -nat |grep "ESTABLISHED"| awk '{print $4}'|awk -F: '{print $2}'|grep "80"|wc -l 

你可能感兴趣的:(获取某个端口的tcp连接数(nginx))