您的当前位置:首页正文

zabbix端口自动发现的python script

2024-11-07 来源:个人技术集锦
#!/usr/bin/env python

import os
import json
portlist = []
new_port_list = []
port_dict = {"data":None}
cmd = '''netstat -tnlp|egrep -i "$1"|awk {'print $4'}|'''
cmd += '''awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort -n| uniq 2>/dev/null'''
auto_localport = os.popen(cmd).readlines()
for ports in auto_localport:
    new_port = ports.strip()
    portlist.append(new_port)
for port in portlist:
    pdict = {}
    #print(port)
    pdict["{#TCP_PORT}"] = port
    new_port_list.append(pdict)
    port_dict["data"] = new_port_list
jsonStr = json.dumps(port_dict,sort_keys=True,indent=4)
print(jsonStr)
Top