diff --git a/jc/parsers/netstat.py b/jc/parsers/netstat.py index 4061add0..75986310 100644 --- a/jc/parsers/netstat.py +++ b/jc/parsers/netstat.py @@ -159,6 +159,24 @@ def parse_line(entry): parsed_line = entry.split() output_line = {} + if entry.find('tcp') == 0: + output_line['session_protocol'] = 'tcp' + + if entry.find('p6') == 2: + output_line['network_protocol'] = 'ipv6' + + else: + output_line['network_protocol'] = 'ipv4' + + elif entry.find('udp') == 0: + output_line['session_protocol'] = 'udp' + + if entry.find('p6') == 2: + output_line['network_protocol'] = 'ipv6' + + else: + output_line['network_protocol'] = 'ipv4' + output_line['local_address'] = parsed_line[3].rsplit(':', 1)[0] output_line['local_port'] = parsed_line[3].rsplit(':', 1)[-1] output_line['foreign_address'] = parsed_line[4].rsplit(':', 1)[0] @@ -189,11 +207,9 @@ def parse(data): for line in cleandata: if line.find('Active Internet connections (w/o servers)') == 0: - state.section = 'client' continue if line.find('Active Internet connections (only servers)') == 0: - state.section = 'server' continue if line.find('Proto') == 0: @@ -202,119 +218,6 @@ def parse(data): if line.find('Active UNIX') == 0: break - if state.section == 'client': - if line.find('tcp') == 0: - state.session = 'tcp' - if line.find('p6') == 2: - state.network = 'ipv6' - else: - state.network = 'ipv4' - elif line.find('udp') == 0: - state.session = 'udp' - if line.find('p6') == 2: - state.network = 'ipv6' - else: - state.network = 'ipv4' - elif state.section == 'server': - if line.find('tcp') == 0: - state.session = 'tcp' - if line.find('p6') == 2: - state.network = 'ipv6' - else: - state.network = 'ipv4' - elif line.find('udp') == 0: - state.session = 'udp' - if line.find('p6') == 2: - state.network = 'ipv6' - else: - state.network = 'ipv4' - - # client section - if state.section == 'client' and state.session == 'tcp' and state.network == 'ipv4': - state.client_tcp_ip4.append(parse_line(line)) - - if state.section == 'client' and state.session == 'tcp' and state.network == 'ipv6': - state.client_tcp_ip6.append(parse_line(line)) - - if state.section == 'client' and state.session == 'udp' and state.network == 'ipv4': - state.client_udp_ip4.append(parse_line(line)) - - if state.section == 'client' and state.session == 'udp' and state.network == 'ipv6': - state.client_udp_ip6.append(parse_line(line)) - - # server section - if state.section == 'server' and state.session == 'tcp' and state.network == 'ipv4': - state.server_tcp_ip4.append(parse_line(line)) - - if state.section == 'server' and state.session == 'tcp' and state.network == 'ipv6': - state.server_tcp_ip6.append(parse_line(line)) - - if state.section == 'server' and state.session == 'udp' and state.network == 'ipv4': - state.server_udp_ip4.append(parse_line(line)) - - if state.section == 'server' and state.session == 'udp' and state.network == 'ipv6': - state.server_udp_ip6.append(parse_line(line)) - - state.session = '' - state.network = '' - - # build dictionary - # client section - if state.client_tcp_ip4: - if 'client' not in output: - output['client'] = {} - if 'tcp' not in output['client']: - output['client']['tcp'] = {} - output['client']['tcp']['ipv4'] = state.client_tcp_ip4 - - if state.client_tcp_ip6: - if 'client' not in output: - output['client'] = {} - if 'tcp' not in output['client']: - output['client']['tcp'] = {} - output['client']['tcp']['ipv6'] = state.client_tcp_ip6 - - if state.client_udp_ip4: - if 'client' not in output: - output['client'] = {} - if 'udp' not in output['client']: - output['client']['udp'] = {} - output['client']['udp']['ipv4'] = state.client_udp_ip4 - - if state.client_udp_ip6: - if 'client' not in output: - output['client'] = {} - if 'udp' not in output['client']: - output['client']['udp'] = {} - output['client']['udp']['ipv6'] = state.client_udp_ip6 - - # server section - if state.server_tcp_ip4: - if 'server' not in output: - output['server'] = {} - if 'tcp' not in output['server']: - output['server']['tcp'] = {} - output['server']['tcp']['ipv4'] = state.server_tcp_ip4 - - if state.server_tcp_ip6: - if 'server' not in output: - output['server'] = {} - if 'tcp' not in output['server']: - output['server']['tcp'] = {} - output['server']['tcp']['ipv6'] = state.server_tcp_ip6 - - if state.server_udp_ip4: - if 'server' not in output: - output['server'] = {} - if 'udp' not in output['server']: - output['server']['udp'] = {} - output['server']['udp']['ipv4'] = state.server_udp_ip4 - - if state.server_udp_ip6: - if 'server' not in output: - output['server'] = {} - if 'udp' not in output['server']: - output['server']['udp'] = {} - output['server']['udp']['ipv6'] = state.server_udp_ip6 + output.append(parse_line(line)) return output