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}
|
||||
...
|
||||
"""
|
||||
import string
|
||||
import jc.utils
|
||||
|
||||
|
||||
@ -87,33 +88,28 @@ def parse(data, raw=False, quiet=False):
|
||||
|
||||
for line in data:
|
||||
try:
|
||||
raw_output = {}
|
||||
output_line = {}
|
||||
ping_responses = []
|
||||
pattern = None
|
||||
footer = False
|
||||
|
||||
linedata = data.splitlines()
|
||||
|
||||
# check for PATTERN
|
||||
if linedata[0].startswith('PATTERN: '):
|
||||
pattern = linedata.pop(0).split(': ')[1]
|
||||
if line.startswith('PATTERN: '):
|
||||
pattern = line.strip().split(': ')[1]
|
||||
continue
|
||||
|
||||
while not linedata[0].startswith('PING '):
|
||||
linedata.pop(0)
|
||||
if line.startswith('PING '):
|
||||
ipv4 = True if 'bytes of data' in line else False
|
||||
|
||||
ipv4 = True if 'bytes of data' in linedata[0] else False
|
||||
|
||||
if ipv4 and linedata[0][5] not in string.digits:
|
||||
if ipv4 and line[5] not in string.digits:
|
||||
hostname = True
|
||||
elif ipv4 and linedata[0][5] in string.digits:
|
||||
elif ipv4 and line[5] in string.digits:
|
||||
hostname = False
|
||||
elif not ipv4 and ' (' in linedata[0]:
|
||||
elif not ipv4 and ' (' in line:
|
||||
hostname = True
|
||||
else:
|
||||
hostname = False
|
||||
|
||||
for line in filter(None, linedata):
|
||||
if line.startswith('PING '):
|
||||
if ipv4 and not hostname:
|
||||
dst_ip, dta_byts = (2, 3)
|
||||
elif ipv4 and hostname:
|
||||
@ -124,7 +120,7 @@ def parse(data, raw=False, quiet=False):
|
||||
dst_ip, dta_byts = (3, 4)
|
||||
|
||||
line = line.replace('(', ' ').replace(')', ' ')
|
||||
raw_output.update(
|
||||
output_line.update(
|
||||
{
|
||||
'destination_ip': line.split()[dst_ip].lstrip('(').rstrip(')'),
|
||||
'data_bytes': line.split()[dta_byts],
|
||||
@ -135,13 +131,13 @@ def parse(data, raw=False, quiet=False):
|
||||
|
||||
if line.startswith('---'):
|
||||
footer = True
|
||||
raw_output['destination'] = line.split()[1]
|
||||
# raw_output['destination'] = line.split()[1]
|
||||
continue
|
||||
|
||||
if footer:
|
||||
if 'packets transmitted' in line:
|
||||
if ' duplicates,' in line:
|
||||
raw_output.update(
|
||||
output_line.update(
|
||||
{
|
||||
'packets_transmitted': line.split()[0],
|
||||
'packets_received': line.split()[3],
|
||||
@ -152,7 +148,7 @@ def parse(data, raw=False, quiet=False):
|
||||
)
|
||||
continue
|
||||
else:
|
||||
raw_output.update(
|
||||
output_line.update(
|
||||
{
|
||||
'packets_transmitted': line.split()[0],
|
||||
'packets_received': line.split()[3],
|
||||
@ -166,7 +162,7 @@ def parse(data, raw=False, quiet=False):
|
||||
else:
|
||||
split_line = line.split(' = ')[1]
|
||||
split_line = split_line.split('/')
|
||||
raw_output.update(
|
||||
output_line.update(
|
||||
{
|
||||
'round_trip_ms_min': split_line[0],
|
||||
'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,
|
||||
'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
|
||||
|
||||
# normal responses
|
||||
@ -232,12 +238,7 @@ def parse(data, raw=False, quiet=False):
|
||||
'unparsed_line': line
|
||||
}
|
||||
|
||||
ping_responses.append(response)
|
||||
continue
|
||||
|
||||
raw_output['responses'] = ping_responses
|
||||
|
||||
return raw_output
|
||||
output_line.update(response)
|
||||
|
||||
if quiet:
|
||||
output_line['_meta'] = {'success': True}
|
||||
@ -248,15 +249,4 @@ def parse(data, raw=False, quiet=False):
|
||||
yield _process(output_line)
|
||||
|
||||
except Exception as e:
|
||||
if not quiet:
|
||||
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()
|
||||
}
|
||||
}
|
||||
yield jc.utils.stream_error(e, quiet, line)
|
||||
|
Reference in New Issue
Block a user