zabbix自動發現端口監控py腳本

#!/usr/bin/python
import os
import json

data = {}
tcp_list = []
port_list = []
command = 'netstat -nuplt| grep "LISTEN" |awk \'{print $4}\' |awk -F ":" \'{print $2}\' |grep -Ev \'^$\''
lines = os.popen(command).readlines()
for line in lines:
    port = int(line.strip())
    if port >= 1024:
        portstr = str(port)
        port_list.append(portstr)
    else :
        pass

for port in list(set(port_list)):
    port_dict = {}
    port_dict['{#TCP_PORT}'] = port
    tcp_list.append(port_dict)

data['data'] = tcp_list
jsonStr = json.dumps(data, sort_keys=True, indent=4)
print jsonStr


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章