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

update docs with new dig parser examples

This commit is contained in:
Kelly Brazil
2021-04-16 16:19:20 -07:00
parent 7c584b89a6
commit c6aa4d0835
6 changed files with 252 additions and 317 deletions

View File

@ -9,62 +9,56 @@
JSON CLI output utility JSON CLI output utility
`jc` JSONifies the output of many CLI tools and file-types for easier parsing in scripts. See the [**Parsers**](#parsers) section for supported commands and file-types. `jc` JSONifies the output of many CLI tools and file-types for easier parsing in scripts. See the [**Parsers**](#parsers) section for supported commands and file-types.
This allows further command-line processing of output with tools like `jq` by piping commands:
```bash ```bash
ls -l /usr/bin | jc --ls | jq '.[] | select(.size > 50000000)' dig example.com | jc --dig
``` ```
```json ```json
{ [{"id":38052,"opcode":"QUERY","status":"NOERROR","flags":["qr","rd","ra"],"query_num":1,"answer_num":1,
"filename": "docker", "authority_num":0,"additional_num":1,"opt_pseudosection":{"edns":{"version":0,"flags":[],"udp":4096}},"question":
"flags": "-rwxr-xr-x", {"name":"example.com.","class":"IN","type":"A"},"answer":[{"name":"example.com.","class":"IN","type":"A","ttl":
"links": 1, 39049,"data":"93.184.216.34"}],"query_time":49,"server":"2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)","when":
"owner": "root", "Fri Apr 16 16:09:00 PDT 2021","rcvd":56,"when_epoch":1618614540,"when_epoch_utc":null}]
"group": "root", ```
"size": 68677120, This allows further command-line processing of output with tools like `jq` by piping commands:
"date": "Aug 14 19:41" ```bash
} $ dig example.com | jc --dig | jq -r '.[].answer[].data'
93.184.216.34
``` ```
or using the alternative "magic" syntax: or using the alternative "magic" syntax:
```bash ```bash
jc ls -l /usr/bin | jq '.[] | select(.size > 50000000)' $ jc dig example.com | jq -r '.[].answer[].data'
``` 93.184.216.34
```json
{
"filename": "docker",
"flags": "-rwxr-xr-x",
"links": 1,
"owner": "root",
"group": "root",
"size": 68677120,
"date": "Aug 14 19:41"
}
``` ```
The `jc` parsers can also be used as python modules. In this case the output will be a python dictionary, or list of dictionaries, instead of JSON: The `jc` parsers can also be used as python modules. In this case the output will be a python dictionary, or list of dictionaries, instead of JSON:
```python ```python
>>> import jc.parsers.ls >>> import jc.parsers.dig
>>> >>>
>>> data='''-rwxr-xr-x 1 root wheel 23648 May 3 22:26 cat >>> data = '''; <<>> DiG 9.10.6 <<>> example.com
... -rwxr-xr-x 1 root wheel 30016 May 3 22:26 chmod ... ;; global options: +cmd
... -rwxr-xr-x 1 root wheel 29024 May 3 22:26 cp ... ;; Got answer:
... -rwxr-xr-x 1 root wheel 375824 May 3 22:26 csh ... ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64612
... -rwxr-xr-x 1 root wheel 28608 May 3 22:26 date ... ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
... -rwxr-xr-x 1 root wheel 32000 May 3 22:26 dd ...
... -rwxr-xr-x 1 root wheel 23392 May 3 22:26 df ... ;; OPT PSEUDOSECTION:
... -rwxr-xr-x 1 root wheel 18128 May 3 22:26 echo''' ... ; EDNS: version: 0, flags:; udp: 4096
... ;; QUESTION SECTION:
... ;example.com. IN A
...
... ;; ANSWER SECTION:
... example.com. 29658 IN A 93.184.216.34
...
... ;; Query time: 52 msec
... ;; SERVER: 2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)
... ;; WHEN: Fri Apr 16 16:13:00 PDT 2021
... ;; MSG SIZE rcvd: 56'''
>>> >>>
>>> jc.parsers.ls.parse(data) >>> jc.parsers.dig.parse(data)
[{'filename': 'cat', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 23648, [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num':
'date': 'May 3 22:26'}, {'filename': 'chmod', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 1, 'authority_num': 0, 'additional_num': 1, 'opt_pseudosection': {'edns': {'version': 0, 'flags': [], 'udp':
'group': 'wheel', 'size': 30016, 'date': 'May 3 22:26'}, {'filename': 'cp', 'flags': '-rwxr-xr-x', 4096}}, 'question': {'name': 'example.com.', 'class': 'IN', 'type': 'A'}, 'answer': [{'name': 'example.com.',
'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 29024, 'date': 'May 3 22:26'}, {'filename': 'csh', 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data': '93.184.216.34'}], 'query_time': 52, 'server':
'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 375824, 'date': 'May 3 '2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56,
22:26'}, {'filename': 'date', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'when_epoch': 1618614780, 'when_epoch_utc': None}]
'size': 28608, 'date': 'May 3 22:26'}, {'filename': 'dd', 'flags': '-rwxr-xr-x', 'links': 1, 'owner':
'root', 'group': 'wheel', 'size': 32000, 'date': 'May 3 22:26'}, {'filename': 'df', 'flags':
'-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 23392, 'date': 'May 3 22:26'},
{'filename': 'echo', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 18128,
'date': 'May 3 22:26'}]
``` ```
Two representations of the data are possible. The default representation uses a strict schema per parser and converts known numbers to int/float JSON values. Certain known values of `None` are converted to JSON `null`, known boolean values are converted, and, in some cases, additional semantic context fields are added. Two representations of the data are possible. The default representation uses a strict schema per parser and converts known numbers to int/float JSON values. Certain known values of `None` are converted to JSON `null`, known boolean values are converted, and, in some cases, additional semantic context fields are added.

View File

@ -43,6 +43,16 @@ Schema:
"data": string "data": string
} }
], ],
"opt_pseudosection": {
"edns": {
"version": integer,
"flags": [
string
],
"udp": integer
},
"cookie": string
},
"question": { "question": {
"name": string, "name": string,
"class": string, "class": string,
@ -57,6 +67,15 @@ Schema:
"data": string "data": string
} }
], ],
"additional": [
{
"name": string,
"class": string,
"type": string,
"ttl": integer,
"data": string
}
],
"authority": [ "authority": [
{ {
"name": string, "name": string,
@ -78,10 +97,10 @@ Schema:
Examples: Examples:
$ dig cnn.com www.cnn.com @205.251.194.64 | jc --dig -p $ dig example.com | jc --dig -p
[ [
{ {
"id": 52172, "id": 2951,
"opcode": "QUERY", "opcode": "QUERY",
"status": "NOERROR", "status": "NOERROR",
"flags": [ "flags": [
@ -90,113 +109,35 @@ Examples:
"ra" "ra"
], ],
"query_num": 1, "query_num": 1,
"answer_num": 4, "answer_num": 1,
"authority_num": 0, "authority_num": 0,
"additional_num": 1, "additional_num": 1,
"opt_pseudosection": {
"edns": {
"version": 0,
"flags": [],
"udp": 4096
}
},
"question": { "question": {
"name": "cnn.com.", "name": "example.com.",
"class": "IN", "class": "IN",
"type": "A" "type": "A"
}, },
"answer": [ "answer": [
{ {
"name": "cnn.com.", "name": "example.com.",
"class": "IN", "class": "IN",
"type": "A", "type": "A",
"ttl": 27, "ttl": 39302,
"data": "151.101.65.67" "data": "93.184.216.34"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 27,
"data": "151.101.129.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 27,
"data": "151.101.1.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": 27,
"data": "151.101.193.67"
} }
], ],
"query_time": 38, "query_time": 49,
"server": "2600", "server": "2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)",
"when": "Tue Mar 30 20:07:59 PDT 2021", "when": "Fri Apr 16 16:05:10 PDT 2021",
"rcvd": 100, "rcvd": 56,
"when_epoch": 1617160079, "when_epoch": 1618614310,
"when_epoch_utc": null
},
{
"id": 36292,
"opcode": "QUERY",
"status": "NOERROR",
"flags": [
"qr",
"aa",
"rd"
],
"query_num": 1,
"answer_num": 1,
"authority_num": 4,
"additional_num": 1,
"question": {
"name": "www.cnn.com.",
"class": "IN",
"type": "A"
},
"answer": [
{
"name": "www.cnn.com.",
"class": "IN",
"type": "CNAME",
"ttl": 300,
"data": "turner-tls.map.fastly.net."
}
],
"authority": [
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": 3600,
"data": "ns-1086.awsdns-07.org."
},
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": 3600,
"data": "ns-1630.awsdns-11.co.uk."
},
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": 3600,
"data": "ns-47.awsdns-05.com."
},
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": 3600,
"data": "ns-576.awsdns-08.net."
}
],
"query_time": 27,
"server": "205.251.194.64#53(205.251.194.64)",
"when": "Tue Mar 30 20:07:59 PDT 2021",
"rcvd": 212,
"when_epoch": 1617160079,
"when_epoch_utc": null "when_epoch_utc": null
} }
] ]
@ -204,7 +145,7 @@ Examples:
$ dig cnn.com www.cnn.com @205.251.194.64 | jc --dig -p -r $ dig cnn.com www.cnn.com @205.251.194.64 | jc --dig -p -r
[ [
{ {
"id": "23843", "id": "46052",
"opcode": "QUERY", "opcode": "QUERY",
"status": "NOERROR", "status": "NOERROR",
"flags": [ "flags": [
@ -213,110 +154,34 @@ Examples:
"ra" "ra"
], ],
"query_num": "1", "query_num": "1",
"answer_num": "4", "answer_num": "1",
"authority_num": "0", "authority_num": "0",
"additional_num": "1", "additional_num": "1",
"opt_pseudosection": {
"edns": {
"version": "0",
"flags": [],
"udp": "4096"
}
},
"question": { "question": {
"name": "cnn.com.", "name": "example.com.",
"class": "IN", "class": "IN",
"type": "A" "type": "A"
}, },
"answer": [ "answer": [
{ {
"name": "cnn.com.", "name": "example.com.",
"class": "IN", "class": "IN",
"type": "A", "type": "A",
"ttl": "30", "ttl": "40426",
"data": "151.101.193.67" "data": "93.184.216.34"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": "30",
"data": "151.101.1.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": "30",
"data": "151.101.65.67"
},
{
"name": "cnn.com.",
"class": "IN",
"type": "A",
"ttl": "30",
"data": "151.101.129.67"
} }
], ],
"query_time": "24 msec", "query_time": "48 msec",
"server": "192.168.1.254#53(192.168.1.254)", "server": "2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)",
"when": "Tue Nov 12 07:16:19 PST 2019", "when": "Fri Apr 16 16:06:12 PDT 2021",
"rcvd": "100" "rcvd": "56"
},
{
"id": "8266",
"opcode": "QUERY",
"status": "NOERROR",
"flags": [
"qr",
"aa",
"rd"
],
"query_num": "1",
"answer_num": "1",
"authority_num": "4",
"additional_num": "1",
"question": {
"name": "www.cnn.com.",
"class": "IN",
"type": "A"
},
"answer": [
{
"name": "www.cnn.com.",
"class": "IN",
"type": "CNAME",
"ttl": "300",
"data": "turner-tls.map.fastly.net."
}
],
"authority": [
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": "3600",
"data": "ns-1086.awsdns-07.org."
},
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": "3600",
"data": "ns-1630.awsdns-11.co.uk."
},
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": "3600",
"data": "ns-47.awsdns-05.com."
},
{
"name": "cnn.com.",
"class": "IN",
"type": "NS",
"ttl": "3600",
"data": "ns-576.awsdns-08.net."
}
],
"query_time": "26 msec",
"server": "205.251.194.64#53(205.251.194.64)",
"when": "Tue Nov 12 07:16:19 PST 2019",
"rcvd": "212"
} }
] ]
@ -421,4 +286,4 @@ Returns:
## Parser Information ## Parser Information
Compatibility: linux, aix, freebsd, darwin Compatibility: linux, aix, freebsd, darwin
Version 1.7 by Kelly Brazil (kellyjonbrazil@gmail.com) Version 2.0 by Kelly Brazil (kellyjonbrazil@gmail.com)

