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,176 +88,165 @@ 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:
|
||||||
|
hostname = True
|
||||||
|
elif ipv4 and line[5] in string.digits:
|
||||||
|
hostname = False
|
||||||
|
elif not ipv4 and ' (' in line:
|
||||||
|
hostname = True
|
||||||
|
else:
|
||||||
|
hostname = False
|
||||||
|
|
||||||
if ipv4 and linedata[0][5] not in string.digits:
|
if ipv4 and not hostname:
|
||||||
hostname = True
|
dst_ip, dta_byts = (2, 3)
|
||||||
elif ipv4 and linedata[0][5] in string.digits:
|
elif ipv4 and hostname:
|
||||||
hostname = False
|
dst_ip, dta_byts = (2, 3)
|
||||||
elif not ipv4 and ' (' in linedata[0]:
|
elif not ipv4 and not hostname:
|
||||||
hostname = True
|
dst_ip, dta_byts = (2, 3)
|
||||||
else:
|
else:
|
||||||
hostname = False
|
dst_ip, dta_byts = (3, 4)
|
||||||
|
|
||||||
for line in filter(None, linedata):
|
line = line.replace('(', ' ').replace(')', ' ')
|
||||||
if line.startswith('PING '):
|
output_line.update(
|
||||||
if ipv4 and not hostname:
|
{
|
||||||
dst_ip, dta_byts = (2, 3)
|
'destination_ip': line.split()[dst_ip].lstrip('(').rstrip(')'),
|
||||||
elif ipv4 and hostname:
|
'data_bytes': line.split()[dta_byts],
|
||||||
dst_ip, dta_byts = (2, 3)
|
'pattern': pattern
|
||||||
elif not ipv4 and not hostname:
|
}
|
||||||
dst_ip, dta_byts = (2, 3)
|
)
|
||||||
else:
|
continue
|
||||||
dst_ip, dta_byts = (3, 4)
|
|
||||||
|
|
||||||
line = line.replace('(', ' ').replace(')', ' ')
|
if line.startswith('---'):
|
||||||
raw_output.update(
|
footer = True
|
||||||
{
|
# raw_output['destination'] = line.split()[1]
|
||||||
'destination_ip': line.split()[dst_ip].lstrip('(').rstrip(')'),
|
continue
|
||||||
'data_bytes': line.split()[dta_byts],
|
|
||||||
'pattern': pattern
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if line.startswith('---'):
|
if footer:
|
||||||
footer = True
|
if 'packets transmitted' in line:
|
||||||
raw_output['destination'] = line.split()[1]
|
if ' duplicates,' in line:
|
||||||
continue
|
output_line.update(
|
||||||
|
|
||||||
if footer:
|
|
||||||
if 'packets transmitted' in line:
|
|
||||||
if ' duplicates,' in line:
|
|
||||||
raw_output.update(
|
|
||||||
{
|
|
||||||
'packets_transmitted': line.split()[0],
|
|
||||||
'packets_received': line.split()[3],
|
|
||||||
'packet_loss_percent': line.split()[7].rstrip('%'),
|
|
||||||
'duplicates': line.split()[5].lstrip('+'),
|
|
||||||
'time_ms': line.split()[11].replace('ms', '')
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
raw_output.update(
|
|
||||||
{
|
|
||||||
'packets_transmitted': line.split()[0],
|
|
||||||
'packets_received': line.split()[3],
|
|
||||||
'packet_loss_percent': line.split()[5].rstrip('%'),
|
|
||||||
'duplicates': '0',
|
|
||||||
'time_ms': line.split()[9].replace('ms', '')
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
else:
|
|
||||||
split_line = line.split(' = ')[1]
|
|
||||||
split_line = split_line.split('/')
|
|
||||||
raw_output.update(
|
|
||||||
{
|
{
|
||||||
'round_trip_ms_min': split_line[0],
|
'packets_transmitted': line.split()[0],
|
||||||
'round_trip_ms_avg': split_line[1],
|
'packets_received': line.split()[3],
|
||||||
'round_trip_ms_max': split_line[2],
|
'packet_loss_percent': line.split()[7].rstrip('%'),
|
||||||
'round_trip_ms_stddev': split_line[3].split()[0]
|
'duplicates': line.split()[5].lstrip('+'),
|
||||||
|
'time_ms': line.split()[11].replace('ms', '')
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
output_line.update(
|
||||||
|
{
|
||||||
|
'packets_transmitted': line.split()[0],
|
||||||
|
'packets_received': line.split()[3],
|
||||||
|
'packet_loss_percent': line.split()[5].rstrip('%'),
|
||||||
|
'duplicates': '0',
|
||||||
|
'time_ms': line.split()[9].replace('ms', '')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
# ping response lines
|
|
||||||
else:
|
else:
|
||||||
# request timeout
|
split_line = line.split(' = ')[1]
|
||||||
if 'no answer yet for icmp_seq=' in line:
|
split_line = split_line.split('/')
|
||||||
timestamp = False
|
output_line.update(
|
||||||
isequence = 5
|
{
|
||||||
|
'round_trip_ms_min': split_line[0],
|
||||||
|
'round_trip_ms_avg': split_line[1],
|
||||||
|
'round_trip_ms_max': split_line[2],
|
||||||
|
'round_trip_ms_stddev': split_line[3].split()[0]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# if timestamp option is specified, then shift icmp sequence field right by one
|
# ping response lines
|
||||||
|
else:
|
||||||
|
# request timeout
|
||||||
|
if 'no answer yet for icmp_seq=' in line:
|
||||||
|
timestamp = False
|
||||||
|
isequence = 5
|
||||||
|
|
||||||
|
# if timestamp option is specified, then shift icmp sequence field right by one
|
||||||
|
if line[0] == '[':
|
||||||
|
timestamp = True
|
||||||
|
isequence = 6
|
||||||
|
|
||||||
|
response = {
|
||||||
|
'type': 'timeout',
|
||||||
|
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
||||||
|
'icmp_seq': line.replace('=', ' ').split()[isequence]
|
||||||
|
}
|
||||||
|
|
||||||
|
output_line.update(response)
|
||||||
|
|
||||||
|
if quiet:
|
||||||
|
output_line['_meta'] = {'success': True}
|
||||||
|
|
||||||
|
if raw:
|
||||||
|
yield output_line
|
||||||
|
else:
|
||||||
|
yield _process(output_line)
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
|
# normal responses
|
||||||
|
elif ' bytes from ' in line:
|
||||||
|
try:
|
||||||
|
line = line.replace('(', ' ').replace(')', ' ').replace('=', ' ')
|
||||||
|
|
||||||
|
# positions of items depend on whether ipv4/ipv6 and/or ip/hostname is used
|
||||||
|
if ipv4 and not hostname:
|
||||||
|
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
|
||||||
|
elif ipv4 and hostname:
|
||||||
|
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
|
||||||
|
elif not ipv4 and not hostname:
|
||||||
|
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
|
||||||
|
elif not ipv4 and hostname:
|
||||||
|
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
|
||||||
|
|
||||||
|
# if timestamp option is specified, then shift everything right by one
|
||||||
|
timestamp = False
|
||||||
if line[0] == '[':
|
if line[0] == '[':
|
||||||
timestamp = True
|
timestamp = True
|
||||||
isequence = 6
|
bts, rip, iseq, t2l, tms = (bts + 1, rip + 1, iseq + 1, t2l + 1, tms + 1)
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
'type': 'timeout',
|
'type': 'reply',
|
||||||
'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]
|
'bytes': line.split()[bts],
|
||||||
|
'response_ip': line.split()[rip].rstrip(':'),
|
||||||
|
'icmp_seq': line.split()[iseq],
|
||||||
|
'ttl': line.split()[t2l],
|
||||||
|
'time_ms': line.split()[tms],
|
||||||
|
'duplicate': True if 'DUP!' in line else False
|
||||||
|
}
|
||||||
|
except Exception:
|
||||||
|
response = {
|
||||||
|
'type': 'unparsable_line',
|
||||||
|
'unparsed_line': line
|
||||||
}
|
}
|
||||||
ping_responses.append(response)
|
|
||||||
continue
|
|
||||||
|
|
||||||
# normal responses
|
output_line.update(response)
|
||||||
elif ' bytes from ' in line:
|
|
||||||
try:
|
|
||||||
line = line.replace('(', ' ').replace(')', ' ').replace('=', ' ')
|
|
||||||
|
|
||||||
# positions of items depend on whether ipv4/ipv6 and/or ip/hostname is used
|
if quiet:
|
||||||
if ipv4 and not hostname:
|
output_line['_meta'] = {'success': True}
|
||||||
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
|
|
||||||
elif ipv4 and hostname:
|
if raw:
|
||||||
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
|
yield output_line
|
||||||
elif not ipv4 and not hostname:
|
else:
|
||||||
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
|
yield _process(output_line)
|
||||||
elif not ipv4 and hostname:
|
|
||||||
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
|
|
||||||
|
|
||||||
# if timestamp option is specified, then shift everything right by one
|
|
||||||
timestamp = False
|
|
||||||
if line[0] == '[':
|
|
||||||
timestamp = True
|
|
||||||
bts, rip, iseq, t2l, tms = (bts + 1, rip + 1, iseq + 1, t2l + 1, tms + 1)
|
|
||||||
|
|
||||||
response = {
|
|
||||||
'type': 'reply',
|
|
||||||
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
|
||||||
'bytes': line.split()[bts],
|
|
||||||
'response_ip': line.split()[rip].rstrip(':'),
|
|
||||||
'icmp_seq': line.split()[iseq],
|
|
||||||
'ttl': line.split()[t2l],
|
|
||||||
'time_ms': line.split()[tms],
|
|
||||||
'duplicate': True if 'DUP!' in line else False
|
|
||||||
}
|
|
||||||
except Exception:
|
|
||||||
response = {
|
|
||||||
'type': 'unparsable_line',
|
|
||||||
'unparsed_line': line
|
|
||||||
}
|
|
||||||
|
|
||||||
ping_responses.append(response)
|
|
||||||
continue
|
|
||||||
|
|
||||||
raw_output['responses'] = ping_responses
|
|
||||||
|
|
||||||
return raw_output
|
|
||||||
|
|
||||||
if quiet:
|
|
||||||
output_line['_meta'] = {'success': True}
|
|
||||||
|
|
||||||
if raw:
|
|
||||||
yield output_line
|
|
||||||
else:
|
|
||||||
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