Python脚本
#!/usr/bin/env python
#coding=utf-8
#Debug in Python2.7
#used for discovery the Listen Port and Process
#Author wangshisheng 20180602
import os
import json
import string
t = os.popen(""" sudo ss -tunlp |grep LISTEN |sed "s#::#FF#g" |grep users |sort |uniq """)
array = []
for port in t.readlines():
r=port.strip()
#print r
port = r.split(‘:‘)[1].split(‘ ‘)[0]
try:
name = r.split(‘\"‘)[1]
except:
name = "none"
cluster = {}
cluster[‘{#TCP_PORT}‘] = port
cluster[‘{#TCP_NAME}‘] = name
array.append(cluster)
#amy = [{k:v} for k,v in dict([list(d.iteritems())[0] for d in array]).iteritems()]
amy = [dict(t) for t in set([tuple(d.items()) for d in array])]
print json.dumps({‘data‘:amy},sort_keys=True,indent=4,separators=(‘,‘,‘:‘))
运行结果如下
{
"data":[
{
"{#TCP_NAME}":"srv.User.plus",
"{#TCP_PORT}":"6049"
},
{
"{#TCP_NAME}":"zabbix_agentd",
"{#TCP_PORT}":"10050"
}
]
}
原文地址:http://blog.51cto.com/ipcpu/2178605
时间: 2024-11-10 05:39:49