Binary file not shown.

View File

@ -94,11 +94,93 @@ Schema:
Examples: Examples:
$ dig cnn.com www.cnn.com @205.251.194.64 | jc --dig -p $ dig example.com | jc --dig -p
[
{
"id": 2951,
"opcode": "QUERY",
"status": "NOERROR",
"flags": [
"qr",
"rd",
"ra"
],
"query_num": 1,
"answer_num": 1,
"authority_num": 0,
"additional_num": 1,
"opt_pseudosection": {
"edns": {
"version": 0,
"flags": [],
"udp": 4096
}
},
"question": {
"name": "example.com.",
"class": "IN",
"type": "A"
},
"answer": [
{
"name": "example.com.",
"class": "IN",
"type": "A",
"ttl": 39302,
"data": "93.184.216.34"
}
],
"query_time": 49,
"server": "2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)",
"when": "Fri Apr 16 16:05:10 PDT 2021",
"rcvd": 56,
"when_epoch": 1618614310,
"when_epoch_utc": null
}
]
$ dig cnn.com www.cnn.com @205.251.194.64 | jc --dig -p -r $ dig cnn.com www.cnn.com @205.251.194.64 | jc --dig -p -r
[
{
"id": "46052",
"opcode": "QUERY",
"status": "NOERROR",
"flags": [
"qr",
"rd",
"ra"
],
"query_num": "1",
"answer_num": "1",
"authority_num": "0",
"additional_num": "1",
"opt_pseudosection": {
"edns": {
"version": "0",
"flags": [],
"udp": "4096"
}
},
"question": {
"name": "example.com.",
"class": "IN",
"type": "A"
},
"answer": [
{
"name": "example.com.",
"class": "IN",
"type": "A",
"ttl": "40426",
"data": "93.184.216.34"
}
],
"query_time": "48 msec",
"server": "2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)",
"when": "Fri Apr 16 16:06:12 PDT 2021",
"rcvd": "56"
}
]
$ dig -x 1.1.1.1 | jc --dig -p $ dig -x 1.1.1.1 | jc --dig -p
[ [

Binary file not shown.

View File

@ -9,62 +9,56 @@
JSON CLI output utility JSON CLI output utility
`jc` JSONifies the output of many CLI tools and file-types for easier parsing in scripts. See the [**Parsers**](#parsers) section for supported commands and file-types. `jc` JSONifies the output of many CLI tools and file-types for easier parsing in scripts. See the [**Parsers**](#parsers) section for supported commands and file-types.
This allows further command-line processing of output with tools like `jq` by piping commands:
```bash ```bash
ls -l /usr/bin | jc --ls | jq '.[] | select(.size > 50000000)' dig example.com | jc --dig
``` ```
```json ```json
{ [{"id":38052,"opcode":"QUERY","status":"NOERROR","flags":["qr","rd","ra"],"query_num":1,"answer_num":1,
"filename": "docker", "authority_num":0,"additional_num":1,"opt_pseudosection":{"edns":{"version":0,"flags":[],"udp":4096}},"question":
"flags": "-rwxr-xr-x", {"name":"example.com.","class":"IN","type":"A"},"answer":[{"name":"example.com.","class":"IN","type":"A","ttl":
"links": 1, 39049,"data":"93.184.216.34"}],"query_time":49,"server":"2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)","when":
"owner": "root", "Fri Apr 16 16:09:00 PDT 2021","rcvd":56,"when_epoch":1618614540,"when_epoch_utc":null}]
"group": "root", ```
"size": 68677120, This allows further command-line processing of output with tools like `jq` by piping commands:
"date": "Aug 14 19:41" ```bash
} $ dig example.com | jc --dig | jq -r '.[].answer[].data'
93.184.216.34
``` ```
or using the alternative "magic" syntax: or using the alternative "magic" syntax:
```bash ```bash
jc ls -l /usr/bin | jq '.[] | select(.size > 50000000)' $ jc dig example.com | jq -r '.[].answer[].data'
``` 93.184.216.34
```json
{
"filename": "docker",
"flags": "-rwxr-xr-x",
"links": 1,
"owner": "root",
"group": "root",
"size": 68677120,
"date": "Aug 14 19:41"
}
``` ```
The `jc` parsers can also be used as python modules. In this case the output will be a python dictionary, or list of dictionaries, instead of JSON: The `jc` parsers can also be used as python modules. In this case the output will be a python dictionary, or list of dictionaries, instead of JSON:
```python ```python
>>> import jc.parsers.ls >>> import jc.parsers.dig
>>> >>>
>>> data='''-rwxr-xr-x 1 root wheel 23648 May 3 22:26 cat >>> data = '''; <<>> DiG 9.10.6 <<>> example.com
... -rwxr-xr-x 1 root wheel 30016 May 3 22:26 chmod ... ;; global options: +cmd
... -rwxr-xr-x 1 root wheel 29024 May 3 22:26 cp ... ;; Got answer:
... -rwxr-xr-x 1 root wheel 375824 May 3 22:26 csh ... ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64612
... -rwxr-xr-x 1 root wheel 28608 May 3 22:26 date ... ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
... -rwxr-xr-x 1 root wheel 32000 May 3 22:26 dd ...
... -rwxr-xr-x 1 root wheel 23392 May 3 22:26 df ... ;; OPT PSEUDOSECTION:
... -rwxr-xr-x 1 root wheel 18128 May 3 22:26 echo''' ... ; EDNS: version: 0, flags:; udp: 4096
... ;; QUESTION SECTION:
... ;example.com. IN A
...
... ;; ANSWER SECTION:
... example.com. 29658 IN A 93.184.216.34
...
... ;; Query time: 52 msec
... ;; SERVER: 2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)
... ;; WHEN: Fri Apr 16 16:13:00 PDT 2021
... ;; MSG SIZE rcvd: 56'''
>>> >>>
>>> jc.parsers.ls.parse(data) >>> jc.parsers.dig.parse(data)
[{'filename': 'cat', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 23648, [{'id': 64612, 'opcode': 'QUERY', 'status': 'NOERROR', 'flags': ['qr', 'rd', 'ra'], 'query_num': 1, 'answer_num':
'date': 'May 3 22:26'}, {'filename': 'chmod', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 1, 'authority_num': 0, 'additional_num': 1, 'opt_pseudosection': {'edns': {'version': 0, 'flags': [], 'udp':
'group': 'wheel', 'size': 30016, 'date': 'May 3 22:26'}, {'filename': 'cp', 'flags': '-rwxr-xr-x', 4096}}, 'question': {'name': 'example.com.', 'class': 'IN', 'type': 'A'}, 'answer': [{'name': 'example.com.',
'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 29024, 'date': 'May 3 22:26'}, {'filename': 'csh', 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data': '93.184.216.34'}], 'query_time': 52, 'server':
'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 375824, 'date': 'May 3 '2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56,
22:26'}, {'filename': 'date', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'when_epoch': 1618614780, 'when_epoch_utc': None}]
'size': 28608, 'date': 'May 3 22:26'}, {'filename': 'dd', 'flags': '-rwxr-xr-x', 'links': 1, 'owner':
'root', 'group': 'wheel', 'size': 32000, 'date': 'May 3 22:26'}, {'filename': 'df', 'flags':
'-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 23392, 'date': 'May 3 22:26'},
{'filename': 'echo', 'flags': '-rwxr-xr-x', 'links': 1, 'owner': 'root', 'group': 'wheel', 'size': 18128,
'date': 'May 3 22:26'}]
``` ```
Two representations of the data are possible. The default representation uses a strict schema per parser and converts known numbers to int/float JSON values. Certain known values of `None` are converted to JSON `null`, known boolean values are converted, and, in some cases, additional semantic context fields are added. Two representations of the data are possible. The default representation uses a strict schema per parser and converts known numbers to int/float JSON values. Certain known values of `None` are converted to JSON `null`, known boolean values are converted, and, in some cases, additional semantic context fields are added.