mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-15 01:24:29 +02:00
somewhat working parser
This commit is contained in:
@ -33,6 +33,7 @@ Examples:
|
|||||||
{example output}
|
{example output}
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
|
import string
|
||||||
import jc.utils
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
@ -87,33 +88,28 @@ def parse(data, raw=False, quiet=False):
|
|||||||
|
|
||||||
for line in data:
|
for line in data:
|
||||||
try:
|
try:
|
||||||
raw_output = {}
|
output_line = {}
|
||||||
ping_responses = []
|
ping_responses = []
|
||||||
pattern = None
|
pattern = None
|
||||||
footer = False
|
footer = False
|
||||||
|
|
||||||
linedata = data.splitlines()
|
|
||||||
|
|
||||||
# check for PATTERN
|
# check for PATTERN
|
||||||
if linedata[0].startswith('PATTERN: '):
|
if line.startswith('PATTERN: '):
|
||||||
pattern = linedata.pop(0).split(': ')[1]
|
pattern = line.strip().split(': ')[1]
|
||||||
|
continue
|
||||||
|
|
||||||
while not linedata[0].startswith('PING '):
|
if line.startswith('PING '):
|
||||||
linedata.pop(0)
|
ipv4 = True if 'bytes of data' in line else False
|
||||||
|
|
||||||
ipv4 = True if 'bytes of data' in linedata[0] else False
|
if ipv4 and line[5] not in string.digits:
|
||||||
|
|
||||||
if ipv4 and linedata[0][5] not in string.digits:
|
|
||||||
hostname = True
|
hostname = True
|
||||||
elif ipv4 and linedata[0][5] in string.digits:
|
elif ipv4 and line[5] in string.digits:
|
||||||
hostname = False
|
hostname = False
|
||||||
elif not ipv4 and ' (' in linedata[0]:
|
elif not ipv4 and ' (' in line:
|
||||||
hostname = True
|
hostname = True
|
||||||
else:
|
else:
|
||||||
hostname = False
|
hostname = False
|
||||||
|
|
||||||
for line in filter(None, linedata):
|
|
||||||
if line.startswith('PING '):
|
|
||||||
if ipv4 and not hostname:
|
if ipv4 and not hostname:
|
||||||
dst_ip, dta_byts = (2, 3)
|
dst_ip, dta_byts = (2, 3)
|
||||||
elif ipv4 and hostname:
|
elif ipv4 and hostname:
|
||||||
@ -124,7 +120,7 @@ def parse(data, raw=False, quiet=False):
|
|||||||
dst_ip, dta_byts = (3, 4)
|
dst_ip, dta_byts = (3, 4)
|
||||||
|
|
||||||
line = line.replace('(', ' ').replace(')', ' ')
|
line = line.replace('(', ' ').replace(')', ' ')
|
||||||
raw_output.update(
|
output_line.update(
|
||||||
{
|
{
|
||||||
'destination_ip': line.split()[dst_ip].lstrip('(').rstrip(')'),
|
'destination_ip': line.split()[dst_ip].lstrip('(').rstrip(')'),
|
||||||
'data_bytes': line.split()[dta_byts],
|
'data_bytes': line.split()[dta_byts],
|
||||||
@ -135,13 +131,13 @@ def parse(data, raw=False, quiet=False):
|
|||||||
|
|
||||||
if line.startswith('---'):
|
if line.startswith('---'):
|
||||||
footer = True
|
footer = True
|
||||||
raw_output['destination'] = line.split()[1]
|
# raw_output['destination'] = line.split()[1]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if footer:
|
if footer:
|
||||||
if 'packets transmitted' in line:
|
if 'packets transmitted' in line:
|
||||||
if ' duplicates,' in line:
|
if ' duplicates,' in line:
|
||||||
raw_output.update(
|
output_line.update(
|
||||||
{
|
{
|
||||||
'packets_transmitted': line.split()[0],
|
'packets_transmitted': line.split()[0],
|
||||||
'packets_received': line.split()[3],
|
'packets_received': line.split()[3],
|
||||||
@ -152,7 +148,7 @@ def parse(data, raw=False, quiet=False):
|
|||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raw_output.update(
|
output_line.update(
|
||||||
{
|
{
|
||||||
'packets_transmitted': line.split()[0],
|
'packets_transmitted': line.split()[0],
|
||||||
'packets_received': line.split()[3],
|
'packets_received': line.split()[3],
|
||||||
@ -166,7 +162,7 @@ def parse(data, raw=False, quiet=False):
|
|||||||
else:
|
else:
|
||||||
split_line = line.split(' = ')[1]
|
split_line = line.split(' = ')[1]
|
||||||
split_line = split_line.split('/')
|
split_line = split_line.split('/')
|
||||||
raw_output.update(
|
output_line.update(
|
||||||
{
|
{
|
||||||
'round_trip_ms_min': split_line[0],
|
'round_trip_ms_min': split_line[0],
|
||||||
'round_trip_ms_avg': split_line[1],
|
'round_trip_ms_avg': split_line[1],
|
||||||
@ -192,7 +188,17 @@ def parse(data, raw=False, quiet=False):
|
|||||||
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
||||||
'icmp_seq': line.replace('=', ' ').split()[isequence]
|
'icmp_seq': line.replace('=', ' ').split()[isequence]
|
||||||
}
|
}
|
||||||
ping_responses.append(response)
|
|
||||||
|
output_line.update(response)
|
||||||
|
|
||||||
|
if quiet:
|
||||||
|
output_line['_meta'] = {'success': True}
|
||||||
|
|
||||||
|
if raw:
|
||||||
|
yield output_line
|
||||||
|
else:
|
||||||
|
yield _process(output_line)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# normal responses
|
# normal responses
|
||||||
@ -232,12 +238,7 @@ def parse(data, raw=False, quiet=False):
|
|||||||
'unparsed_line': line
|
'unparsed_line': line
|
||||||
}
|
}
|
||||||
|
|
||||||
ping_responses.append(response)
|
output_line.update(response)
|
||||||
continue
|
|
||||||
|
|
||||||
raw_output['responses'] = ping_responses
|
|
||||||
|
|
||||||
return raw_output
|
|
||||||
|
|
||||||
if quiet:
|
if quiet:
|
||||||
output_line['_meta'] = {'success': True}
|
output_line['_meta'] = {'success': True}
|
||||||
@ -248,15 +249,4 @@ def parse(data, raw=False, quiet=False):
|
|||||||
yield _process(output_line)
|
yield _process(output_line)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not quiet:
|
yield jc.utils.stream_error(e, quiet, line)
|
||||||
e.args = (str(e) + '... Use the quiet option (-q) to ignore errors.',)
|
|
||||||
raise e
|
|
||||||
else:
|
|
||||||
yield {
|
|
||||||
'_meta':
|
|
||||||
{
|
|
||||||
'success': False,
|
|
||||||
'error': 'error parsing line',
|
|
||||||
'line': line.strip()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user