1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-17 01:32:37 +02:00

add support for +noall +answer

This commit is contained in:
Kelly Brazil
2021-05-07 16:42:09 -07:00
parent 3c51b2d83d
commit 61851c1bd0
2 changed files with 85 additions and 3 deletions

View File

@ -3,6 +3,8 @@
# jc.parsers.dig
jc - JSON CLI output utility `dig` command output parser
The `+noall +answer` options are supported in cases where only the answer information is desired.
The `when_epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
The `when_epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
@ -274,6 +276,42 @@ Examples:
}
]
$ dig +noall +answer cnn.com | jc --dig -p
[
{
"answer": [
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.193.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.65.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.1.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.129.67"
}
]
}
]
## info
```python
@ -301,4 +339,4 @@ Returns:
## Parser Information
Compatibility: linux, aix, freebsd, darwin
Version 2.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 2.1 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@ -1,5 +1,7 @@
"""jc - JSON CLI output utility `dig` command output parser
The `+noall +answer` options are supported in cases where only the answer information is desired.
The `when_epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
The `when_epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
@ -270,13 +272,49 @@ Examples:
"rcvd": "78"
}
]
$ dig +noall +answer cnn.com | jc --dig -p
[
{
"answer": [
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.193.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.65.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.1.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 60,
"data": "151.101.129.67"
}
]
}
]
"""
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '2.0'
version = '2.1'
description = '`dig` command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@ -500,6 +538,7 @@ def parse(data, raw=False, quiet=False):
# section can be: header, flags, question, authority, answer, axfr, additional, opt_pseudosection, footer
section = ''
output_entry = {}
answer_list = []
if jc.utils.has_data(data):
for line in cleandata:
@ -581,7 +620,12 @@ def parse(data, raw=False, quiet=False):
output_entry.update({'authority': authority_list})
continue
if not line.startswith(';') and section == 'answer':
# https://github.com/kellyjonbrazil/jc/issues/133
# to allow parsing of output that only has the answer section - e.g:
# dig +noall +answer example.com
# we allow section to be 'answer' (normal output) or
# '', which means +noall +answer was used.
if not line.startswith(';') and (section == 'answer' or section == ''):
answer_list.append(_parse_answer(line))
output_entry.update({'answer': answer_list})
continue