#!/usr/bin/pythonimport osimport jsondata = {}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 : passfor port in list(set(port_list)): port_dict = {} port_dict['{#TCP_PORT}'] = port tcp_list.append(port_dict)data['data'] = tcp_listjsonStr = json.dumps(data, sort_keys=True, indent=4)print jsonStr