1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2026-04-03 17:44:07 +02:00

Compare commits

..

158 Commits

Author SHA1 Message Date
Kelly Brazil
ab482e521d Merge pull request #163 from kellyjonbrazil/dev
Dev v1.16.2
2021-08-31 09:50:38 -07:00
Kelly Brazil
e08b61fa81 add schema note to sfdisk 2021-08-31 08:54:23 -07:00
Kelly Brazil
ce61bd1d2b add tests for -F fixes 2021-08-31 08:50:42 -07:00
Kelly Brazil
7b708f7518 Don't convert 'size' to int, except for legacy -d support. Change partition table detection logic to fix -F output 2021-08-31 08:39:54 -07:00
Kelly Brazil
89ca50c7fc add -l to sfdisk raw example 2021-08-30 21:56:36 -07:00
Kelly Brazil
fb54899dcc doc update 2021-08-30 21:53:40 -07:00
Kelly Brazil
0a625ad7dd remove unneeded lines 2021-08-30 21:52:55 -07:00
Kelly Brazil
d32e45efbe add sfdisk tests 2021-08-30 21:49:07 -07:00
Kelly Brazil
c77696bc78 version bump 2021-08-30 20:55:26 -07:00
Kelly Brazil
736fde9e78 add support for newer versions of sfdisk 2021-08-30 20:51:19 -07:00
Kelly Brazil
9c1ad92fed fix examples 2021-08-26 10:31:51 -07:00
Kelly Brazil
1a9fd2139d add "For new parsers:" 2021-08-26 10:29:13 -07:00
Kelly Brazil
7661e7f27a formatting 2021-08-26 10:27:11 -07:00
Kelly Brazil
f857b7fbf7 add custom parser folder info 2021-08-26 10:25:37 -07:00
Kelly Brazil
d94d12dbc5 add foo parser template info 2021-08-26 10:20:53 -07:00
Kelly Brazil
700916276a Merge pull request #159 from kellyjonbrazil/master
Sync Master to dev
2021-08-26 10:01:08 -07:00
Kelly Brazil
834e52369c update man page 2021-08-16 10:33:26 -07:00
Kelly Brazil
1ce53365de Merge pull request #156 from kellyjonbrazil/dev
Dev v1.16.1
2021-08-16 10:32:03 -07:00
Kelly Brazil
473f70668f Merge branch 'master' into dev
# Conflicts:
#	jc/man/jc.1.gz
#	man/jc.1
#	man/jc.1.gz
2021-08-16 10:28:11 -07:00
Kelly Brazil
0dbd2702f6 reformat doc_text for nicer indentation 2021-08-13 17:17:51 -07:00
Kelly Brazil
01e3764a9b fix join syntax 2021-08-13 16:03:46 -07:00
Kelly Brazil
ff9c81722a try/except for shlex.join since only available in python 3.8 2021-08-13 16:01:45 -07:00
Kelly Brazil
166aef7a02 version bump 2021-08-13 15:22:33 -07:00
Kelly Brazil
78caf7646b add stat fix 2021-08-13 15:19:41 -07:00
Kelly Brazil
1f99d40cec fix filename with spaces for osx/bsd 2021-08-13 15:19:31 -07:00
Kelly Brazil
4c2912d3d5 use shlex.join to quote run_command_str for better error messages 2021-08-13 15:19:17 -07:00
Kelly Brazil
45e6e06be5 add glob and process substitution fixes 2021-08-13 14:19:45 -07:00
Kelly Brazil
fdbe3e05f3 fix issue with globs not including filenames with spaces with magic syntax (introduced during switch to use subprocess to grab process exit code). No longer need to shlex quote arguments. 2021-08-13 14:19:26 -07:00
Kelly Brazil
7cc168f640 add comment 2021-08-12 16:55:07 -07:00
Kelly Brazil
ff2d609c9b add close_fds=False in subprocess.Popen() to allow process substitution in magic syntax. Also check for too many open files exception 2021-08-12 16:36:35 -07:00
Kelly Brazil
2689697b4c formatting 2021-07-22 17:03:00 -07:00
Kelly Brazil
f90a0ea8ab formatting 2021-07-22 12:20:48 -07:00
Kelly Brazil
caabe60f84 fix kv example 2021-07-22 12:20:31 -07:00
Kelly Brazil
2bef4ed603 package updates 2021-07-21 08:47:16 -07:00
Kelly Brazil
ee57be533b fix sfdisk example 2021-07-20 12:35:02 -05:00
Kelly Brazil
c5b7aaca25 Merge pull request #149 from kellyjonbrazil/dev
Dev v1.16.0
2021-07-20 10:19:14 -07:00
Kelly Brazil
7a1be905bb version bump 2021-07-20 12:15:29 -05:00
Kelly Brazil
5798495a11 add 1.15.7 to 1.16.0 changes 2021-07-20 12:13:33 -05:00
Kelly Brazil
46171e2202 add sfdisk example 2021-07-20 12:13:14 -05:00
Kelly Brazil
dd5c924ff5 add man page location change 2021-07-19 12:13:46 -07:00
Kelly Brazil
30c4ab2976 new man page location 2021-07-19 12:08:50 -07:00
Kelly Brazil
26ea4d47b3 version bump and deprecate /man/jc.1.gz and /jc/man/jc.1.gz 2021-07-19 12:04:25 -07:00
Kelly Brazil
2732cd175c include CHANGELOG in source distribution 2021-07-12 08:10:18 -07:00
Kelly Brazil
3e54b597be add timezone change for unit tests 2021-07-07 09:00:19 -07:00
Kelly Brazil
f10ebea209 update tests to set correct timezone on POSIX systems 2021-07-07 08:53:52 -07:00
Kelly Brazil
2c6f3993cb remove extra comma 2021-07-01 15:53:10 -07:00
Kelly Brazil
708a696920 remove extra lines 2021-07-01 15:51:38 -07:00
Kelly Brazil
20bbb5d331 remove extra lines 2021-07-01 15:50:55 -07:00
Kelly Brazil
223e785b54 rename variable 2021-06-30 17:07:43 -07:00
Kelly Brazil
3d78692c59 add sfdisk tests 2021-06-30 16:54:34 -07:00
Kelly Brazil
5321a15dcf update examples 2021-06-30 14:30:35 -07:00
Kelly Brazil
a452f8252a fix integer conversion 2021-06-30 14:27:12 -07:00
Kelly Brazil
49267f09ac change to root prompt in examples 2021-06-30 14:06:15 -07:00
Kelly Brazil
db47f35783 add working sfdisk parser 2021-06-30 12:38:36 -07:00
Kelly Brazil
d48abf312c Merge pull request #147 from kellyjonbrazil/master
sync to dev
2021-06-30 10:10:59 -07:00
Kelly Brazil
3b22ce4110 fix local plugin parser issue where parser has .py in the name but it is not at the end 2021-06-28 08:04:46 -07:00
Kelly Brazil
c521ca5bc9 change possible to available 2021-06-11 11:41:16 -07:00
Kelly Brazil
3ddc1c6659 add jello 2021-06-11 11:39:38 -07:00
Kelly Brazil
a8e19402b7 remove temporary fix 2021-06-09 08:33:48 -07:00
Kelly Brazil
0927902b30 temp fix for wrong url 2021-06-08 16:51:03 -07:00
Kelly Brazil
572548b42f Merge pull request #141 from kellyjonbrazil/dev
Dev v1.15.5
2021-05-27 17:00:54 -07:00
Kelly Brazil
ff7ab0a1ed doc updates for v1.15.5 2021-05-27 16:58:14 -07:00
Kelly Brazil
5db71b05cb add en_US.UTF-8 to LANG info 2021-05-25 09:47:24 -07:00
Kelly Brazil
f9b952885a add python 3.9.5 packaged info 2021-05-21 10:21:21 -07:00
Kelly Brazil
e7983bc0b2 version bump 2021-05-21 10:16:19 -07:00
Kelly Brazil
473a643142 add windows msi info 2021-05-21 10:16:08 -07:00
Kelly Brazil
14f3d442cb formatting 2021-05-21 09:46:51 -07:00
Kelly Brazil
d6f4ed9ab5 add long-form UTC test 2021-05-21 09:46:34 -07:00
Kelly Brazil
1b8d654444 add error message info 2021-05-20 15:49:18 -07:00
Kelly Brazil
6002af0dca add more error message detail to the user for ParseError and LibraryNotFound exceptions 2021-05-20 15:46:31 -07:00
Kelly Brazil
0924d822a3 add windows example 2021-05-20 15:36:27 -07:00
Kelly Brazil
72a37b9289 version bump 2021-05-19 16:23:34 -07:00
Kelly Brazil
5eef7bd769 use LibraryNotInstalled exception instead of exiting via sys.exit 2021-05-19 16:14:26 -07:00
Kelly Brazil
c6893e1bd5 add LibraryNotInstalled exception 2021-05-19 16:13:05 -07:00
Kelly Brazil
039f6612e4 Merge pull request #139 from kellyjonbrazil/master
sync branches
2021-05-18 10:29:02 -07:00
Kelly Brazil
95aec9c6f9 add info about Magic syntax not supporting shell builtins 2021-05-18 10:27:36 -07:00
Kelly Brazil
4e9652a8ec spelling 2021-05-17 14:36:20 -07:00
Kelly Brazil
773b7f4b1f add space before exit code table 2021-05-17 08:38:09 -07:00
Kelly Brazil
d17ffde9cd Merge pull request #138 from kellyjonbrazil/dev
Dev v1.15.4
2021-05-17 08:35:13 -07:00
Kelly Brazil
9c57c09c00 doc update for release 2021-05-17 08:24:31 -07:00
Kelly Brazil
4d730a9de5 add UTC fix 2021-05-16 20:57:17 -07:00
Kelly Brazil
fc57bcfce2 fix for when UTC is referenced as "Coordinated Universal Time" 2021-05-16 20:51:39 -07:00
Kelly Brazil
fa5571486c simplify json_out function 2021-05-16 20:33:03 -07:00
Kelly Brazil
9996c4fe23 update docs for shell builtins 2021-05-16 19:55:53 -07:00
Kelly Brazil
038d429024 doc updates 2021-05-16 19:44:10 -07:00
Kelly Brazil
9bf6facb0d remove magic command capability since dir is a shell builtin 2021-05-16 19:43:10 -07:00
Kelly Brazil
965717886e add exceptions module info 2021-05-16 19:30:00 -07:00
Kelly Brazil
e9bfc3dd29 add time format, dig compatibility, windows colors fix 2021-05-16 19:21:37 -07:00
Kelly Brazil
f46b33eacf add windows time format 2021-05-16 19:20:13 -07:00
Kelly Brazil
f475fe44df add new time format for systeminfo 2021-05-16 19:09:53 -07:00
Kelly Brazil
5fdbe2962d make dig compatible with all platforms 2021-05-16 17:48:56 -07:00
Kelly Brazil
ab291b9eef only force enable colors when running on windows 2021-05-16 17:48:40 -07:00
Kelly Brazil
fd411fd772 attempt to get colors working on windows 2021-05-16 17:43:05 -07:00
Kelly Brazil
b1e95a60a2 remove unnecessary comment 2021-05-13 08:42:27 -07:00
Kelly Brazil
bb1439f0d5 use ParseError exception from jc.exceptions module 2021-05-13 08:20:58 -07:00
Kelly Brazil
ba963d98a0 add exceptions module including ParseError 2021-05-13 08:20:35 -07:00
Kelly Brazil
83440ccb55 error message capitalization 2021-05-13 08:02:38 -07:00
Kelly Brazil
796f61bfa4 handle case where the user pipes data and uses magic syntax simultaneously 2021-05-12 17:01:09 -07:00
Kelly Brazil
070cac4ae1 remove commented line 2021-05-12 15:36:06 -07:00
Kelly Brazil
3ed84f9f42 reorganize main function. remove pass condition. 2021-05-12 15:19:11 -07:00
Kelly Brazil
a205afb6f3 set run_command_str if run_command is set 2021-05-12 13:44:15 -07:00
Kelly Brazil
a6d983dd8f move run_command_str variable 2021-05-12 13:38:08 -07:00
Kelly Brazil
b6c8d6d01d add exception handling for filenotfound or other subprocess.popen and json.dumps exceptions 2021-05-12 13:18:58 -07:00
Kelly Brazil
b5a5d5b133 set parser_name for magic syntax use 2021-05-12 11:36:27 -07:00
Kelly Brazil
da528e7814 move separators to a variable 2021-05-12 09:40:22 -07:00
Kelly Brazil
4acebf4f62 move variables 2021-05-12 08:48:49 -07:00
Kelly Brazil
4d40808d2b update comments 2021-05-12 08:27:39 -07:00
Kelly Brazil
c543f00bd3 simplify piped_output function 2021-05-11 14:30:46 -07:00
Kelly Brazil
947cf41dfa add exit code info 2021-05-11 12:42:21 -07:00
Kelly Brazil
8d8c58742e formatting 2021-05-11 11:32:08 -07:00
Kelly Brazil
995ecc9bfb add exit codes section 2021-05-11 11:11:17 -07:00
Kelly Brazil
21a15225eb add exit codes section 2021-05-11 10:59:26 -07:00
Kelly Brazil
48921d4584 ensure exit code never exceeds 255 2021-05-11 10:50:35 -07:00
Kelly Brazil
342db45edc fix combined exit codes 2021-05-11 10:36:55 -07:00
Kelly Brazil
5f88ecf844 add comments to magic_parser return 2021-05-10 21:23:23 -07:00
Kelly Brazil
a56aebfe70 remove side-effect comment since it is no longer relevant 2021-05-10 21:09:00 -07:00
Kelly Brazil
422bb744a8 update man pages 2021-05-10 21:03:30 -07:00
Kelly Brazil
c3b814a15f move imports to the top 2021-05-10 21:02:28 -07:00
Kelly Brazil
e4574047a0 update tests for magic_parser function 2021-05-10 20:49:44 -07:00
Kelly Brazil
0d7d7951f8 don't reset sys.argv anymore. check for 'valid_command' instead 2021-05-10 18:58:45 -07:00
Kelly Brazil
da904e4770 remove final \n from stderr string when printing 2021-05-10 18:50:59 -07:00
Kelly Brazil
19b540041a proof of concept for passing command exit codes when using magic syntax. Needs more testing 2021-05-10 18:31:30 -07:00
Kelly Brazil
f2ffb93eea formatting 2021-05-10 10:43:40 -07:00
Kelly Brazil
c0c0e05642 add dig +noall +answer support 2021-05-10 10:40:13 -07:00
Kelly Brazil
966978f17e add more unparsable line ping tests 2021-05-10 10:39:51 -07:00
Kelly Brazil
8ab08a5231 doc update 2021-05-10 10:13:52 -07:00
Kelly Brazil
48e534fa03 add +noall +answer test 2021-05-07 16:55:18 -07:00
Kelly Brazil
61851c1bd0 add support for +noall +answer 2021-05-07 16:42:09 -07:00
Kelly Brazil
3c51b2d83d add tests for unparsable lines on linux 2021-05-07 13:50:28 -07:00
Kelly Brazil
ee3a28528e Merge branch 'dev' of https://github.com/kellyjonbrazil/jc into dev
# Conflicts:
#	templates/readme_template
2021-05-05 10:03:58 -07:00
Kelly Brazil
916bcdae38 Merge pull request #136 from kellyjonbrazil/master
merge changes from master
2021-05-05 10:02:25 -07:00
Kelly Brazil
efb1d3e6b2 add blog title and minor formatting 2021-05-05 09:23:21 -07:00
Kelly Brazil
4e6ae66bac formatting 2021-05-05 09:20:47 -07:00
Kelly Brazil
5ee88e7b67 add use cases section 2021-05-05 09:18:45 -07:00
Kelly Brazil
c3b68903cb add use cases section 2021-05-05 09:17:11 -07:00
Kelly Brazil
fe1f1013a7 add use cases section 2021-05-05 09:16:05 -07:00
Kelly Brazil
fb14f5439f fix and tests for unknown or unparsable errors 2021-05-05 08:03:27 -07:00
Kelly Brazil
5ca0fc364e add unparsed_line to docs 2021-05-04 19:05:20 -07:00
Kelly Brazil
a1fe7037e5 add unparsed_line field if line cannot be parsed 2021-05-04 19:04:16 -07:00
Kelly Brazil
c2af7d113e add ping updates 2021-05-04 15:35:47 -07:00
Kelly Brazil
ff034e401d use try/except to make parser more resilient against unknown error types 2021-05-04 15:34:45 -07:00
Kelly Brazil
5abe095beb update ping docs 2021-05-03 15:26:16 -07:00
Kelly Brazil
33de5f01e6 version bump 2021-05-03 15:16:47 -07:00
Kelly Brazil
8ce155d843 add support for error replies in v4 ping on osx and bsd 2021-05-03 15:16:33 -07:00
Kelly Brazil
b921d5ec95 initial support for error replies in bsd/osx 2021-04-30 16:53:52 -07:00
Kelly Brazil
e21542aaa2 Merge pull request #135 from kellyjonbrazil/dev
Dev clarify packaged binaries in readme
2021-04-30 10:31:55 -07:00
Kelly Brazil
6150aae0ae clarify packaged binaries 2021-04-30 10:31:02 -07:00
Kelly Brazil
f27b35f371 Merge pull request #131 from kellyjonbrazil/master
sync branches
2021-04-28 15:28:35 -07:00
Kelly Brazil
d24f9a885d fix contributing link 2021-04-28 15:26:52 -07:00
Kelly Brazil
48dd82c8d1 trigger page build on github pages 2021-04-28 15:15:41 -07:00
Kelly Brazil
e57167ad1f empty commit 2021-04-28 12:23:31 -07:00
Kelly Brazil
ada9137642 Merge pull request #130 from kellyjonbrazil/dev
Dev link updates
2021-04-28 11:53:15 -07:00
Kelly Brazil
bd428a9fd7 fix link 2021-04-28 11:52:24 -07:00
Kelly Brazil
934941332f minor link updates 2021-04-28 11:50:10 -07:00
Kelly Brazil
0c209dbd10 Merge pull request #129 from kellyjonbrazil/dev
Dev update readme
2021-04-28 11:36:05 -07:00
Kelly Brazil
56e041aa26 add Practical JSON at the Command Line blog link 2021-04-28 11:34:51 -07:00
Kelly Brazil
cf9d48582e Merge pull request #128 from kellyjonbrazil/master
sync branches
2021-04-26 13:17:20 -07:00
Kelly Brazil
92e2252bee fix typo 2021-04-26 12:18:21 -07:00
95 changed files with 2628 additions and 270 deletions

View File

@@ -1,5 +1,50 @@
jc changelog
20210830 v1.16.2
- Note to Package Maintainers: please see note at 20210720 v1.16.0
- Update sfdisk parser to support the -F option and newer versions of sfdisk
20210813 v1.16.1
- Note to Package Maintainers: please see note at 20210720 v1.16.0
- Fix issue with process substitution with the magic syntax
- Fix issue with globs not including filenames with spaces with magic syntax
- Fix stat parser to properly handle filenames with spaces on macOS/BSD
20210720 v1.16.0
- Note to Package Maintainers:
TL;DR: `/man/jc.1.gz` and `/jc/man/jc.1.gz` are deprecated and only `/man/jc.1` should be used.
The Man page in the PyPi source packages will be moving from `/jc/man/jc.1.gz` to `/man/jc.1`
in version 1.17.0. For now the Man pages will be available in both locations, but be aware that
the Man page at `/jc/man/jc.1.gz` is now considered deprecated.
Also, starting in v1.17.0, the Man page will no longer be compressed in the source package,
therefore `/man/jc.1.gz` should also be considered deprecated and will no longer be available
after v1.17.0. Please use `/man/jc.1` and compress downstream if you would like.
- Include CHANGELOG in source distribution
- Fix Man page location in source packages
- Add sfdisk command parser tested on linux
- Update unit test files to change the timezone when needed (POSIX only)
- Binaries and DEB/RPM/MSI packages now include Python 3.9.5 interpreter
20210628 v1.15.6
- Fix issue to only load local plugin parsers that have filenames that end in .py
20210520 v1.15.5
- Fix issue where help and about information would not display if a 3rd party parser library was missing. (e.g. xmltodict)
- Add more error message detail when encountering ParseError and LibraryNotFound exceptions
20210517 v1.15.4
- Update ping parser to support error responses in OSX and BSD
- Update ping parser to be more resilient against parsing errors for unknown error types
- Update dig parser to support `+noall +answer` use case
- Update dig parser compatibility to all platforms
- Fix colors in Windows terminals (cmd.exe and PowerShell)
- Fix epoch calculations when UTC is referenced as "Coordinated Universal Time"
- Add Windows time format for systeminfo output
- Add exceptions module to standardize parser exceptions
- JC no longer swallows exit codes when using the "magic" syntax. See the Exit Codes section of the README and man page for details
20210426 v1.15.3
- Add ufw status command parser tested on linux
- Add ufw-appinfo command parser tested on linux

View File

@@ -15,11 +15,12 @@ Pull requests are the best way to propose changes to the codebase (we use [Githu
1. Open an issue to discuss the new feature, bug fix, or parser before opening a pull request. For new parsers, it is important to agree upon a schema before developing the parser.
2. Fork the repo and create your branch from `dev`, if available, otherwise `master`.
3. If you've added code that should be tested, add tests. All new parsers should have several sample outputs and tests.
4. Documentation is auto-generated from docstrings, so ensure they are clear and accurate.
5. Ensure the test suite passes. (Note: "**America/Los_Angeles**" timezone should be configured on the test system)
6. Make sure your code lints.
7. Issue that pull request!
3. For new parsers: Use the `jc/parsers/foo.py` parser as a [template](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py) to get started. You can even place a new parser python module file in the [parser plugin directory](https://github.com/kellyjonbrazil/jc#custom-parsers) to get started right away with just a standard `jc` installation.
4. If you've added code that should be tested, add tests. All new parsers should have several sample outputs and tests.
5. Documentation is auto-generated from docstrings, so ensure they are clear and accurate.
6. Ensure the test suite passes. (Note: "**America/Los_Angeles**" timezone should be configured on the test system)
7. Make sure your code lints.
8. Issue that pull request!
## Parser Schema Guidelines
- Try to keep the schema as flat as possible - typically a list of flat dictionaries
@@ -34,11 +35,11 @@ Bad:
```
{
"Interface 1": [
192.168.1.1,
172.16.1.1
"192.168.1.1",
"172.16.1.1"
],
"Wifi Interface 1": [
10.1.1.1
"10.1.1.1"
]
}
```
@@ -48,14 +49,14 @@ Good:
{
"interface": "Interface 1",
"ip_addresses": [
192.168.1.1,
172.16.1.1
"192.168.1.1",
"172.16.1.1"
]
},
{
"interface": "Wifi Interface 1",
"ip_addresses": [
10.1.1.1
"10.1.1.1"
]
}
]

View File

@@ -732,7 +732,7 @@ dig -x 1.1.1.1 | jc --dig -p # or: jc -p dig -x 1.1.1.1
```
### dir
```bash
dir | jc --dir -p # or: jc -p dir
dir | jc --dir -p
```
```json
[
@@ -1671,7 +1671,7 @@ iw dev wlan0 scan | jc --iw-scan -p # or: jc -p iw dev wlan0 scan
```
### jobs
```bash
jobs -l | jc --jobs -p # or: jc -p jobs
jobs -l | jc --jobs -p
```
```json
[
@@ -2556,6 +2556,75 @@ rpm_qia | jc --rpm_qi -p # or: jc -p rpm -qia
}
]
```
### sfdisk
```bash
sfdisk -l | jc --sfdisk -p # or jc -p sfdisk -l
```
```json
[
{
"disk": "/dev/sda",
"cylinders": 2610,
"heads": 255,
"sectors_per_track": 63,
"units": "cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0",
"partitions": [
{
"device": "/dev/sda1",
"boot": true,
"start": 0,
"end": 130,
"cyls": 131,
"blocks": 1048576,
"id": "83",
"system": "Linux"
},
{
"device": "/dev/sda2",
"boot": false,
"start": 130,
"end": 2610,
"cyls": 2481,
"blocks": 19921920,
"id": "8e",
"system": "Linux LVM"
},
{
"device": "/dev/sda3",
"boot": false,
"start": 0,
"end": null,
"cyls": 0,
"blocks": 0,
"id": "0",
"system": "Empty"
},
{
"device": "/dev/sda4",
"boot": false,
"start": 0,
"end": null,
"cyls": 0,
"blocks": 0,
"id": "0",
"system": "Empty"
}
]
},
{
"disk": "/dev/mapper/centos-root",
"cylinders": 2218,
"heads": 255,
"sectors_per_track": 63
},
{
"disk": "/dev/mapper/centos-swap",
"cylinders": 261,
"heads": 255,
"sectors_per_track": 63
}
]
```
### /etc/shadow file
```bash
cat /etc/shadow | jc --shadow -p

View File

@@ -1 +1,3 @@
include jc/man/jc.1.gz
include man/jc.1
include CHANGELOG

View File

@@ -1,7 +1,7 @@
![Tests](https://github.com/kellyjonbrazil/jc/workflows/Tests/badge.svg?branch=master)
![Pypi](https://img.shields.io/pypi/v/jc.svg)
> Try the new `jc` [web demo](https://jc-web-demo.herokuapp.com/)!
> Try the `jc` [web demo](https://jc-web-demo.herokuapp.com/)
> JC is [now available](https://galaxy.ansible.com/community/general) as an Ansible filter plugin in the `community.general` collection! See this [blog post](https://blog.kellybrazil.com/2020/08/30/parsing-command-output-in-ansible-with-jc/) for an example.
@@ -19,7 +19,7 @@ dig example.com | jc --dig
39049,"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:09:00 PDT 2021","rcvd":56,"when_epoch":1618614540,"when_epoch_utc":null}]
```
This allows further command-line processing of output with tools like `jq` by piping commands:
This allows further command-line processing of output with tools like `jq` or [`jello`](https://github.com/kellyjonbrazil/jello) by piping commands:
```bash
$ dig example.com | jc --dig | jq -r '.[].answer[].data'
93.184.216.34
@@ -60,24 +60,30 @@ The `jc` parsers can also be used as python modules. In this case the output wil
'2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56,
'when_epoch': 1618614780, 'when_epoch_utc': None}]
```
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 available. 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.
To access the raw, pre-processed JSON, use the `-r` cli option or the `raw=True` function parameter in `parse()`.
Schemas for each parser can be found at the documentation link beside each parser below.
Schemas for each parser can be found at the documentation link beside each [**Parser**](#parsers) below.
Release notes can be found [here](https://blog.kellybrazil.com/category/jc-news/).
## Why Would Anyone Do This!?
For more information on the motivations for this project, please see my [blog post](https://blog.kellybrazil.com/2019/11/26/bringing-the-unix-philosophy-to-the-21st-century/).
For more information on the motivations for this project, please see my blog post on [Bringing the Unix Philosophy to the 21st Century](https://blog.kellybrazil.com/2019/11/26/bringing-the-unix-philosophy-to-the-21st-century/).
See also:
- [libxo on FreeBSD](http://juniper.github.io/libxo/libxo-manual.html)
- [powershell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7)
- [blog: linux apps should have a json flag](https://thomashunter.name/posts/2012-06-06-linux-cli-apps-should-have-a-json-flag)
Use Cases:
- [Bash scripting](https://blog.kellybrazil.com/2021/04/12/practical-json-at-the-command-line/)
- [Ansible command output parsing](https://blog.kellybrazil.com/2020/08/30/parsing-command-output-in-ansible-with-jc/)
- [Saltstack command output parsing](https://blog.kellybrazil.com/2020/09/15/parsing-command-output-in-saltstack-with-jc/)
- [Nornir command output parsing](https://blog.kellybrazil.com/2020/12/09/parsing-command-output-in-nornir-with-jc/)
## Installation
There are several ways to get `jc`. You can install via `pip`; other OS package repositories like `apt-get`, `dnf`, `zypper`, `pacman`, `nix-env`, `guix`, `brew`, or `portsnap`; via DEB/RPM packages; or by downloading the correct binary for your architecture and running it anywhere on your filesystem.
There are several ways to get `jc`. You can install via `pip`, OS package repositories, via DEB/RPM/MSI packaged binaries for linux and Windows, or by downloading the correct binary for your architecture and running it anywhere on your filesystem.
### Pip (macOS, linux, unix, Windows)
```bash
@@ -94,7 +100,7 @@ pip3 install jc
| Arch linux | `pacman -S jc` |
| NixOS linux | `nix-env -iA nixpkgs.jc` or `nix-env -iA nixos.jc` |
| Guix System linux | `guix install jc` |
| MacOS | `brew install jc` |
| macOS | `brew install jc` |
| FreeBSD | `portsnap fetch update && cd /usr/ports/textproc/py-jc && make install clean` |
| Ansible filter plugin | `ansible-galaxy collection install community.general` |
@@ -163,6 +169,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio
- `--ps` enables the `ps` command parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/ps))
- `--route` enables the `route` command parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/route))
- `--rpm-qi` enables the `rpm -qi` command parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/rpm_qi))
- `--sfdisk` enables the `sfdisk` command parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/sfdisk))
- `--shadow` enables the `/etc/shadow` file parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/shadow))
- `--ss` enables the `ss` command parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/ss))
- `--stat` enables the `stat` command parser ([documentation](https://kellyjonbrazil.github.io/jc/docs/parsers/stat))
@@ -197,6 +204,19 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio
- `-r` raw output. Provides a more literal JSON output, typically with string values and no additional semantic processing
- `-v` version information
### Exit Codes
Any fatal errors within `jc` will generate an exit code of `100`, otherwise the exit code will be `0`. When using the "magic" syntax (e.g. `jc ifconfig eth0`), `jc` will store the exit code of the program being parsed and add it to the `jc` exit code. This way it is easier to determine if an error was from the parsed program or `jc`.
Consider the following examples using `ifconfig`:
| `ifconfig` exit code | `jc` exit code | Combined exit code | Interpretation |
|----------------------|----------------|--------------------|------------------------------------|
| `0` | `0` | `0` | No errors |
| `1` | `0` | `1` | Error in `ifconfig` |
| `0` | `100` | `100` | Error in `jc` |
| `1` | `100` | `101` | Error in both `ifconfig` and `jc` |
### Setting Custom Colors via Environment Variable
You can specify custom colors via the `JC_COLORS` environment variable. The `JC_COLORS` environment variable takes four comma separated string values in the following format:
```bash
@@ -222,14 +242,14 @@ Custom local parser plugins may be placed in a `jc/jcparsers` folder in your loc
Local parser plugins are standard python module files. Use the [`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py) parser as a template and simply place a `.py` file in the `jcparsers` subfolder.
Local plugin filenames must be valid python module names, therefore must consist entirely of alphanumerics and start with a letter. Local plugins may override default plugins.
Local plugin filenames must be valid python module names, therefore must consist entirely of alphanumerics and start with a letter. Local plugins may override default parsers.
> Note: The application data directory follows the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
### Caveats
**Locale:**
For best results set the `LANG` locale environment variable to `C`. For example, either by setting directly on the command-line:
For best results set the `LANG` locale environment variable to `C` or `en_US.UTF-8`. For example, either by setting directly on the command-line:
```
$ LANG=C date | jc --date
```
@@ -242,16 +262,21 @@ $ export LANG=C
Some parsers have calculated epoch timestamp fields added to the output. Unless a timestamp field name has a `_utc` suffix it is considered naive. (i.e. based on the local timezone of the system the `jc` parser was run on).
If a UTC timezone can be detected in the text of the command output, the timestamp will be timezone aware and have a `_utc`P suffix on the key name. (e.g. `epoch_utc`) No other timezones are supported for aware timestamps.
If a UTC timezone can be detected in the text of the command output, the timestamp will be timezone aware and have a `_utc` suffix on the key name. (e.g. `epoch_utc`) No other timezones are supported for aware timestamps.
## Compatibility
Some parsers like `ls`, `ps`, `dig`, etc. will work on any platform. Other parsers that are platform-specific will generate a warning message if they are used on an unsupported platform. To see all parser information, including compatibility, run `jc -ap`.
Some parsers like `dig`, `xml`, `csv`, etc. will work on any platform. Other parsers that convert platform-specific output will generate a warning message if they are run on an unsupported platform. To see all parser information, including compatibility, run `jc -ap`.
You may still use a parser on an unsupported platform - for example, you may want to parse a file with linux `lsof` output on an macOS laptop. In that case you can suppress the warning message with the `-q` cli option or the `quiet=True` function parameter in `parse()`:
You may still use a parser on an unsupported platform - for example, you may want to parse a file with linux `lsof` output on an macOS or Windows laptop. In that case you can suppress the warning message with the `-q` cli option or the `quiet=True` function parameter in `parse()`:
macOS:
```bash
cat lsof.out | jc --lsof -q
```
or Windows:
```bash
type lsof.out | jc --lsof -q
```
Tested on:
- Centos 7.7
@@ -263,6 +288,8 @@ Tested on:
- NixOS
- FreeBSD12
- Windows 10
- Windows 2016 Server
- Windows 2019 Server
## Contributions
Feel free to add/improve code or parsers! You can use the [`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py) parser as a template and submit your parser with a pull request.

View File

@@ -3,6 +3,10 @@
# jc.parsers.dig
jc - JSON CLI output utility `dig` command output parser
Options supported:
- `+noall +answer` options are supported in cases where only the answer information is desired.
- `+axfr` option is supported on its own
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 +278,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
@@ -299,6 +339,6 @@ Returns:
List of Dictionaries. Raw or processed structured data.
## Parser Information
Compatibility: linux, aix, freebsd, darwin
Compatibility: linux, aix, freebsd, darwin, win32, cygwin
Version 2.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 2.1 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@@ -9,16 +9,14 @@ Options supported:
- `/C, /-C`
- `/S`
The "Magic" syntax is not supported since the `dir` command is a shell builtin.
The `epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
Usage (cli):
C:> dir | jc --dir
or
C:> jc dir
Usage (module):
import jc.parsers.dir
@@ -145,4 +143,4 @@ Returns:
## Parser Information
Compatibility: win32
Version 1.2 by Rasheed Elsaleh (rasheed@rebelliondefense.com)
Version 1.3 by Rasheed Elsaleh (rasheed@rebelliondefense.com)

View File

@@ -5,6 +5,8 @@ jc - JSON CLI output utility `history` command output parser
This parser will output a list of dictionaries each containing `line` and `command` keys. If you would like a simple dictionary output, then use the `-r` command-line option or the `raw=True` argument in the `parse()` function.
The "Magic" syntax is not supported since the `history` command is a shell builtin.
Usage (cli):
$ history | jc --history

View File

@@ -5,14 +5,12 @@ jc - JSON CLI output utility `jobs` command output parser
Also supports the `-l` option.
The "Magic" syntax is not supported since the `jobs` command is a shell builtin.
Usage (cli):
$ jobs | jc --jobs
or
$ jc jobs
Usage (module):
import jc.parsers.jobs

View File

@@ -38,7 +38,7 @@ Examples:
# but can be preserved with the -r argument
occupation:"Engineer"
$ cat keyvalue.txt | jc --ini -p
$ cat keyvalue.txt | jc --kv -p
{
"name": "John Doe",
"address": "555 California Drive",

View File

@@ -38,14 +38,26 @@ Schema:
"round_trip_ms_stddev": float,
"responses": [
{
"type": string, # ('reply' or 'timeout')
"type": string, # 'reply', 'timeout', 'unparsable_line', etc. See `_error_type.type_map` for all options
"unparsed_line": string, # only if an 'unparsable_line' type
"timestamp": float,
"bytes": integer,
"response_ip": string,
"icmp_seq": integer,
"ttl": integer,
"time_ms": float,
"duplicate": boolean
"duplicate": boolean,
"vr": integer, # hex value converted to decimal
"hl": integer, # hex value converted to decimal
"tos": integer, # hex value converted to decimal
"len": integer, # hex value converted to decimal
"id": integer, # hex value converted to decimal
"flg": integer, # hex value converted to decimal
"off": integer, # hex value converted to decimal
"pro": integer, # hex value converted to decimal
"cks": ingeger, # hex value converted to decimal
"src": string,
"dst": string
}
]
}
@@ -169,4 +181,4 @@ Returns:
## Parser Information
Compatibility: linux, darwin, freebsd
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@@ -25,7 +25,7 @@ Schema:
"genmask": string,
"flags": string,
"flags_pretty": [
string,
string
]
"metric": integer,
"ref": integer,
@@ -76,7 +76,6 @@ Examples:
}
]
$ route -ee | jc --route -p -r
[
{
@@ -108,7 +107,6 @@ Examples:
]
## info
```python
info()

228
docs/parsers/sfdisk.md Normal file
View File

@@ -0,0 +1,228 @@
[Home](https://kellyjonbrazil.github.io/jc/)
# jc.parsers.sfdisk
jc - JSON CLI output utility `sfdisk` command output parser
Supports the following `sfdisk` options:
- `-l`
- `-F`
- `-d` (deprecated - only for older versions of util-linux)
- `-uM` (deprecated - only for older versions of util-linux)
- `-uC` (deprecated - only for older versions of util-linux)
- `-uS` (deprecated - only for older versions of util-linux)
- `-uB` (deprecated - only for older versions of util-linux)
Usage (cli):
# sfdisk -l | jc --sfdisk
or
# jc sfdisk -l
Usage (module):
import jc.parsers.sfdisk
result = jc.parsers.sfdisk.parse(sfdisk_command_output)
Schema:
[
{
"disk": string,
"disk_size": string,
"free_disk_size": string,
"bytes": integer,
"free_bytes": integer,
"sectors": integer,
"free_sectors": integer,
"cylinders": integer,
"heads": integer,
"sectors_per_track": integer,
"units": string,
"logical_sector_size": integer,
"physical_sector_size": integer,
"min_io_size": integer,
"optimal_io_size": integer,
"disk_label_type": string,
"disk_identifier": string,
"disk_model": string,
"partitions": [
{
"device": string,
"boot": boolean,
"start": integer,
"end": integer,
"size": string, # Note: will be integer when using deprecated -d sfdisk option
"cyls": integer,
"mib": integer,
"blocks": integer,
"sectors": integer,
"id": string,
"system": string,
"type": string
}
]
}
]
Examples:
# sfdisk -l | jc --sfdisk -p
[
{
"disk": "/dev/sda",
"cylinders": 2610,
"heads": 255,
"sectors_per_track": 63,
"units": "cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0",
"partitions": [
{
"device": "/dev/sda1",
"boot": true,
"start": 0,
"end": 130,
"cyls": 131,
"blocks": 1048576,
"id": "83",
"system": "Linux"
},
{
"device": "/dev/sda2",
"boot": false,
"start": 130,
"end": 2610,
"cyls": 2481,
"blocks": 19921920,
"id": "8e",
"system": "Linux LVM"
},
{
"device": "/dev/sda3",
"boot": false,
"start": 0,
"end": null,
"cyls": 0,
"blocks": 0,
"id": "0",
"system": "Empty"
},
{
"device": "/dev/sda4",
"boot": false,
"start": 0,
"end": null,
"cyls": 0,
"blocks": 0,
"id": "0",
"system": "Empty"
}
]
},
{
"disk": "/dev/mapper/centos-root",
"cylinders": 2218,
"heads": 255,
"sectors_per_track": 63
},
{
"disk": "/dev/mapper/centos-swap",
"cylinders": 261,
"heads": 255,
"sectors_per_track": 63
}
]
# sfdisk -l | jc --sfdisk -p -r
[
{
"disk": "/dev/sda",
"cylinders": "2610",
"heads": "255",
"sectors_per_track": "63",
"units": "cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0",
"partitions": [
{
"device": "/dev/sda1",
"boot": "*",
"start": "0+",
"end": "130-",
"cyls": "131-",
"blocks": "1048576",
"id": "83",
"system": "Linux"
},
{
"device": "/dev/sda2",
"boot": null,
"start": "130+",
"end": "2610-",
"cyls": "2481-",
"blocks": "19921920",
"id": "8e",
"system": "Linux LVM"
},
{
"device": "/dev/sda3",
"boot": null,
"start": "0",
"end": "-",
"cyls": "0",
"blocks": "0",
"id": "0",
"system": "Empty"
},
{
"device": "/dev/sda4",
"boot": null,
"start": "0",
"end": "-",
"cyls": "0",
"blocks": "0",
"id": "0",
"system": "Empty"
}
]
},
{
"disk": "/dev/mapper/centos-root",
"cylinders": "2218",
"heads": "255",
"sectors_per_track": "63"
},
{
"disk": "/dev/mapper/centos-swap",
"cylinders": "261",
"heads": "255",
"sectors_per_track": "63"
}
]
## info
```python
info()
```
Provides parser metadata (version, author, etc.)
## parse
```python
parse(data, raw=False, quiet=False)
```
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
Returns:
List of Dictionaries. Raw or processed structured data.
## Parser Information
Compatibility: linux
Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@@ -193,4 +193,4 @@ Returns:
## Parser Information
Compatibility: linux, darwin, freebsd
Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@@ -95,4 +95,4 @@ Returns:
## Parser Information
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@@ -109,4 +109,4 @@ Returns:
## Parser Information
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@@ -70,7 +70,7 @@ Module Example:
...
... ;; 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
@@ -86,4 +86,4 @@ Module Example:
"""
name = 'jc'
__version__ = '1.15.3'
__version__ = '1.16.2'

278
jc/cli.py
View File

@@ -6,13 +6,18 @@ import sys
import os
import os.path
import re
import shlex
import importlib
import textwrap
import signal
import shlex
import subprocess
import json
import jc
import jc.appdirs as appdirs
import jc.utils
import jc.tracebackplus
from jc.exceptions import LibraryNotInstalled, ParseError
# make pygments import optional
try:
import pygments
@@ -89,6 +94,7 @@ parsers = [
'ps',
'route',
'rpm-qi',
'sfdisk',
'shadow',
'ss',
'stat',
@@ -114,6 +120,9 @@ parsers = [
'yaml'
]
JC_ERROR_EXIT = 100
# List of custom or override parsers.
# Allow any <user_data_dir>/jc/jcparsers/*.py
local_parsers = []
@@ -122,7 +131,7 @@ local_parsers_dir = os.path.join(data_dir, 'jcparsers')
if os.path.isdir(local_parsers_dir):
sys.path.append(data_dir)
for name in os.listdir(local_parsers_dir):
if re.match(r'\w+\.py', name) and os.path.isfile(os.path.join(local_parsers_dir, name)):
if re.match(r'\w+\.py$', name) and os.path.isfile(os.path.join(local_parsers_dir, name)):
plugin_name = name[0:-3]
local_parsers.append(plugin_name)
if plugin_name not in parsers:
@@ -186,10 +195,9 @@ def set_env_colors(env_colors=None):
Default colors:
JC_COLORS=blue,brightblack,magenta,green
JC_COLORS=blue,brightblack,magenta,green
or
JC_COLORS=default,default,default,default
JC_COLORS=default,default,default,default
"""
input_error = False
@@ -207,7 +215,7 @@ def set_env_colors(env_colors=None):
# if there is an issue with the env variable, just set all colors to default and move on
if input_error:
jc.utils.warning_message('could not parse JC_COLORS environment variable')
jc.utils.warning_message('Could not parse JC_COLORS environment variable')
color_list = ['default', 'default', 'default', 'default']
# Try the color set in the JC_COLORS env variable first. If it is set to default, then fall back to default colors
@@ -221,15 +229,12 @@ def set_env_colors(env_colors=None):
def piped_output():
"""Return False if stdout is a TTY. True if output is being piped to another program"""
if sys.stdout.isatty():
return False
else:
return True
return False if sys.stdout.isatty() else True
def ctrlc(signum, frame):
"""Exit with error on SIGINT"""
sys.exit(1)
sys.exit(JC_ERROR_EXIT)
def parser_shortname(parser_argument):
@@ -355,11 +360,10 @@ def help_doc(options):
# load parser module just in time so we don't need to load all modules
parser = parser_module(arg)
compatible = ', '.join(parser.info.compatible)
doc_text = f'''{parser.__doc__}
Compatibility: {compatible}
Version {parser.info.version} by {parser.info.author} ({parser.info.author_email})
'''
doc_text = \
f'{parser.__doc__}\n'\
f'Compatibility: {compatible}\n\n'\
f'Version {parser.info.version} by {parser.info.author} ({parser.info.author_email})\n'
return doc_text
@@ -377,43 +381,47 @@ def versiontext():
def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
"""Return a JSON formatted string. String may include color codes or be pretty printed."""
separators = (',', ':')
indent = None
if pretty:
separators = None
indent = 2
if not mono and not piped_out:
# set colors
class JcStyle(Style):
styles = set_env_colors(env_colors)
if pretty:
return str(highlight(json.dumps(data, indent=2, ensure_ascii=False),
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else:
return str(highlight(json.dumps(data, separators=(',', ':'), ensure_ascii=False),
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
return str(highlight(json.dumps(data, indent=indent, separators=separators, ensure_ascii=False),
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else:
if pretty:
return json.dumps(data, indent=2, ensure_ascii=False)
else:
return json.dumps(data, separators=(',', ':'), ensure_ascii=False)
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=False)
def generate_magic_command(args):
"""
Return a tuple with a boolean and a command, where the boolean signifies that
the command is valid, and the command is either a command string or None.
def magic_parser(args):
"""
Parse command arguments for magic syntax: jc -p ls -al
# Parse with magic syntax: jc -p ls -al
Return a tuple:
valid_command (bool) is this a valid command? (exists in magic dict)
run_command (list) list of the user's command to run. None if no command.
jc_parser (str) parser to use for this user's command.
jc_options (list) list of jc options
"""
# bail immediately if there are no args or a parser is defined
if len(args) <= 1 or args[1].startswith('--'):
return False, None
return False, None, None, []
# correctly parse escape characters and spaces with shlex
args_given = ' '.join(map(shlex.quote, args[1:])).split()
args_given = args[1:]
options = []
# find the options
for arg in list(args_given):
# parser found - use standard syntax
if arg.startswith('--'):
return False, None
return False, None, None, []
# option found - populate option list
elif arg.startswith('-'):
@@ -425,11 +433,11 @@ def generate_magic_command(args):
# if -h, -a, or -v found in options, then bail out
if 'h' in options or 'a' in options or 'v' in options:
return False, None
return False, None, None, []
# all options popped and no command found - for case like 'jc -a'
# all options popped and no command found - for case like 'jc -x'
if len(args_given) == 0:
return False, None
return False, None, None, []
magic_dict = {}
parser_info = about_jc()['parsers']
@@ -446,31 +454,38 @@ def generate_magic_command(args):
# try to get a parser for two_word_command, otherwise get one for one_word_command
found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command))
# construct a new command line using the standard syntax: COMMAND | jc --PARSER -OPTIONS
run_command = ' '.join(args_given)
if found_parser:
cmd_options = ('-' + ''.join(options)) if options else ''
return True, ' '.join([run_command, '|', 'jc', found_parser, cmd_options])
else:
return False, run_command
return (
True if found_parser else False, # was a suitable parser found?
args_given, # run_command
found_parser, # the parser selected
options # jc options to preserve
)
def magic():
"""Runs the command generated by generate_magic_command() to support magic syntax"""
valid_command, run_command = generate_magic_command(sys.argv)
if valid_command:
os.system(run_command)
sys.exit(0)
elif run_command is None:
return
else:
jc.utils.error_message(f'parser not found for "{run_command}". Use "jc -h" for help.')
sys.exit(1)
def run_user_command(command):
"""Use subprocess to run the user's command. Returns the STDOUT, STDERR, and the Exit Code as a tuple."""
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=False, # Allows inheriting file descriptors. Useful for process substitution
universal_newlines=True)
stdout, stderr = proc.communicate()
return (
stdout or '\n',
stderr,
proc.returncode
)
def combined_exit_code(program_exit=0, jc_exit=0):
exit_code = program_exit + jc_exit
if exit_code > 255:
exit_code = 255
return exit_code
def main():
import jc.utils
# break on ctrl-c keyboard interrupt
signal.signal(signal.SIGINT, ctrlc)
@@ -480,17 +495,26 @@ def main():
except AttributeError:
pass
# try magic syntax first: e.g. jc -p ls -al
magic()
# enable colors for Windows cmd.exe terminal
if sys.platform.startswith('win32'):
os.system('')
# parse magic syntax first: e.g. jc -p ls -al
magic_options = []
valid_command, run_command, magic_found_parser, magic_options = magic_parser(sys.argv)
# set colors
jc_colors = os.getenv('JC_COLORS')
# set options
options = []
options.extend(magic_options)
# options
for opt in sys.argv:
if opt.startswith('-') and not opt.startswith('--'):
options.extend(opt[1:])
# find options if magic_parser did not find a command
if not valid_command:
for opt in sys.argv:
if opt.startswith('-') and not opt.startswith('--'):
options.extend(opt[1:])
about = 'a' in options
debug = 'd' in options
@@ -502,6 +526,9 @@ def main():
raw = 'r' in options
version_info = 'v' in options
if verbose_debug:
jc.tracebackplus.enable(context=11)
if not pygments_installed:
mono = True
@@ -517,44 +544,111 @@ def main():
print(versiontext())
sys.exit(0)
if verbose_debug:
import jc.tracebackplus
jc.tracebackplus.enable(context=11)
# if magic syntax used, try to run the command and error if it's not found, etc.
magic_stdout, magic_stderr, magic_exit_code = None, None, 0
if run_command:
try:
run_command_str = shlex.join(run_command) # python 3.8+
except AttributeError:
run_command_str = ' '.join(run_command) # older python versions
if sys.stdin.isatty():
jc.utils.error_message('Missing piped data. Use "jc -h" for help.')
sys.exit(1)
if valid_command:
try:
magic_stdout, magic_stderr, magic_exit_code = run_user_command(run_command)
if magic_stderr:
print(magic_stderr[:-1], file=sys.stderr)
data = sys.stdin.read()
except FileNotFoundError:
if debug:
raise
else:
jc.utils.error_message(f'"{run_command_str}" command could not be found. For details use the -d or -dd option.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
found = False
except OSError:
if debug:
raise
else:
jc.utils.error_message(f'"{run_command_str}" command could not be run due to too many open files. For details use the -d or -dd option.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
for arg in sys.argv:
parser_name = parser_shortname(arg)
except Exception:
if debug:
raise
else:
jc.utils.error_message(f'"{run_command_str}" command could not be run. For details use the -d or -dd option.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
if parser_name in parsers:
# load parser module just in time so we don't need to load all modules
parser = parser_module(arg)
try:
result = parser.parse(data, raw=raw, quiet=quiet)
elif run_command is not None:
jc.utils.error_message(f'"{run_command_str}" cannot be used with Magic syntax. Use "jc -h" for help.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
# find the correct parser
if magic_found_parser:
parser = parser_module(magic_found_parser)
parser_name = parser_shortname(magic_found_parser)
else:
found = False
for arg in sys.argv:
parser_name = parser_shortname(arg)
if parser_name in parsers:
parser = parser_module(arg)
found = True
break
except Exception:
if debug:
raise
else:
import jc.utils
jc.utils.error_message(
f'{parser_name} parser could not parse the input data. Did you use the correct parser?\n'
' For details use the -d or -dd option. Use "jc -h" for help.')
sys.exit(1)
if not found:
jc.utils.error_message('Missing or incorrect arguments. Use "jc -h" for help.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
if not found:
jc.utils.error_message('Missing or incorrect arguments. Use "jc -h" for help.')
sys.exit(1)
# check for input errors (pipe vs magic)
if not sys.stdin.isatty() and magic_stdout:
jc.utils.error_message('Piped data and Magic syntax used simultaneously. Use "jc -h" for help.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
print(json_out(result, pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output()))
elif sys.stdin.isatty() and magic_stdout is None:
jc.utils.error_message('Missing piped data. Use "jc -h" for help.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
# parse the data
data = magic_stdout or sys.stdin.read()
try:
result = parser.parse(data, raw=raw, quiet=quiet)
except (ParseError, LibraryNotInstalled) as e:
if debug:
raise
else:
jc.utils.error_message(
f'Parser issue with {parser_name}:\n'
f' {e}\n'
' For details use the -d or -dd option. Use "jc -h" for help.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
except Exception:
if debug:
raise
else:
jc.utils.error_message(
f'{parser_name} parser could not parse the input data. Did you use the correct parser?\n'
' For details use the -d or -dd option. Use "jc -h" for help.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
# output the json
try:
print(json_out(result, pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output()))
sys.exit(combined_exit_code(magic_exit_code, 0))
except Exception:
if debug:
raise
else:
jc.utils.error_message(
'There was an issue generating the JSON output.\n'
' For details use the -d or -dd option.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
if __name__ == '__main__':

9
jc/exceptions.py Normal file
View File

@@ -0,0 +1,9 @@
"""jc - JSON CLI output utility exceptions"""
class ParseError(Exception):
pass
class LibraryNotInstalled(Exception):
pass

Binary file not shown.

View File

@@ -1,5 +1,9 @@
"""jc - JSON CLI output utility `dig` command output parser
Options supported:
- `+noall +answer` options are supported in cases where only the answer information is desired.
- `+axfr` option is supported on its own
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,19 +274,55 @@ 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'
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
compatible = ['linux', 'aix', 'freebsd', 'darwin']
compatible = ['linux', 'aix', 'freebsd', 'darwin', 'win32', 'cygwin']
magic_commands = ['dig']
@@ -500,6 +540,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 +622,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

View File

@@ -6,16 +6,14 @@ Options supported:
- `/C, /-C`
- `/S`
The "Magic" syntax is not supported since the `dir` command is a shell builtin.
The `epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
Usage (cli):
C:> dir | jc --dir
or
C:> jc dir
Usage (module):
import jc.parsers.dir
@@ -121,14 +119,13 @@ import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.2'
version = '1.3'
description = '`dir` command parser'
author = 'Rasheed Elsaleh'
author_email = 'rasheed@rebelliondefense.com'
# compatible options: win32
compatible = ['win32']
magic_commands = ['dir']
__version__ = info.version

View File

@@ -2,6 +2,8 @@
This parser will output a list of dictionaries each containing `line` and `command` keys. If you would like a simple dictionary output, then use the `-r` command-line option or the `raw=True` argument in the `parse()` function.
The "Magic" syntax is not supported since the `history` command is a shell builtin.
Usage (cli):
$ history | jc --history

View File

@@ -2,14 +2,12 @@
Also supports the `-l` option.
The "Magic" syntax is not supported since the `jobs` command is a shell builtin.
Usage (cli):
$ jobs | jc --jobs
or
$ jc jobs
Usage (module):
import jc.parsers.jobs

View File

@@ -35,7 +35,7 @@ Examples:
# but can be preserved with the -r argument
occupation:"Engineer"
$ cat keyvalue.txt | jc --ini -p
$ cat keyvalue.txt | jc --kv -p
{
"name": "John Doe",
"address": "555 California Drive",

View File

@@ -35,14 +35,26 @@ Schema:
"round_trip_ms_stddev": float,
"responses": [
{
"type": string, # ('reply' or 'timeout')
"type": string, # 'reply', 'timeout', 'unparsable_line', etc. See `_error_type.type_map` for all options
"unparsed_line": string, # only if an 'unparsable_line' type
"timestamp": float,
"bytes": integer,
"response_ip": string,
"icmp_seq": integer,
"ttl": integer,
"time_ms": float,
"duplicate": boolean
"duplicate": boolean,
"vr": integer, # hex value converted to decimal
"hl": integer, # hex value converted to decimal
"tos": integer, # hex value converted to decimal
"len": integer, # hex value converted to decimal
"id": integer, # hex value converted to decimal
"flg": integer, # hex value converted to decimal
"off": integer, # hex value converted to decimal
"pro": integer, # hex value converted to decimal
"cks": ingeger, # hex value converted to decimal
"src": string,
"dst": string
}
]
}
@@ -140,12 +152,13 @@ Examples:
}
"""
import string
import ipaddress
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.4'
version = '1.5'
description = '`ping` and `ping6` command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@@ -170,7 +183,8 @@ def _process(proc_data):
Dictionary. Structured data to conform to the schema.
"""
int_list = ['data_bytes', 'packets_transmitted', 'packets_received', 'bytes', 'icmp_seq', 'ttl', 'duplicates']
int_list = ['data_bytes', 'packets_transmitted', 'packets_received', 'bytes', 'icmp_seq', 'ttl',
'duplicates', 'vr', 'hl', 'tos', 'len', 'id', 'flg', 'off', 'pro', 'cks']
float_list = ['packet_loss_percent', 'round_trip_ms_min', 'round_trip_ms_avg', 'round_trip_ms_max',
'round_trip_ms_stddev', 'timestamp', 'time_ms']
@@ -192,6 +206,54 @@ def _process(proc_data):
return proc_data
def _ipv6_in(line):
line_list = line.replace('(', ' ').replace(')', ' ').replace(',', ' ').replace('%', ' ').split()
ipv6 = False
for item in line_list:
try:
_ = ipaddress.IPv6Address(item)
ipv6 = True
except Exception:
pass
return ipv6
def _error_type(line):
# from https://github.com/dgibson/iputils/blob/master/ping.c
# https://android.googlesource.com/platform/external/ping/+/8fc3c91cf9e7f87bc20b9e6d3ea2982d87b70d9a/ping.c
# https://opensource.apple.com/source/network_cmds/network_cmds-328/ping.tproj/ping.c
type_map = {
'Destination Net Unreachable': 'destination_net_unreachable',
'Destination Host Unreachable': 'destination_host_unreachable',
'Destination Protocol Unreachable': 'destination_protocol_unreachable',
'Destination Port Unreachable': 'destination_port_unreachable',
'Frag needed and DF set': 'frag_needed_and_df_set',
'Source Route Failed': 'source_route_failed',
'Destination Net Unknown': 'destination_net_unknown',
'Destination Host Unknown': 'destination_host_unknown',
'Source Host Isolated': 'source_host_isolated',
'Destination Net Prohibited': 'destination_net_prohibited',
'Destination Host Prohibited': 'destination_host_prohibited',
'Destination Net Unreachable for Type of Service': 'destination_net_unreachable_for_type_of_service',
'Destination Host Unreachable for Type of Service': 'destination_host_unreachable_for_type_of_service',
'Packet filtered': 'packet_filtered',
'Precedence Violation': 'precedence_violation',
'Precedence Cutoff': 'precedence_cutoff',
'Dest Unreachable, Bad Code': 'dest_unreachable_bad_code',
'Redirect Network': 'redirect_network',
'Redirect Host': 'redirect_host',
'Redirect Type of Service and Network': 'redirect_type_of_service_and_network',
'Redirect, Bad Code': 'redirect_bad_code',
'Time to live exceeded': 'time_to_live_exceeded',
'Frag reassembly time exceeded': 'frag_reassembly_time_exceeded',
'Time exceeded, Bad Code': 'time_exceeded_bad_code'
}
for err_type, code in type_map.items():
if err_type in line:
return code
def _linux_parse(data):
raw_output = {}
ping_responses = []
@@ -302,36 +364,41 @@ def _linux_parse(data):
continue
# normal responses
else:
elif ' bytes from ' in line:
try:
line = line.replace('(', ' ').replace(')', ' ').replace('=', ' ')
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)
# 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] == '[':
timestamp = True
bts, rip, iseq, t2l, tms = (bts + 1, rip + 1, iseq + 1, t2l + 1, tms + 1)
# 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
}
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
@@ -346,6 +413,7 @@ def _bsd_parse(data):
ping_responses = []
pattern = None
footer = False
ping_error = False
linedata = data.splitlines()
@@ -419,7 +487,7 @@ def _bsd_parse(data):
# ping response lines
else:
# ipv4 lines
if ',' not in line:
if not _ipv6_in(line):
# request timeout
if line.startswith('Request timeout for '):
@@ -430,9 +498,80 @@ def _bsd_parse(data):
ping_responses.append(response)
continue
# catch error responses
err = _error_type(line)
if err:
response = {
'type': err
}
try:
response['bytes'] = line.split()[0]
response['response_ip'] = line.split()[4].strip(':').strip('(').strip(')')
except Exception:
pass
ping_error = True
continue
if ping_error:
if line.startswith('Vr'):
continue
else:
error_line = line.split()
try:
response.update(
{
'vr': int(error_line[0], 16), # convert from hex to decimal
'hl': int(error_line[1], 16),
'tos': int(error_line[2], 16),
'len': int(error_line[3], 16),
'id': int(error_line[4], 16),
'flg': int(error_line[5], 16),
'off': int(error_line[6], 16),
'ttl': int(error_line[7], 16),
'pro': int(error_line[8], 16),
'cks': int(error_line[9], 16),
'src': error_line[10],
'dst': error_line[11],
}
)
except Exception:
pass
if response:
ping_responses.append(response)
ping_error = False
continue
# normal response
else:
line = line.replace(':', ' ').replace('=', ' ')
elif ' bytes from ' in line:
try:
line = line.replace(':', ' ').replace('=', ' ')
response = {
'type': 'reply',
'bytes': line.split()[0],
'response_ip': line.split()[3],
'icmp_seq': line.split()[5],
'ttl': line.split()[7],
'time_ms': line.split()[9]
}
except Exception:
response = {
'type': 'unparsable_line',
'unparsed_line': line
}
ping_responses.append(response)
continue
# ipv6 lines
elif ' bytes from ' in line:
try:
line = line.replace(',', ' ').replace('=', ' ')
response = {
'type': 'reply',
'bytes': line.split()[0],
@@ -441,20 +580,12 @@ def _bsd_parse(data):
'ttl': line.split()[7],
'time_ms': line.split()[9]
}
ping_responses.append(response)
continue
except Exception:
response = {
'type': 'unparsable_line',
'unparsed_line': line
}
# ipv6 lines
else:
line = line.replace(',', ' ').replace('=', ' ')
response = {
'type': 'reply',
'bytes': line.split()[0],
'response_ip': line.split()[3],
'icmp_seq': line.split()[5],
'ttl': line.split()[7],
'time_ms': line.split()[9]
}
ping_responses.append(response)
continue
@@ -462,8 +593,9 @@ def _bsd_parse(data):
if ping_responses:
seq_list = []
for reply in ping_responses:
seq_list.append(reply['icmp_seq'])
reply['duplicate'] = True if seq_list.count(reply['icmp_seq']) > 1 else False
if 'icmp_seq' in reply:
seq_list.append(reply['icmp_seq'])
reply['duplicate'] = True if seq_list.count(reply['icmp_seq']) > 1 else False
raw_output['responses'] = ping_responses

View File

@@ -22,7 +22,7 @@ Schema:
"genmask": string,
"flags": string,
"flags_pretty": [
string,
string
]
"metric": integer,
"ref": integer,
@@ -73,7 +73,6 @@ Examples:
}
]
$ route -ee | jc --route -p -r
[
{
@@ -103,7 +102,6 @@ Examples:
"irtt": "0"
}
]
"""
import jc.utils
import jc.parsers.universal

407
jc/parsers/sfdisk.py Normal file
View File

@@ -0,0 +1,407 @@
"""jc - JSON CLI output utility `sfdisk` command output parser
Supports the following `sfdisk` options:
- `-l`
- `-F`
- `-d` (deprecated - only for older versions of util-linux)
- `-uM` (deprecated - only for older versions of util-linux)
- `-uC` (deprecated - only for older versions of util-linux)
- `-uS` (deprecated - only for older versions of util-linux)
- `-uB` (deprecated - only for older versions of util-linux)
Usage (cli):
# sfdisk -l | jc --sfdisk
or
# jc sfdisk -l
Usage (module):
import jc.parsers.sfdisk
result = jc.parsers.sfdisk.parse(sfdisk_command_output)
Schema:
[
{
"disk": string,
"disk_size": string,
"free_disk_size": string,
"bytes": integer,
"free_bytes": integer,
"sectors": integer,
"free_sectors": integer,
"cylinders": integer,
"heads": integer,
"sectors_per_track": integer,
"units": string,
"logical_sector_size": integer,
"physical_sector_size": integer,
"min_io_size": integer,
"optimal_io_size": integer,
"disk_label_type": string,
"disk_identifier": string,
"disk_model": string,
"partitions": [
{
"device": string,
"boot": boolean,
"start": integer,
"end": integer,
"size": string, # Note: will be integer when using deprecated -d sfdisk option
"cyls": integer,
"mib": integer,
"blocks": integer,
"sectors": integer,
"id": string,
"system": string,
"type": string
}
]
}
]
Examples:
# sfdisk -l | jc --sfdisk -p
[
{
"disk": "/dev/sda",
"cylinders": 2610,
"heads": 255,
"sectors_per_track": 63,
"units": "cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0",
"partitions": [
{
"device": "/dev/sda1",
"boot": true,
"start": 0,
"end": 130,
"cyls": 131,
"blocks": 1048576,
"id": "83",
"system": "Linux"
},
{
"device": "/dev/sda2",
"boot": false,
"start": 130,
"end": 2610,
"cyls": 2481,
"blocks": 19921920,
"id": "8e",
"system": "Linux LVM"
},
{
"device": "/dev/sda3",
"boot": false,
"start": 0,
"end": null,
"cyls": 0,
"blocks": 0,
"id": "0",
"system": "Empty"
},
{
"device": "/dev/sda4",
"boot": false,
"start": 0,
"end": null,
"cyls": 0,
"blocks": 0,
"id": "0",
"system": "Empty"
}
]
},
{
"disk": "/dev/mapper/centos-root",
"cylinders": 2218,
"heads": 255,
"sectors_per_track": 63
},
{
"disk": "/dev/mapper/centos-swap",
"cylinders": 261,
"heads": 255,
"sectors_per_track": 63
}
]
# sfdisk -l | jc --sfdisk -p -r
[
{
"disk": "/dev/sda",
"cylinders": "2610",
"heads": "255",
"sectors_per_track": "63",
"units": "cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0",
"partitions": [
{
"device": "/dev/sda1",
"boot": "*",
"start": "0+",
"end": "130-",
"cyls": "131-",
"blocks": "1048576",
"id": "83",
"system": "Linux"
},
{
"device": "/dev/sda2",
"boot": null,
"start": "130+",
"end": "2610-",
"cyls": "2481-",
"blocks": "19921920",
"id": "8e",
"system": "Linux LVM"
},
{
"device": "/dev/sda3",
"boot": null,
"start": "0",
"end": "-",
"cyls": "0",
"blocks": "0",
"id": "0",
"system": "Empty"
},
{
"device": "/dev/sda4",
"boot": null,
"start": "0",
"end": "-",
"cyls": "0",
"blocks": "0",
"id": "0",
"system": "Empty"
}
]
},
{
"disk": "/dev/mapper/centos-root",
"cylinders": "2218",
"heads": "255",
"sectors_per_track": "63"
},
{
"disk": "/dev/mapper/centos-swap",
"cylinders": "261",
"heads": "255",
"sectors_per_track": "63"
}
]
"""
import jc.utils
import jc.parsers.universal
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.1'
description = '`sfdisk` command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
compatible = ['linux']
magic_commands = ['sfdisk']
__version__ = info.version
def _process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (List of Dictionaries) raw structured data to process
Returns:
List of Dictionaries. Structured to conform to the schema.
"""
int_list = ['cylinders', 'heads', 'sectors_per_track', 'start', 'end', 'cyls', 'mib',
'blocks', 'sectors', 'bytes', 'logical_sector_size', 'physical_sector_size',
'min_io_size', 'optimal_io_size', 'free_bytes', 'free_sectors']
bool_list = ['boot']
for entry in proc_data:
for key in entry:
if key in int_list:
entry[key] = jc.utils.convert_to_int(entry[key].replace('-', ''))
if 'partitions' in entry:
for p in entry['partitions']:
for key in p:
# legacy conversion for -d option
if key == 'size':
if p[key].isnumeric():
p[key] = jc.utils.convert_to_int(p[key])
# normal conversions
if key in int_list:
p[key] = jc.utils.convert_to_int(p[key].replace('-', ''))
if key in bool_list:
p[key] = jc.utils.convert_to_bool(p[key])
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
Returns:
List of Dictionaries. Raw or processed structured data.
"""
if not quiet:
jc.utils.compatibility(__name__, info.compatible)
raw_output = []
item = {}
partitions = []
option = ''
section = ''
if jc.utils.has_data(data):
for line in data.splitlines():
# deprecated - only for older versions of util-linux
if line.startswith('# partition table of'):
if item:
raw_output.append(item)
item = {}
partitions = []
option = 'd'
item['disk'] = line.split()[4]
continue
# deprecated - only for older versions of util-linux
if option == 'd':
if line.startswith('unit: '):
item['units'] = line.split()[1]
section = 'partitions'
continue
if section == 'partitions' and line:
part = {}
part['device'] = line.split()[0]
line = line.replace(',', ' ').replace('=', ' ')
part['start'] = line.split()[3]
part['size'] = line.split()[5]
part['id'] = line.split()[7]
part['boot'] = '*' if 'bootable' in line else None
partitions.append(part)
item['partitions'] = partitions
continue
else:
# older versions of util-linux
# Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track
if line.startswith('Disk ') and 'sectors/track' in line:
if item:
raw_output.append(item)
item = {}
partitions = []
line = line.replace(':', '').replace(',', '')
fields = line.split()
item['disk'] = fields[1]
item['cylinders'] = fields[2]
item['heads'] = fields[4]
item['sectors_per_track'] = fields[6]
continue
# util-linux v2.32.0+ (?)
# Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
if line.startswith('Disk ') and line.endswith('sectors'):
if item:
raw_output.append(item)
item = {}
partitions = []
line = line.replace(':', '').replace(',', '')
fields = line.split()
item['disk'] = fields[1]
item['disk_size'] = ' '.join(fields[2:4])
item['bytes'] = fields[4]
item['sectors'] = fields[6]
continue
if line.startswith('Disk model: '):
item['disk_model'] = line.split(':', maxsplit=1)[1].strip()
continue
if line.startswith('Sector size (logical/physical)'):
fields = line.split()
item['logical_sector_size'] = fields[3]
item['physical_sector_size'] = fields[6]
continue
if line.startswith('I/O size (minimum/optimal)'):
fields = line.split()
item['min_io_size'] = fields[3]
item['optimal_io_size'] = fields[6]
continue
if line.startswith('Disklabel type'):
item['disk_label_type'] = line.split(':', maxsplit=1)[1].strip()
continue
if line.startswith('Disk identifier'):
item['disk_identifier'] = line.split(':', maxsplit=1)[1].strip()
continue
if line.startswith('Units: '):
item['units'] = line.split(':')[1].strip()
continue
# sfdisk -F
if line.startswith('Unpartitioned space'):
line = line.replace(':', '').replace(',', '')
fields = line.split()
item['disk'] = fields[2]
item['free_disk_size'] = ' '.join(fields[3:5])
item['free_bytes'] = fields[5]
item['free_sectors'] = fields[7]
continue
# partition lines
if 'Start' in line and 'End' in line and ('Sectors' in line or 'Device' in line):
section = 'partitions'
partitions.append(line.lower().replace('#', ' '))
continue
if section == 'partitions' and line:
partitions.append(line)
continue
if section == 'partitions' and line == '':
item['partitions'] = jc.parsers.universal.sparse_table_parse(partitions)
section = ''
partitions = []
continue
# get final partitions if there are any left over
if section == 'partitions' and option != 'd' and partitions:
item['partitions'] = jc.parsers.universal.sparse_table_parse(partitions)
if item:
raw_output.append(item)
if raw:
return raw_output
else:
return _process(raw_output)

View File

@@ -169,7 +169,7 @@ import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.8'
version = '1.9'
description = '`stat` command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@@ -322,7 +322,7 @@ def parse(data, raw=False, quiet=False):
for line in cleandata:
value = shlex.split(line)
output_line = {
'file': value[15],
'file': ' '.join(value[15:]),
'unix_device': value[0],
'inode': value[1],
'flags': value[2],

View File

@@ -43,6 +43,7 @@ Example:
}
"""
import jc.utils
from jc.exceptions import ParseError
class info():
@@ -60,10 +61,6 @@ class info():
__version__ = info.version
class ParseError(Exception):
pass
def _process(proc_data):
"""
Final processing to conform to the schema.

View File

@@ -65,19 +65,13 @@ Examples:
...
}
"""
import sys
import jc.utils
# check if xml library is installed and fail gracefully if it is not
try:
import xmltodict
except Exception:
jc.utils.error_message('The xmltodict library is not installed.')
sys.exit(1)
from jc.exceptions import LibraryNotInstalled
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.4'
version = '1.5'
description = 'XML file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@@ -121,6 +115,12 @@ def parse(data, raw=False, quiet=False):
Dictionary. Raw or processed structured data.
"""
# check if xml library is installed and fail gracefully if it is not
try:
import xmltodict
except Exception:
raise LibraryNotInstalled('The xmltodict library is not installed.')
if not quiet:
jc.utils.compatibility(__name__, info.compatible)

View File

@@ -79,19 +79,13 @@ Examples:
}
]
"""
import sys
import jc.utils
# check if yaml library is installed and fail gracefully if it is not
try:
from ruamel.yaml import YAML
except Exception:
jc.utils.error_message('The ruamel.yaml library is not installed.')
sys.exit(1)
from jc.exceptions import LibraryNotInstalled
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.4'
version = '1.5'
description = 'YAML file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@@ -135,6 +129,12 @@ def parse(data, raw=False, quiet=False):
List of Dictionaries representing the YAML documents.
"""
# check if yaml library is installed and fail gracefully if it is not
try:
from ruamel.yaml import YAML
except Exception:
raise LibraryNotInstalled('The ruamel.yaml library is not installed.')
if not quiet:
jc.utils.compatibility(__name__, info.compatible)

View File

@@ -154,7 +154,7 @@ def convert_to_bool(value):
# if float converts, then bool the result
# if float does not convert then look for truthy string and bool True
# else False
truthy = ['y', 'yes', 'true']
truthy = ['y', 'yes', 'true', '*']
if isinstance(value, (int, float)):
return bool(value)
@@ -239,6 +239,9 @@ class timestamp:
}
utc_tz = False
# sometimes UTC is referenced as 'Coordinated Universal Time'. Convert to 'UTC'
data = data.replace('Coordinated Universal Time', 'UTC')
if 'UTC' in data:
utc_tz = True
if 'UTC+' in data or 'UTC-' in data:
@@ -254,6 +257,7 @@ class timestamp:
{'id': 1500, 'format': '%Y-%m-%d %H:%M', 'locale': None}, # en_US.UTF-8 local format (found in who cli output): 2021-03-23 00:14
{'id': 1600, 'format': '%m/%d/%Y %I:%M %p', 'locale': None}, # Windows english format (found in dir cli output): 12/07/2019 02:09 AM
{'id': 1700, 'format': '%m/%d/%Y, %I:%M:%S %p', 'locale': None}, # Windows english format wint non-UTC tz (found in systeminfo cli output): 3/22/2021, 1:15:51 PM (UTC-0600)
{'id': 1705, 'format': '%m/%d/%Y, %I:%M:%S %p %Z', 'locale': None}, # Windows english format with UTC tz (found in systeminfo cli output): 3/22/2021, 1:15:51 PM (UTC)
{'id': 1710, 'format': '%m/%d/%Y, %I:%M:%S %p UTC%z', 'locale': None}, # Windows english format with UTC tz (found in systeminfo cli output): 3/22/2021, 1:15:51 PM (UTC+0000)
{'id': 2000, 'format': '%a %d %b %Y %I:%M:%S %p %Z', 'locale': None}, # en_US.UTF-8 local format (found in upower cli output): Tue 23 Mar 2021 04:12:11 PM UTC
{'id': 3000, 'format': '%a %d %b %Y %I:%M:%S %p', 'locale': None}, # en_US.UTF-8 local format with non-UTC tz (found in upower cli output): Tue 23 Mar 2021 04:12:11 PM IST

515
man/jc.1 Normal file
View File

@@ -0,0 +1,515 @@
.TH jc 1 2021-08-31 1.16.2 "JSON CLI output utility"
.SH NAME
jc \- JSONifies the output of many CLI tools and file-types
.SH SYNOPSIS
COMMAND | jc PARSER [OPTIONS]
or "Magic" syntax:
jc [OPTIONS] COMMAND
.SH DESCRIPTION
jc JSONifies the output of many CLI tools and file-types for easier parsing in scripts. jc accepts piped input from \fBSTDIN\fP and outputs a JSON representation of the previous command's output to \fBSTDOUT\fP. Alternatively, the "Magic" syntax can be used by prepending jc to the command to be converted. Options can be passed to jc immediately before the command is given. (Note: "Magic" syntax does not support shell builtins or command aliases)
.SH OPTIONS
.B
Parsers:
.RS
.TP
.B
\fB--acpi\fP
`acpi` command parser
.TP
.B
\fB--airport\fP
`airport -I` command parser
.TP
.B
\fB--airport-s\fP
`airport -s` command parser
.TP
.B
\fB--arp\fP
`arp` command parser
.TP
.B
\fB--blkid\fP
`blkid` command parser
.TP
.B
\fB--cksum\fP
`cksum` and `sum` command parser
.TP
.B
\fB--crontab\fP
`crontab` command and file parser
.TP
.B
\fB--crontab-u\fP
`crontab` file parser with user support
.TP
.B
\fB--csv\fP
CSV file parser
.TP
.B
\fB--date\fP
`date` command parser
.TP
.B
\fB--df\fP
`df` command parser
.TP
.B
\fB--dig\fP
`dig` command parser
.TP
.B
\fB--dir\fP
`dir` command parser
.TP
.B
\fB--dmidecode\fP
`dmidecode` command parser
.TP
.B
\fB--dpkg-l\fP
`dpkg -l` command parser
.TP
.B
\fB--du\fP
`du` command parser
.TP
.B
\fB--env\fP
`env` command parser
.TP
.B
\fB--file\fP
`file` command parser
.TP
.B
\fB--finger\fP
`finger` command parser
.TP
.B
\fB--free\fP
`free` command parser
.TP
.B
\fB--fstab\fP
`/etc/fstab` file parser
.TP
.B
\fB--group\fP
`/etc/group` file parser
.TP
.B
\fB--gshadow\fP
`/etc/gshadow` file parser
.TP
.B
\fB--hash\fP
`hash` command parser
.TP
.B
\fB--hashsum\fP
hashsum command parser (`md5sum`, `shasum`, etc.)
.TP
.B
\fB--hciconfig\fP
`hciconfig` command parser
.TP
.B
\fB--history\fP
`history` command parser
.TP
.B
\fB--hosts\fP
`/etc/hosts` file parser
.TP
.B
\fB--id\fP
`id` command parser
.TP
.B
\fB--ifconfig\fP
`ifconfig` command parser
.TP
.B
\fB--ini\fP
INI file parser
.TP
.B
\fB--iptables\fP
`iptables` command parser
.TP
.B
\fB--iw-scan\fP
`iw dev [device] scan` command parser
.TP
.B
\fB--jobs\fP
`jobs` command parser
.TP
.B
\fB--kv\fP
Key/Value file parser
.TP
.B
\fB--last\fP
`last` and `lastb` command parser
.TP
.B
\fB--ls\fP
`ls` command parser
.TP
.B
\fB--lsblk\fP
`lsblk` command parser
.TP
.B
\fB--lsmod\fP
`lsmod` command parser
.TP
.B
\fB--lsof\fP
`lsof` command parser
.TP
.B
\fB--mount\fP
`mount` command parser
.TP
.B
\fB--netstat\fP
`netstat` command parser
.TP
.B
\fB--ntpq\fP
`ntpq -p` command parser
.TP
.B
\fB--passwd\fP
`/etc/passwd` file parser
.TP
.B
\fB--ping\fP
`ping` and `ping6` command parser
.TP
.B
\fB--pip-list\fP
`pip list` command parser
.TP
.B
\fB--pip-show\fP
`pip show` command parser
.TP
.B
\fB--ps\fP
`ps` command parser
.TP
.B
\fB--route\fP
`route` command parser
.TP
.B
\fB--rpm-qi\fP
`rpm -qi` command parser
.TP
.B
\fB--sfdisk\fP
`sfdisk` command parser
.TP
.B
\fB--shadow\fP
`/etc/shadow` file parser
.TP
.B
\fB--ss\fP
`ss` command parser
.TP
.B
\fB--stat\fP
`stat` command parser
.TP
.B
\fB--sysctl\fP
`sysctl` command parser
.TP
.B
\fB--systemctl\fP
`systemctl` command parser
.TP
.B
\fB--systemctl-lj\fP
`systemctl list-jobs` command parser
.TP
.B
\fB--systemctl-ls\fP
`systemctl list-sockets` command parser
.TP
.B
\fB--systemctl-luf\fP
`systemctl list-unit-files` command parser
.TP
.B
\fB--systeminfo\fP
`systeminfo` command parser
.TP
.B
\fB--time\fP
`/usr/bin/time` command parser
.TP
.B
\fB--timedatectl\fP
`timedatectl status` command parser
.TP
.B
\fB--tracepath\fP
`tracepath` and `tracepath6` command parser
.TP
.B
\fB--traceroute\fP
`traceroute` and `traceroute6` command parser
.TP
.B
\fB--ufw\fP
`ufw status` command parser
.TP
.B
\fB--ufw-appinfo\fP
`ufw app info [application]` command parser
.TP
.B
\fB--uname\fP
`uname -a` command parser
.TP
.B
\fB--upower\fP
`upower` command parser
.TP
.B
\fB--uptime\fP
`uptime` command parser
.TP
.B
\fB--w\fP
`w` command parser
.TP
.B
\fB--wc\fP
`wc` command parser
.TP
.B
\fB--who\fP
`who` command parser
.TP
.B
\fB--xml\fP
XML file parser
.TP
.B
\fB--yaml\fP
YAML file parser
.RE
.PP
.B
Options:
.RS
.TP
.B
\fB-a\fP
about jc (JSON output)
.TP
.B
\fB-d\fP
debug - show traceback (\fB-dd\fP for verbose traceback)
.TP
.B
\fB-h\fP
help (\fB-h --parser_name\fP for parser documentation)
.TP
.B
\fB-m\fP
monochrome output
.TP
.B
\fB-p\fP
pretty print output
.TP
.B
\fB-q\fP
quiet - suppress warnings
.TP
.B
\fB-r\fP
raw JSON output
.TP
.B
\fB-v\fP
version information
.SH EXIT CODES
Any fatal errors within jc will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), jc will store the exit code of the program being parsed and add it to the jc exit code. This way it is easier to determine if an error was from the parsed program or jc.
Consider the following examples using `ifconfig`:
.RS
ifconfig exit code = \fB0\fP, jc exit code = \fB0\fP, combined exit code = \fB0\fP (no errors)
ifconfig exit code = \fB1\fP, jc exit code = \fB0\fP, combined exit code = \fB1\fP (error in ifconfig)
ifconfig exit code = \fB0\fP, jc exit code = \fB100\fP, combined exit code = \fB100\fP (error in jc)
ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB101\fP (error in both ifconfig and jc)
.RE
.SH ENVIRONMENT
You can specify custom colors via the \fBJC_COLORS\fP environment variable. The \fBJC_COLORS\fP environment variable takes four comma separated string values in the following format:
JC_COLORS=<keyname_color>,<keyword_color>,<number_color>,<string_color>
Where colors are: \fBblack\fP, \fBred\fP, \fBgreen\fP, \fByellow\fP, \fBblue\fP, \fBmagenta\fP, \fBcyan\fP, \fBgray\fP, \fBbrightblack\fP, \fBbrightred\fP, \fBbrightgreen\fP, \fBbrightyellow\fP, \fBbrightblue\fP, \fBbrightmagenta\fP, \fBbrightcyan\fP, \fBwhite\fP, or \fBdefault\fP
For example, to set to the default colors:
.RS
JC_COLORS=blue,brightblack,magenta,green
or
JC_COLORS=default,default,default,default
.RE
.SH CUSTOM PARSERS
Custom local parser plugins may be placed in a \fBjc/jcparsers\fP folder in your local "App data directory":
.RS
- Linux/unix: \fB$HOME/.local/share/jc/jcparsers\fP
- macOS: \fB$HOME/Library/Application Support/jc/jcparsers\fP
- Windows: \fB$LOCALAPPDATA\\jc\\jc\\jcparsers\fP
.RE
Local parser plugins are standard python module files. Use the \fBjc/parsers/foo.py\fP parser as a template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder.
Local plugin filenames must be valid python module names, therefore must consist entirely of alphanumerics and start with a letter. Local plugins may override default plugins.
Note: The application data directory follows the XDG Base Directory Specification
.SH CAVEATS
\fBLocale:\fP For best results set the \fBLANG\fP locale environment variable to \fBC\fP or \fBen_US.UTF-8\fP. For example, either by setting directly on the command-line:
\fB$ LANG=C date | jc --date\fP
or by exporting to the environment before running commands:
\fB$ export LANG=C\fP
\fBTimezones:\fP Some parsers have calculated epoch timestamp fields added to the output. Unless a timestamp field name has a \fB_utc\fP suffix it is considered naive. (i.e. based on the local timezone of the system the \fBjc\fP parser was run on).
If a UTC timezone can be detected in the text of the command output, the timestamp will be timezone aware and have a \fB_utc\fP suffix on the key name. (e.g. \fBepoch_utc\fP) No other timezones are supported for aware timestamps.
.SH EXAMPLES
Standard Syntax:
.RS
$ dig www.google.com | jc \fB--dig\fP \fB-p\fP
.RE
Magic Syntax:
.RS
$ jc \fB-p\fP dig www.google.com
.RE
For parser documentation:
.RS
$ jc \fB-h\fP \fB--dig\fP
.RE
.SH AUTHOR
Kelly Brazil (kellyjonbrazil@gmail.com)
https://github.com/kellyjonbrazil/jc
.SH COPYRIGHT
Copyright (c) 2019-2021 Kelly Brazil
License: MIT License

Binary file not shown.

View File

@@ -22,4 +22,4 @@ with open('man/jc.1', 'rb') as f_in:
shutil.copyfile('man/jc.1.gz', 'jc/man/jc.1.gz')
os.remove('man/jc.1')
# os.remove('man/jc.1')

View File

@@ -5,7 +5,7 @@ with open('README.md', 'r') as f:
setuptools.setup(
name='jc',
version='1.15.3',
version='1.16.2',
author='Kelly Brazil',
author_email='kellyjonbrazil@gmail.com',
description='Converts the output of popular command-line tools and file-types to JSON.',

View File

@@ -4,12 +4,12 @@ jc \- JSONifies the output of many CLI tools and file-types
.SH SYNOPSIS
COMMAND | jc PARSER [OPTIONS]
or magic syntax:
or "Magic" syntax:
jc [OPTIONS] COMMAND
.SH DESCRIPTION
jc JSONifies the output of many CLI tools and file-types for easier parsing in scripts. jc accepts piped input from \fBSTDIN\fP and outputs a JSON representation of the previous command's output to \fBSTDOUT\fP. Alternatively, the "magic" syntax can be used by prepending jc to the command to be converted. Options can be passed to jc immediately before the command is given. (Note: command aliases are not supported).
jc JSONifies the output of many CLI tools and file-types for easier parsing in scripts. jc accepts piped input from \fBSTDIN\fP and outputs a JSON representation of the previous command's output to \fBSTDOUT\fP. Alternatively, the "Magic" syntax can be used by prepending jc to the command to be converted. Options can be passed to jc immediately before the command is given. (Note: "Magic" syntax does not support shell builtins or command aliases)
.SH OPTIONS
.B
@@ -62,6 +62,21 @@ raw JSON output
\fB-v\fP
version information
.SH EXIT CODES
Any fatal errors within jc will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), jc will store the exit code of the program being parsed and add it to the jc exit code. This way it is easier to determine if an error was from the parsed program or jc.
Consider the following examples using `ifconfig`:
.RS
ifconfig exit code = \fB0\fP, jc exit code = \fB0\fP, combined exit code = \fB0\fP (no errors)
ifconfig exit code = \fB1\fP, jc exit code = \fB0\fP, combined exit code = \fB1\fP (error in ifconfig)
ifconfig exit code = \fB0\fP, jc exit code = \fB100\fP, combined exit code = \fB100\fP (error in jc)
ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB101\fP (error in both ifconfig and jc)
.RE
.SH ENVIRONMENT
You can specify custom colors via the \fBJC_COLORS\fP environment variable. The \fBJC_COLORS\fP environment variable takes four comma separated string values in the following format:
@@ -97,7 +112,7 @@ Local plugin filenames must be valid python module names, therefore must consist
Note: The application data directory follows the XDG Base Directory Specification
.SH CAVEATS
\fBLocale:\fP For best results set the \fBLANG\fP locale environment variable to \fBC\fP. For example, either by setting directly on the command-line:
\fBLocale:\fP For best results set the \fBLANG\fP locale environment variable to \fBC\fP or \fBen_US.UTF-8\fP. For example, either by setting directly on the command-line:
\fB$ LANG=C date | jc --date\fP

View File

@@ -1,7 +1,7 @@
![Tests](https://github.com/kellyjonbrazil/jc/workflows/Tests/badge.svg?branch=master)
![Pypi](https://img.shields.io/pypi/v/jc.svg)
> Try the new `jc` [web demo](https://jc-web-demo.herokuapp.com/)!
> Try the `jc` [web demo](https://jc-web-demo.herokuapp.com/)
> JC is [now available](https://galaxy.ansible.com/community/general) as an Ansible filter plugin in the `community.general` collection! See this [blog post](https://blog.kellybrazil.com/2020/08/30/parsing-command-output-in-ansible-with-jc/) for an example.
@@ -19,7 +19,7 @@ dig example.com | jc --dig
39049,"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:09:00 PDT 2021","rcvd":56,"when_epoch":1618614540,"when_epoch_utc":null}]
```
This allows further command-line processing of output with tools like `jq` by piping commands:
This allows further command-line processing of output with tools like `jq` or [`jello`](https://github.com/kellyjonbrazil/jello) by piping commands:
```bash
$ dig example.com | jc --dig | jq -r '.[].answer[].data'
93.184.216.34
@@ -60,24 +60,30 @@ The `jc` parsers can also be used as python modules. In this case the output wil
'2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)', 'when': 'Fri Apr 16 16:13:00 PDT 2021', 'rcvd': 56,
'when_epoch': 1618614780, 'when_epoch_utc': None}]
```
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 available. 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.
To access the raw, pre-processed JSON, use the `-r` cli option or the `raw=True` function parameter in `parse()`.
Schemas for each parser can be found at the documentation link beside each parser below.
Schemas for each parser can be found at the documentation link beside each [**Parser**](#parsers) below.
Release notes can be found [here](https://blog.kellybrazil.com/category/jc-news/).
## Why Would Anyone Do This!?
For more information on the motivations for this project, please see my [blog post](https://blog.kellybrazil.com/2019/11/26/bringing-the-unix-philosophy-to-the-21st-century/).
For more information on the motivations for this project, please see my blog post on [Bringing the Unix Philosophy to the 21st Century](https://blog.kellybrazil.com/2019/11/26/bringing-the-unix-philosophy-to-the-21st-century/).
See also:
- [libxo on FreeBSD](http://juniper.github.io/libxo/libxo-manual.html)
- [powershell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7)
- [blog: linux apps should have a json flag](https://thomashunter.name/posts/2012-06-06-linux-cli-apps-should-have-a-json-flag)
Use Cases:
- [Bash scripting](https://blog.kellybrazil.com/2021/04/12/practical-json-at-the-command-line/)
- [Ansible command output parsing](https://blog.kellybrazil.com/2020/08/30/parsing-command-output-in-ansible-with-jc/)
- [Saltstack command output parsing](https://blog.kellybrazil.com/2020/09/15/parsing-command-output-in-saltstack-with-jc/)
- [Nornir command output parsing](https://blog.kellybrazil.com/2020/12/09/parsing-command-output-in-nornir-with-jc/)
## Installation
There are several ways to get `jc`. You can install via `pip`; other OS package repositories like `apt-get`, `dnf`, `zypper`, `pacman`, `nix-env`, `guix`, `brew`, or `portsnap`; via DEB/RPM packages; or by downloading the correct binary for your architecture and running it anywhere on your filesystem.
There are several ways to get `jc`. You can install via `pip`, OS package repositories, via DEB/RPM/MSI packaged binaries for linux and Windows, or by downloading the correct binary for your architecture and running it anywhere on your filesystem.
### Pip (macOS, linux, unix, Windows)
```bash
@@ -94,7 +100,7 @@ pip3 install jc
| Arch linux | `pacman -S jc` |
| NixOS linux | `nix-env -iA nixpkgs.jc` or `nix-env -iA nixos.jc` |
| Guix System linux | `guix install jc` |
| MacOS | `brew install jc` |
| macOS | `brew install jc` |
| FreeBSD | `portsnap fetch update && cd /usr/ports/textproc/py-jc && make install clean` |
| Ansible filter plugin | `ansible-galaxy collection install community.general` |
@@ -125,6 +131,19 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio
- `-r` raw output. Provides a more literal JSON output, typically with string values and no additional semantic processing
- `-v` version information
### Exit Codes
Any fatal errors within `jc` will generate an exit code of `100`, otherwise the exit code will be `0`. When using the "magic" syntax (e.g. `jc ifconfig eth0`), `jc` will store the exit code of the program being parsed and add it to the `jc` exit code. This way it is easier to determine if an error was from the parsed program or `jc`.
Consider the following examples using `ifconfig`:
| `ifconfig` exit code | `jc` exit code | Combined exit code | Interpretation |
|----------------------|----------------|--------------------|------------------------------------|
| `0` | `0` | `0` | No errors |
| `1` | `0` | `1` | Error in `ifconfig` |
| `0` | `100` | `100` | Error in `jc` |
| `1` | `100` | `101` | Error in both `ifconfig` and `jc` |
### Setting Custom Colors via Environment Variable
You can specify custom colors via the `JC_COLORS` environment variable. The `JC_COLORS` environment variable takes four comma separated string values in the following format:
```bash
@@ -150,14 +169,14 @@ Custom local parser plugins may be placed in a `jc/jcparsers` folder in your loc
Local parser plugins are standard python module files. Use the [`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py) parser as a template and simply place a `.py` file in the `jcparsers` subfolder.
Local plugin filenames must be valid python module names, therefore must consist entirely of alphanumerics and start with a letter. Local plugins may override default plugins.
Local plugin filenames must be valid python module names, therefore must consist entirely of alphanumerics and start with a letter. Local plugins may override default parsers.
> Note: The application data directory follows the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
### Caveats
**Locale:**
For best results set the `LANG` locale environment variable to `C`. For example, either by setting directly on the command-line:
For best results set the `LANG` locale environment variable to `C` or `en_US.UTF-8`. For example, either by setting directly on the command-line:
```
$ LANG=C date | jc --date
```
@@ -170,16 +189,21 @@ $ export LANG=C
Some parsers have calculated epoch timestamp fields added to the output. Unless a timestamp field name has a `_utc` suffix it is considered naive. (i.e. based on the local timezone of the system the `jc` parser was run on).
If a UTC timezone can be detected in the text of the command output, the timestamp will be timezone aware and have a `_utc`P suffix on the key name. (e.g. `epoch_utc`) No other timezones are supported for aware timestamps.
If a UTC timezone can be detected in the text of the command output, the timestamp will be timezone aware and have a `_utc` suffix on the key name. (e.g. `epoch_utc`) No other timezones are supported for aware timestamps.
## Compatibility
Some parsers like `ls`, `ps`, `dig`, etc. will work on any platform. Other parsers that are platform-specific will generate a warning message if they are used on an unsupported platform. To see all parser information, including compatibility, run `jc -ap`.
Some parsers like `dig`, `xml`, `csv`, etc. will work on any platform. Other parsers that convert platform-specific output will generate a warning message if they are run on an unsupported platform. To see all parser information, including compatibility, run `jc -ap`.
You may still use a parser on an unsupported platform - for example, you may want to parse a file with linux `lsof` output on an macOS laptop. In that case you can suppress the warning message with the `-q` cli option or the `quiet=True` function parameter in `parse()`:
You may still use a parser on an unsupported platform - for example, you may want to parse a file with linux `lsof` output on an macOS or Windows laptop. In that case you can suppress the warning message with the `-q` cli option or the `quiet=True` function parameter in `parse()`:
macOS:
```bash
cat lsof.out | jc --lsof -q
```
or Windows:
```bash
type lsof.out | jc --lsof -q
```
Tested on:
- Centos 7.7
@@ -191,6 +215,8 @@ Tested on:
- NixOS
- FreeBSD12
- Windows 10
- Windows 2016 Server
- Windows 2019 Server
## Contributions
Feel free to add/improve code or parsers! You can use the [`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py) parser as a template and submit your parser with a pull request.

View File

@@ -0,0 +1 @@
{"destination_ip":"127.0.0.1","data_bytes":56,"pattern":null,"destination":"127.0.0.1","packets_transmitted":20,"packets_received":20,"packet_loss_percent":0.0,"duplicates":0,"time_ms":19070.0,"round_trip_ms_min":0.038,"round_trip_ms_avg":0.047,"round_trip_ms_max":0.08,"round_trip_ms_stddev":0.011,"responses":[{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":1,"ttl":64,"time_ms":0.038,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":2,"ttl":64,"time_ms":0.043,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":3,"ttl":64,"time_ms":0.044,"duplicate":false},{"type":"unparsable_line","unparsed_line":"64 bytes from 127.0.0.1: error - weird error"},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":5,"ttl":64,"time_ms":0.08,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":6,"ttl":64,"time_ms":0.043,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":7,"ttl":64,"time_ms":0.047,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":8,"ttl":64,"time_ms":0.04,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":9,"ttl":64,"time_ms":0.052,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":10,"ttl":64,"time_ms":0.044,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":11,"ttl":64,"time_ms":0.043,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":12,"ttl":64,"time_ms":0.043,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":13,"ttl":64,"time_ms":0.05,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":14,"ttl":64,"time_ms":0.045,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":15,"ttl":64,"time_ms":0.062,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":16,"ttl":64,"time_ms":0.046,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":17,"ttl":64,"time_ms":0.046,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":18,"ttl":64,"time_ms":0.045,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":19,"ttl":64,"time_ms":0.044,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"127.0.0.1","icmp_seq":20,"ttl":64,"time_ms":0.044,"duplicate":false}]}

View File

@@ -0,0 +1,27 @@
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.043 ms: some weird error
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.044 ms
64 bytes from 127.0.0.1: error - weird error
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.080 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.043 ms
this is a weird error message
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.047 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.044 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.043 ms
unparsable line
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.043 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.045 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.062 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=64 time=0.045 ms
64 bytes from 127.0.0.1: icmp_seq=19 ttl=64 time=0.044 ms
64 bytes from 127.0.0.1: icmp_seq=20 ttl=64 time=0.044 ms
--- 127.0.0.1 ping statistics ---
20 packets transmitted, 20 received, 0% packet loss, time 19070ms
rtt min/avg/max/mdev = 0.038/0.047/0.080/0.011 ms

View File

@@ -0,0 +1 @@
{"destination_ip":"2a04:4e42:600::323","data_bytes":56,"pattern":"0xabcd","destination":"2a04:4e42:600::323","packets_transmitted":20,"packets_received":19,"packet_loss_percent":5.0,"duplicates":0,"time_ms":19067.0,"round_trip_ms_min":27.064,"round_trip_ms_avg":33.626,"round_trip_ms_max":38.146,"round_trip_ms_stddev":3.803,"responses":[{"type":"unparsable_line","unparsed_line":"64 bytes from 2a04:4e42:600::323: strange error"},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":2,"ttl":59,"time_ms":28.4,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":3,"ttl":59,"time_ms":36.0,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":4,"ttl":59,"time_ms":28.5,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":5,"ttl":59,"time_ms":35.8,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":6,"ttl":59,"time_ms":34.4,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":7,"ttl":59,"time_ms":30.7,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":8,"ttl":59,"time_ms":28.5,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":9,"ttl":59,"time_ms":36.5,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":10,"ttl":59,"time_ms":36.3,"duplicate":false},{"type":"timeout","timestamp":null,"icmp_seq":11},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":12,"ttl":59,"time_ms":37.4,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":13,"ttl":59,"time_ms":30.7,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":14,"ttl":59,"time_ms":36.5,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":15,"ttl":59,"time_ms":35.4,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":16,"ttl":59,"time_ms":36.3,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":17,"ttl":59,"time_ms":37.5,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":18,"ttl":59,"time_ms":36.2,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":19,"ttl":59,"time_ms":27.0,"duplicate":false},{"type":"reply","timestamp":null,"bytes":64,"response_ip":"2a04:4e42:600::323","icmp_seq":20,"ttl":59,"time_ms":38.1,"duplicate":false}]}

View File

@@ -0,0 +1,27 @@
PATTERN: 0xabcd
PING 2a04:4e42:600::323(2a04:4e42:600::323) 56 data bytes
64 bytes from 2a04:4e42:600::323: strange error
64 bytes from 2a04:4e42:600::323: icmp_seq=2 ttl=59 time=28.4 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=3 ttl=59 time=36.0 ms
strange error here
64 bytes from 2a04:4e42:600::323: icmp_seq=4 ttl=59 time=28.5 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=5 ttl=59 time=35.8 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=6 ttl=59 time=34.4 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=7 ttl=59 time=30.7 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=8 ttl=59 time=28.5 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=9 ttl=59 time=36.5 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=10 ttl=59 time=36.3 ms
no answer yet for icmp_seq=11
64 bytes from 2a04:4e42:600::323: icmp_seq=12 ttl=59 time=37.4 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=13 ttl=59 time=30.7 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=14 ttl=59 time=36.5 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=15 ttl=59 time=35.4 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=16 ttl=59 time=36.3 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=17 ttl=59 time=37.5 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=18 ttl=59 time=36.2 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=19 ttl=59 time=27.0 ms
64 bytes from 2a04:4e42:600::323: icmp_seq=20 ttl=59 time=38.1 ms
--- 2a04:4e42:600::323 ping statistics ---
20 packets transmitted, 19 received, 5% packet loss, time 19067ms
rtt min/avg/max/mdev = 27.064/33.626/38.146/3.803 ms

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","units":"sectors","partitions":[{"device":"/dev/sda1","start":2048,"size":2097152,"id":"83","boot":true},{"device":"/dev/sda2","start":2099200,"size":39843840,"id":"8e","boot":false},{"device":"/dev/sda3","start":0,"size":0,"id":"0","boot":false},{"device":"/dev/sda4","start":0,"size":0,"id":"0","boot":false}]},{"disk":"/dev/sdb","units":"sectors","partitions":[{"device":"/dev/sdb1","start":2048,"size":2097152,"id":"83","boot":true},{"device":"/dev/sdb2","start":2099200,"size":39843840,"id":"8e","boot":false},{"device":"/dev/sdb3","start":0,"size":0,"id":"0","boot":false},{"device":"/dev/sdb4","start":0,"size":0,"id":"0","boot":false}]}]

View File

@@ -0,0 +1,16 @@
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 2097152, Id=83, bootable
/dev/sda2 : start= 2099200, size= 39843840, Id=8e
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
# partition table of /dev/sdb
unit: sectors
/dev/sdb1 : start= 2048, size= 2097152, Id=83, bootable
/dev/sdb2 : start= 2099200, size= 39843840, Id=8e
/dev/sdb3 : start= 0, size= 0, Id= 0
/dev/sdb4 : start= 0, size= 0, Id= 0

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","units":"sectors","partitions":[{"device":"/dev/sda1","start":2048,"size":2097152,"id":"83","boot":true},{"device":"/dev/sda2","start":2099200,"size":39843840,"id":"8e","boot":false},{"device":"/dev/sda3","start":0,"size":0,"id":"0","boot":false},{"device":"/dev/sda4","start":0,"size":0,"id":"0","boot":false}]}]

View File

@@ -0,0 +1,7 @@
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 2097152, Id=83, bootable
/dev/sda2 : start= 2099200, size= 39843840, Id=8e
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","cylinders":2610,"heads":255,"sectors_per_track":63,"units":"cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0","partitions":[{"device":"/dev/sda1","boot":true,"start":0,"end":130,"cyls":131,"blocks":1048576,"id":"83","system":"Linux"},{"device":"/dev/sda2","boot":false,"start":130,"end":2610,"cyls":2481,"blocks":19921920,"id":"8e","system":"Linux LVM"},{"device":"/dev/sda3","boot":false,"start":0,"end":null,"cyls":0,"blocks":0,"id":"0","system":"Empty"},{"device":"/dev/sda4","boot":false,"start":0,"end":null,"cyls":0,"blocks":0,"id":"0","system":"Empty"}]},{"disk":"/dev/mapper/centos-root","cylinders":2218,"heads":255,"sectors_per_track":63},{"disk":"/dev/mapper/centos-swap","cylinders":261,"heads":255,"sectors_per_track":63,"units":"cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0","partitions":[{"device":"/dev/sda1","boot":true,"start":0,"end":130,"cyls":131,"blocks":1048576,"id":"83","system":"Linux"},{"device":"/dev/sda2","boot":false,"start":130,"end":2610,"cyls":2481,"blocks":19921920,"id":"8e","system":"Linux LVM"},{"device":"/dev/sda3","boot":false,"start":0,"end":null,"cyls":0,"blocks":0,"id":"0","system":"Empty"},{"device":"/dev/sda4","boot":false,"start":0,"end":null,"cyls":0,"blocks":0,"id":"0","system":"Empty"}]}]

View File

@@ -0,0 +1,21 @@
Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/sda1 * 0+ 130- 131- 1048576 83 Linux
/dev/sda2 130+ 2610- 2481- 19921920 8e Linux LVM
/dev/sda3 0 - 0 0 0 Empty
/dev/sda4 0 - 0 0 0 Empty
Disk /dev/mapper/centos-root: 2218 cylinders, 255 heads, 63 sectors/track
Disk /dev/mapper/centos-swap: 261 cylinders, 255 heads, 63 sectors/track
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/sda1 * 0+ 130- 131- 1048576 83 Linux
/dev/sda2 130+ 2610- 2481- 19921920 8e Linux LVM
/dev/sda3 0 - 0 0 0 Empty
/dev/sda4 0 - 0 0 0 Empty

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","cylinders":2610,"heads":255,"sectors_per_track":63,"units":"cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0","partitions":[{"device":"/dev/sda1","boot":true,"start":0,"end":130,"cyls":131,"blocks":1048576,"id":"83","system":"Linux"},{"device":"/dev/sda2","boot":false,"start":130,"end":2610,"cyls":2481,"blocks":19921920,"id":"8e","system":"Linux LVM"},{"device":"/dev/sda3","boot":false,"start":0,"end":null,"cyls":0,"blocks":0,"id":"0","system":"Empty"},{"device":"/dev/sda4","boot":false,"start":0,"end":null,"cyls":0,"blocks":0,"id":"0","system":"Empty"}]},{"disk":"/dev/mapper/centos-root","cylinders":2218,"heads":255,"sectors_per_track":63},{"disk":"/dev/mapper/centos-swap","cylinders":261,"heads":255,"sectors_per_track":63}]

14
tests/fixtures/centos-7.7/sfdisk-l.out vendored Normal file
View File

@@ -0,0 +1,14 @@
Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/sda1 * 0+ 130- 131- 1048576 83 Linux
/dev/sda2 130+ 2610- 2481- 19921920 8e Linux LVM
/dev/sda3 0 - 0 0 0 Empty
/dev/sda4 0 - 0 0 0 Empty
Disk /dev/mapper/centos-root: 2218 cylinders, 255 heads, 63 sectors/track
Disk /dev/mapper/centos-swap: 261 cylinders, 255 heads, 63 sectors/track

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","cylinders":2610,"heads":255,"sectors_per_track":63,"units":"blocks of 1024 bytes, counting from 0","partitions":[{"device":"/dev/sda1","boot":true,"start":1024,"end":1049599,"blocks":1048576,"id":"83","system":"Linux"},{"device":"/dev/sda2","boot":false,"start":1049600,"end":20971519,"blocks":19921920,"id":"8e","system":"Linux LVM"},{"device":"/dev/sda3","boot":false,"start":0,"end":null,"blocks":0,"id":"0","system":"Empty"},{"device":"/dev/sda4","boot":false,"start":0,"end":null,"blocks":0,"id":"0","system":"Empty"}]},{"disk":"/dev/mapper/centos-root","cylinders":2218,"heads":255,"sectors_per_track":63},{"disk":"/dev/mapper/centos-swap","cylinders":261,"heads":255,"sectors_per_track":63}]

View File

@@ -0,0 +1,14 @@
Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track
Units: blocks of 1024 bytes, counting from 0
Device Boot Start End #blocks Id System
/dev/sda1 * 1024 1049599 1048576 83 Linux
/dev/sda2 1049600 20971519 19921920 8e Linux LVM
/dev/sda3 0 - 0 0 Empty
/dev/sda4 0 - 0 0 Empty
Disk /dev/mapper/centos-root: 2218 cylinders, 255 heads, 63 sectors/track
Disk /dev/mapper/centos-swap: 261 cylinders, 255 heads, 63 sectors/track

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","cylinders":2610,"heads":255,"sectors_per_track":63,"units":"1MiB = 1024*1024 bytes, blocks of 1024 bytes, counting from 0","partitions":[{"device":"/dev/sda1","boot":true,"start":1,"end":1024,"mib":1024,"blocks":1048576,"id":"83","system":"Linux"},{"device":"/dev/sda2","boot":false,"start":1025,"end":20479,"mib":19455,"blocks":19921920,"id":"8e","system":"Linux LVM"},{"device":"/dev/sda3","boot":false,"start":0,"end":null,"mib":0,"blocks":0,"id":"0","system":"Empty"},{"device":"/dev/sda4","boot":false,"start":0,"end":null,"mib":0,"blocks":0,"id":"0","system":"Empty"}]},{"disk":"/dev/mapper/centos-root","cylinders":2218,"heads":255,"sectors_per_track":63},{"disk":"/dev/mapper/centos-swap","cylinders":261,"heads":255,"sectors_per_track":63}]

View File

@@ -0,0 +1,14 @@
Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track
Units: 1MiB = 1024*1024 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End MiB #blocks Id System
/dev/sda1 * 1 1024 1024 1048576 83 Linux
/dev/sda2 1025 20479 19455 19921920 8e Linux LVM
/dev/sda3 0 - 0 0 0 Empty
/dev/sda4 0 - 0 0 0 Empty
Disk /dev/mapper/centos-root: 2218 cylinders, 255 heads, 63 sectors/track
Disk /dev/mapper/centos-swap: 261 cylinders, 255 heads, 63 sectors/track

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","cylinders":2610,"heads":255,"sectors_per_track":63,"units":"sectors of 512 bytes, counting from 0","partitions":[{"device":"/dev/sda1","boot":true,"start":2048,"end":2099199,"sectors":2097152,"id":"83","system":"Linux"},{"device":"/dev/sda2","boot":false,"start":2099200,"end":41943039,"sectors":39843840,"id":"8e","system":"Linux LVM"},{"device":"/dev/sda3","boot":false,"start":0,"end":null,"sectors":0,"id":"0","system":"Empty"},{"device":"/dev/sda4","boot":false,"start":0,"end":null,"sectors":0,"id":"0","system":"Empty"}]},{"disk":"/dev/mapper/centos-root","cylinders":2218,"heads":255,"sectors_per_track":63},{"disk":"/dev/mapper/centos-swap","cylinders":261,"heads":255,"sectors_per_track":63}]

View File

@@ -0,0 +1,14 @@
Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track
Units: sectors of 512 bytes, counting from 0
Device Boot Start End #sectors Id System
/dev/sda1 * 2048 2099199 2097152 83 Linux
/dev/sda2 2099200 41943039 39843840 8e Linux LVM
/dev/sda3 0 - 0 0 Empty
/dev/sda4 0 - 0 0 Empty
Disk /dev/mapper/centos-root: 2218 cylinders, 255 heads, 63 sectors/track
Disk /dev/mapper/centos-swap: 261 cylinders, 255 heads, 63 sectors/track

1
tests/fixtures/centos-8/sfdisk-F.json vendored Normal file
View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","free_disk_size":"0 B","free_bytes":0,"free_sectors":0,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512}]

9
tests/fixtures/centos-8/sfdisk-F.out vendored Normal file
View File

@@ -0,0 +1,9 @@
Unpartitioned space /dev/sda: 0 B, 0 bytes, 0 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

1
tests/fixtures/centos-8/sfdisk-l.json vendored Normal file
View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","disk_size":"20 GiB","bytes":21474836480,"sectors":41943040,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512,"disk_label_type":"dos","disk_identifier":"0x94988ac4","partitions":[{"device":"/dev/sda1","boot":true,"start":2048,"end":2099199,"sectors":2097152,"size":"1G","id":"83","type":"Linux"},{"device":"/dev/sda2","boot":false,"start":2099200,"end":41943039,"sectors":39843840,"size":"19G","id":"8e","type":"Linux LVM"}]},{"disk":"/dev/mapper/cl-root","disk_size":"17 GiB","bytes":18249416704,"sectors":35643392,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512},{"disk":"/dev/mapper/cl-swap","disk_size":"2 GiB","bytes":2147483648,"sectors":4194304,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512}]

24
tests/fixtures/centos-8/sfdisk-l.out vendored Normal file
View File

@@ -0,0 +1,24 @@
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x94988ac4
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 41943039 39843840 19G 8e Linux LVM
Disk /dev/mapper/cl-root: 17 GiB, 18249416704 bytes, 35643392 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/cl-swap: 2 GiB, 2147483648 bytes, 4194304 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

1
tests/fixtures/debian10/sfdisk-F.json vendored Normal file
View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","free_disk_size":"0 B","free_bytes":0,"free_sectors":0,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512}]

3
tests/fixtures/debian10/sfdisk-F.out vendored Normal file
View File

@@ -0,0 +1,3 @@
Unpartitioned space /dev/sda: 0 B, 0 bytes, 0 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sdc","free_disk_size":"1 GiB","free_bytes":1073741824,"free_sectors":2097152,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"partitions":[{"start":4194304,"end":6291455,"sectors":2097152,"size":"1G"}]}]

6
tests/fixtures/debian10/sfdisk-F2.out vendored Normal file
View File

@@ -0,0 +1,6 @@
Unpartitioned space /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
4194304 6291455 2097152 1G

1
tests/fixtures/debian10/sfdisk-l.json vendored Normal file
View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","disk_size":"20 GiB","bytes":21474836480,"sectors":41943040,"disk_model":"VMware Virtual S","units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512,"disk_label_type":"dos","disk_identifier":"0x3719ef0f","partitions":[{"device":"/dev/sda1","boot":true,"start":2048,"end":39942143,"sectors":39940096,"size":"19G","id":"83","type":"Linux"},{"device":"/dev/sda2","boot":false,"start":39944190,"end":41940991,"sectors":1996802,"size":"975M","id":"5","type":"Extended"},{"device":"/dev/sda5","boot":false,"start":39944192,"end":41940991,"sectors":1996800,"size":"975M","id":"82","type":"Linux swap / Solaris"}]}]

12
tests/fixtures/debian10/sfdisk-l.out vendored Normal file
View File

@@ -0,0 +1,12 @@
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3719ef0f
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 39942143 39940096 19G 83 Linux
/dev/sda2 39944190 41940991 1996802 975M 5 Extended
/dev/sda5 39944192 41940991 1996800 975M 82 Linux swap / Solaris

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","disk_size":"32 GiB","bytes":34359738368,"sectors":67108864,"disk_model":"QEMU HARDDISK","units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512,"disk_label_type":"dos","disk_identifier":"0x4f1ce2bf","partitions":[{"device":"/dev/sda1","boot":true,"start":2048,"end":13260799,"sectors":13258752,"size":"6.3G","id":"83","type":"Linux"},{"device":"/dev/sda2","boot":false,"start":13262846,"end":67106815,"sectors":53843970,"size":"25.7G","id":"5","type":"Extended"},{"device":"/dev/sda5","boot":false,"start":13262848,"end":18331647,"sectors":5068800,"size":"2.4G","id":"83","type":"Linux"},{"device":"/dev/sda6","boot":false,"start":18333696,"end":20332543,"sectors":1998848,"size":"976M","id":"82","type":"Linux swap / Solaris"},{"device":"/dev/sda7","boot":false,"start":20334592,"end":21352447,"sectors":1017856,"size":"497M","id":"83","type":"Linux"},{"device":"/dev/sda8","boot":false,"start":21354496,"end":67106815,"sectors":45752320,"size":"21.8G","id":"83","type":"Linux"}]}]

15
tests/fixtures/debian10/sfdisk-l2.out vendored Normal file
View File

@@ -0,0 +1,15 @@
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x4f1ce2bf
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 13260799 13258752 6.3G 83 Linux
/dev/sda2 13262846 67106815 53843970 25.7G 5 Extended
/dev/sda5 13262848 18331647 5068800 2.4G 83 Linux
/dev/sda6 18333696 20332543 1998848 976M 82 Linux swap / Solaris
/dev/sda7 20334592 21352447 1017856 497M 83 Linux
/dev/sda8 21354496 67106815 45752320 21.8G 83 Linux

View File

@@ -0,0 +1 @@
[{"disk":"/dev/sda","disk_size":"238.5 GiB","bytes":256060514304,"sectors":500118192,"disk_model":"NT-256","units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512,"disk_label_type":"gpt","disk_identifier":"50DEF501-A333-48D1-B1FC-C2F1CCFD5715","partitions":[{"device":"/dev/sda1","start":2048,"end":526335,"sectors":524288,"size":"256M","type":"Linux root (x86)"},{"device":"/dev/sda2","start":526336,"end":138545151,"sectors":138018816,"size":"65.8G","type":"Linux root (x86)"},{"device":"/dev/sda3","start":483684352,"end":500117503,"sectors":16433152,"size":"7.9G","type":"Linux swap"},{"device":"/dev/sda4","start":138545152,"end":483684351,"sectors":345139200,"size":"164.6G","type":"Linux filesystem"}]}]

13
tests/fixtures/debian10/sfdisk-l3.out vendored Normal file
View File

@@ -0,0 +1,13 @@
Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 sectors
Disk model: NT-256
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 50DEF501-A333-48D1-B1FC-C2F1CCFD5715
Device Start End Sectors Size Type
/dev/sda1 2048 526335 524288 256M Linux root (x86)
/dev/sda2 526336 138545151 138018816 65.8G Linux root (x86)
/dev/sda3 483684352 500117503 16433152 7.9G Linux swap
/dev/sda4 138545152 483684351 345139200 164.6G Linux filesystem

View File

@@ -0,0 +1 @@
[{"answer":[{"name":"cnn.com.","class":"IN","type":"A","ttl":47,"data":"151.101.65.67"},{"name":"cnn.com.","class":"IN","type":"A","ttl":47,"data":"151.101.193.67"},{"name":"cnn.com.","class":"IN","type":"A","ttl":47,"data":"151.101.129.67"},{"name":"cnn.com.","class":"IN","type":"A","ttl":47,"data":"151.101.1.67"}]}]

View File

@@ -0,0 +1,4 @@
cnn.com. 47 IN A 151.101.65.67
cnn.com. 47 IN A 151.101.193.67
cnn.com. 47 IN A 151.101.129.67
cnn.com. 47 IN A 151.101.1.67

View File

@@ -0,0 +1 @@
{"destination_ip":"192.168.1.220","data_bytes":56,"pattern":null,"destination":"192.168.1.220","packets_transmitted":8,"packets_received":0,"packet_loss_percent":100.0,"duplicates":0,"responses":[{"type":"timeout","icmp_seq":0,"duplicate":false},{"type":"timeout","icmp_seq":1,"duplicate":false},{"type":"unparsable_line","unparsed_line":"92 bytes from fgt1.attlocal.net (192.168.1.220) Destination Network Unreachable"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":11887,"flg":0,"off":0,"ttl":63,"pro":1,"cks":51248,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"unparsable_line","unparsed_line":"92 bytes from fgt1.attlocal.net (192.168.1.220) Weird error message"},{"type":"timeout","icmp_seq":2,"duplicate":false},{"type":"timeout","icmp_seq":3,"duplicate":false},{"type":"timeout","icmp_seq":4,"duplicate":false},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":40674,"flg":0,"off":0,"ttl":63,"pro":1,"cks":22461,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":31035,"flg":0,"off":0,"ttl":63,"pro":1,"cks":32100,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":53536,"flg":0,"off":0,"ttl":63,"pro":1,"cks":9599,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"timeout","icmp_seq":5,"duplicate":false},{"type":"timeout","icmp_seq":6,"duplicate":false}]}

View File

@@ -0,0 +1,35 @@
PING 192.168.1.220 (192.168.1.220): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Network Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 567b 0 0000 3f 01 a024 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 2e6f 0 0000 3f 01 c830 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Weird error message
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 a1ed 0 0000 3f 01 54b2 192.168.1.221 192.168.1.220
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
Request timeout for icmp_seq 4
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 9ee2 0 0000 3f 01 57bd 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 793b 0 0000 3f 01 7d64 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 d120 0 0000 3f 01 257f 192.168.1.221 192.168.1.220
Request timeout for icmp_seq 5
Request timeout for icmp_seq 6
--- 192.168.1.220 ping statistics ---
8 packets transmitted, 0 packets received, 100.0% packet loss

View File

@@ -0,0 +1 @@
{"destination_ip":"192.168.1.220","data_bytes":56,"pattern":null,"destination":"192.168.1.220","packets_transmitted":8,"packets_received":0,"packet_loss_percent":100.0,"duplicates":0,"responses":[{"type":"timeout","icmp_seq":0,"duplicate":false},{"type":"timeout","icmp_seq":1,"duplicate":false},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":22139,"flg":0,"off":0,"ttl":63,"pro":1,"cks":40996,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":11887,"flg":0,"off":0,"ttl":63,"pro":1,"cks":51248,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":41453,"flg":0,"off":0,"ttl":63,"pro":1,"cks":21682,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"timeout","icmp_seq":2,"duplicate":false},{"type":"timeout","icmp_seq":3,"duplicate":false},{"type":"timeout","icmp_seq":4,"duplicate":false},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":40674,"flg":0,"off":0,"ttl":63,"pro":1,"cks":22461,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":31035,"flg":0,"off":0,"ttl":63,"pro":1,"cks":32100,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"destination_host_unreachable","bytes":92,"response_ip":"192.168.1.220","vr":4,"hl":5,"tos":0,"len":21504,"id":53536,"flg":0,"off":0,"ttl":63,"pro":1,"cks":9599,"src":"192.168.1.221","dst":"192.168.1.220"},{"type":"timeout","icmp_seq":5,"duplicate":false},{"type":"timeout","icmp_seq":6,"duplicate":false}]}

View File

@@ -0,0 +1,35 @@
PING 192.168.1.220 (192.168.1.220): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 567b 0 0000 3f 01 a024 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 2e6f 0 0000 3f 01 c830 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 a1ed 0 0000 3f 01 54b2 192.168.1.221 192.168.1.220
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
Request timeout for icmp_seq 4
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 9ee2 0 0000 3f 01 57bd 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 793b 0 0000 3f 01 7d64 192.168.1.221 192.168.1.220
92 bytes from fgt1.attlocal.net (192.168.1.220): Destination Host Unreachable
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 5400 d120 0 0000 3f 01 257f 192.168.1.221 192.168.1.220
Request timeout for icmp_seq 5
Request timeout for icmp_seq 6
--- 192.168.1.220 ping statistics ---
8 packets transmitted, 0 packets received, 100.0% packet loss

View File

@@ -0,0 +1 @@
{"source_ip":"::1","destination_ip":"::1","data_bytes":56,"pattern":null,"destination":"::1","packets_transmitted":3,"packets_received":3,"packet_loss_percent":0.0,"duplicates":0,"round_trip_ms_min":0.071,"round_trip_ms_avg":0.115,"round_trip_ms_max":0.153,"round_trip_ms_stddev":0.034,"responses":[{"type":"reply","bytes":16,"response_ip":"::1","icmp_seq":0,"ttl":64,"time_ms":0.071,"duplicate":false},{"type":"unparsable_line","unparsed_line":"16 bytes from ::1 strange error"},{"type":"reply","bytes":16,"response_ip":"::1","icmp_seq":2,"ttl":64,"time_ms":0.122,"duplicate":false}]}

View File

@@ -0,0 +1,9 @@
PING6(56=40+8+8 bytes) ::1 --> ::1
16 bytes from ::1, icmp_seq=0 hlim=64 time=0.071 ms
16 bytes from ::1, strange error
weird error message
16 bytes from ::1, icmp_seq=2 hlim=64 time=0.122 ms
--- ::1 ping6 statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/std-dev = 0.071/0.115/0.153/0.034 ms

View File

@@ -0,0 +1 @@
[{"file":"file name with spaces.txt","unix_device":16777220,"inode":161929661,"flags":"-rw-r--r--","links":1,"user":"kbrazil","group":"staff","rdev":0,"size":0,"access_time":"Aug 13 15:03:52 2021","modify_time":"Aug 13 14:37:03 2021","change_time":"Aug 13 14:37:03 2021","birth_time":"Aug 13 14:37:03 2021","block_size":4096,"blocks":0,"unix_flags":"0","access_time_epoch":1628892232,"access_time_epoch_utc":null,"modify_time_epoch":1628890623,"modify_time_epoch_utc":null,"change_time_epoch":1628890623,"change_time_epoch_utc":null,"birth_time_epoch":1628890623,"birth_time_epoch_utc":null}]

View File

@@ -0,0 +1,2 @@
16777220 161929661 -rw-r--r-- 1 kbrazil staff 0 0 "Aug 13 15:03:52 2021" "Aug 13 14:37:03 2021" "Aug 13 14:37:03 2021" "Aug 13 14:37:03 2021" 4096 0 0 file name with spaces.txt

View File

@@ -5,31 +5,31 @@ import jc.cli
class MyTests(unittest.TestCase):
def test_cli_generate_magic_command(self):
def test_cli_magic_parser(self):
commands = {
'jc -p systemctl list-sockets': 'systemctl list-sockets | jc --systemctl-ls -p',
'jc -p systemctl list-unit-files': 'systemctl list-unit-files | jc --systemctl-luf -p',
'jc -p pip list': 'pip list | jc --pip-list -p',
'jc -p pip3 list': 'pip3 list | jc --pip-list -p',
'jc -p pip show jc': 'pip show jc | jc --pip-show -p',
'jc -p pip3 show jc': 'pip3 show jc | jc --pip-show -p',
'jc -prd last': 'last | jc --last -prd',
'jc -prd lastb': 'lastb | jc --last -prd',
'jc -p airport -I': 'airport -I | jc --airport -p',
'jc -p -r airport -I': 'airport -I | jc --airport -pr',
'jc -prd airport -I': 'airport -I | jc --airport -prd',
'jc -p nonexistent command': 'nonexistent command',
'jc -ap': None,
'jc -a arp -a': None,
'jc -v': None,
'jc -h': None,
'jc -h --arp': None,
'jc -h arp': None,
'jc -h arp -a': None
'jc -p systemctl list-sockets': (True, ['systemctl', 'list-sockets'], '--systemctl-ls', ['p']),
'jc -p systemctl list-unit-files': (True, ['systemctl', 'list-unit-files'], '--systemctl-luf', ['p']),
'jc -p pip list': (True, ['pip', 'list'], '--pip-list', ['p']),
'jc -p pip3 list': (True, ['pip3', 'list'], '--pip-list', ['p']),
'jc -p pip show jc': (True, ['pip', 'show', 'jc'], '--pip-show', ['p']),
'jc -p pip3 show jc': (True, ['pip3', 'show', 'jc'], '--pip-show', ['p']),
'jc -prd last': (True, ['last'], '--last', ['p', 'r', 'd']),
'jc -prdd lastb': (True, ['lastb'], '--last', ['p', 'r', 'd', 'd']),
'jc -p airport -I': (True, ['airport', '-I'], '--airport', ['p']),
'jc -p -r airport -I': (True, ['airport', '-I'], '--airport', ['p', 'r']),
'jc -prd airport -I': (True, ['airport', '-I'], '--airport', ['p', 'r', 'd']),
'jc -p nonexistent command': (False, ['nonexistent', 'command'], None, ['p']),
'jc -ap': (False, None, None, []),
'jc -a arp -a': (False, None, None, []),
'jc -v': (False, None, None, []),
'jc -h': (False, None, None, []),
'jc -h --arp': (False, None, None, []),
'jc -h arp': (False, None, None, []),
'jc -h arp -a': (False, None, None, [])
}
for command, expected_command in commands.items():
self.assertEqual(jc.cli.generate_magic_command(command.split(' '))[1], expected_command)
self.assertEqual(jc.cli.magic_parser(command.split(' ')), expected_command)
def test_cli_set_env_colors(self):
if pygments.__version__.startswith('2.3.'):

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.date
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.dig
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):
@@ -55,10 +62,12 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/dig-axfr.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_dig_axfr = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/dig-noall-answer.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_dig_noall_answer = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/dig-answer-spaces.out'), 'r', encoding='utf-8') as f:
self.generic_dig_answer_spaces = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/dig-additional.out'), 'r', encoding='utf-8') as f:
self.generic_dig_additional = f.read()
@@ -123,10 +132,12 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/dig-axfr.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_dig_axfr_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/dig-noall-answer.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_dig_noall_answer_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/dig-answer-spaces.json'), 'r', encoding='utf-8') as f:
self.generic_dig_answer_spaces_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/dig-additional.json'), 'r', encoding='utf-8') as f:
self.generic_dig_additional_json = json.loads(f.read())
@@ -241,6 +252,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.dig.parse(self.osx_10_14_6_dig_axfr, quiet=True), self.osx_10_14_6_dig_axfr_json)
def test_dig_noall_answer_osx_10_14_6(self):
"""
Test 'dig +noall +answer' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.dig.parse(self.osx_10_14_6_dig_noall_answer, quiet=True), self.osx_10_14_6_dig_noall_answer_json)
def test_dig_answer_spaces(self):
"""
Test 'dig' with spaces in the answer data (e.g. TXT responses)

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.dir
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.last
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.ls
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):

View File

@@ -30,6 +30,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-O-p.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_O_p = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-O-p-unparsable.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_O_p_unparsable = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-O-D-p.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_O_D_p = f.read()
@@ -45,6 +48,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-dup.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_dup = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping-ip-O-unparsedlines.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping_ip_O_unparsedlines = f.read()
# ubuntu
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ping-ip-O.out'), 'r', encoding='utf-8') as f:
self.ubuntu_18_4_ping_ip_O = f.read()
@@ -157,6 +163,12 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip-unreachable.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_unreachable = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip-unknown-errors.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_unknown_errors = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping6-hostname-p.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping6_hostname_p = f.read()
@@ -175,6 +187,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping6-ip.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping6_ip = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping6-ip-unparsable.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping6_ip_unparsable = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip-dup.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_dup = f.read()
@@ -209,6 +224,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-O-p.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_O_p_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-O-p-unparsable.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_O_p_unparsable_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-O-D-p.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_O_D_p_json = json.loads(f.read())
@@ -224,6 +242,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping6-ip-dup.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping6_ip_dup_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ping-ip-O-unparsedlines.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_ping_ip_O_unparsedlines_json = json.loads(f.read())
# ubunutu
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ping-ip-O.json'), 'r', encoding='utf-8') as f:
self.ubuntu_18_4_ping_ip_O_json = json.loads(f.read())
@@ -336,6 +357,12 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip-unreachable.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_unreachable_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip-unknown-errors.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_unknown_errors_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping6-hostname-p.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping6_hostname_p_json = json.loads(f.read())
@@ -354,6 +381,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping6-ip.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping6_ip_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping6-ip-unparsable.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping6_ip_unparsable_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ping-ip-dup.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_ping_ip_dup_json = json.loads(f.read())
@@ -409,6 +439,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.ping.parse(self.centos_7_7_ping6_ip_O_p, quiet=True), self.centos_7_7_ping6_ip_O_p_json)
def test_ping6_ip_O_p_unparsable_centos_7_7(self):
"""
Test 'ping6 <ip> -O -p' with unparsable lines on Centos 7.7
"""
self.assertEqual(jc.parsers.ping.parse(self.centos_7_7_ping6_ip_O_p_unparsable, quiet=True), self.centos_7_7_ping6_ip_O_p_unparsable_json)
def test_ping6_ip_O_D_p_centos_7_7(self):
"""
Test 'ping6 <ip> -O -D -p' on Centos 7.7
@@ -439,6 +475,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.ping.parse(self.centos_7_7_ping6_ip_dup, quiet=True), self.centos_7_7_ping6_ip_dup_json)
def test_ping_ip_O_unparsedlines_centos_7_7(self):
"""
Test 'ping <ip> -O' on Centos 7.7 with unparsable lines and error messages
"""
self.assertEqual(jc.parsers.ping.parse(self.centos_7_7_ping_ip_O_unparsedlines, quiet=True), self.centos_7_7_ping_ip_O_unparsedlines_json)
def test_ping_ip_O_ubuntu_18_4(self):
"""
Test 'ping <ip> -O' on Ubuntu 18.4
@@ -651,10 +693,22 @@ class MyTests(unittest.TestCase):
def test_ping_ip_osx_10_14_6(self):
"""
Test 'ping6 <ip>' on osx 10.14.6
Test 'ping <ip>' on osx 10.14.6
"""
self.assertEqual(jc.parsers.ping.parse(self.osx_10_14_6_ping_ip, quiet=True), self.osx_10_14_6_ping_ip_json)
def test_ping_ip_unreachable_osx_10_14_6(self):
"""
Test 'ping <ip>' with host unreachable error on osx 10.14.6
"""
self.assertEqual(jc.parsers.ping.parse(self.osx_10_14_6_ping_ip_unreachable, quiet=True), self.osx_10_14_6_ping_ip_unreachable_json)
def test_ping_ip_unknown_errors_osx_10_14_6(self):
"""
Test 'ping <ip>' with unknown/unparsable errors on osx 10.14.6
"""
self.assertEqual(jc.parsers.ping.parse(self.osx_10_14_6_ping_ip_unknown_errors, quiet=True), self.osx_10_14_6_ping_ip_unknown_errors_json)
def test_ping6_hostname_p_osx_10_14_6(self):
"""
Test 'ping6 <hostname> -p' on osx 10.14.6
@@ -691,6 +745,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.ping.parse(self.osx_10_14_6_ping6_ip, quiet=True), self.osx_10_14_6_ping6_ip_json)
def test_ping6_ip_unparsable_osx_10_14_6(self):
"""
Test 'ping6 <ip>' with unparsable lines on osx 10.14.6
"""
self.assertEqual(jc.parsers.ping.parse(self.osx_10_14_6_ping6_ip_unparsable, quiet=True), self.osx_10_14_6_ping6_ip_unparsable_json)
def test_ping_ip_dup_osx_10_14_6(self):
"""
Test 'ping <ip>' to broadcast IP to get duplicate replies on osx 10.14.6

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import unittest
import json
import jc.parsers.rpm_qi
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):

192
tests/test_sfdisk.py Normal file
View File

@@ -0,0 +1,192 @@
import os
import json
import unittest
import jc.parsers.sfdisk
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
def setUp(self):
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-l.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_l = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-l-multi.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_l_multi = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-d.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_d = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-d-multi.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_d_multi = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luB.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_luB = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luM.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_luM = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luS.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_luS = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-l.out'), 'r', encoding='utf-8') as f:
self.centos_8_sfdisk_l = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-F.out'), 'r', encoding='utf-8') as f:
self.centos_8_sfdisk_F = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l.out'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_l = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l2.out'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_l2 = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l3.out'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_l3 = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-F.out'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_F = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-F2.out'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_F2 = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-l.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_l_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-l-multi.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_l_multi_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-d.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_d_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-d-multi.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_d_multi_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luB.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_luB_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luM.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_luM_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luS.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_sfdisk_luS_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-l.json'), 'r', encoding='utf-8') as f:
self.centos_8_sfdisk_l_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-F.json'), 'r', encoding='utf-8') as f:
self.centos_8_sfdisk_F_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l.json'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_l_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l2.json'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_l2_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l3.json'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_l3_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-F.json'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_F_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-F2.json'), 'r', encoding='utf-8') as f:
self.debian_10_sfdisk_F2_json = json.loads(f.read())
def test_sfdisk_nodata(self):
"""
Test 'sfdisk' with no data
"""
self.assertEqual(jc.parsers.sfdisk.parse('', quiet=True), [])
def test_sfdisk_l_centos_7_7(self):
"""
Test 'sfdisk -l' on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_l, quiet=True), self.centos_7_7_sfdisk_l_json)
def test_sfdisk_l_multi_centos_7_7(self):
"""
Test 'sfdisk -l' with multiple disk data on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_l_multi, quiet=True), self.centos_7_7_sfdisk_l_multi_json)
def test_sfdisk_d_centos_7_7(self):
"""
Test 'sfdisk -d' on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_d, quiet=True), self.centos_7_7_sfdisk_d_json)
def test_sfdisk_d_multi_centos_7_7(self):
"""
Test 'sfdisk -d' with multiple disk data on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_d_multi, quiet=True), self.centos_7_7_sfdisk_d_multi_json)
def test_sfdisk_luB_centos_7_7(self):
"""
Test 'sfdisk -luB' on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_luB, quiet=True), self.centos_7_7_sfdisk_luB_json)
def test_sfdisk_luM_centos_7_7(self):
"""
Test 'sfdisk -luM' on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_luM, quiet=True), self.centos_7_7_sfdisk_luM_json)
def test_sfdisk_luS_centos_7_7(self):
"""
Test 'sfdisk -luS' on Centos 7.7
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_luS, quiet=True), self.centos_7_7_sfdisk_luS_json)
def test_sfdisk_l_centos_8(self):
"""
Test 'sfdisk -l' on Centos 8
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_8_sfdisk_l, quiet=True), self.centos_8_sfdisk_l_json)
def test_sfdisk_F_centos_8(self):
"""
Test 'sfdisk -F' on Centos 8
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.centos_8_sfdisk_F, quiet=True), self.centos_8_sfdisk_F_json)
def test_sfdisk_l_debian_10(self):
"""
Test 'sfdisk -l' on Debian 10
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_l, quiet=True), self.debian_10_sfdisk_l_json)
def test_sfdisk_l2_debian_10(self):
"""
Test 'sfdisk -l' on Debian 10 (second example)
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_l2, quiet=True), self.debian_10_sfdisk_l2_json)
def test_sfdisk_l3_debian_10(self):
"""
Test 'sfdisk -l' on Debian 10 (third example)
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_l3, quiet=True), self.debian_10_sfdisk_l3_json)
def test_sfdisk_F_debian10(self):
"""
Test 'sfdisk -F' on Debian 10
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_F, quiet=True), self.debian_10_sfdisk_F_json)
def test_sfdisk_F2_debian10(self):
"""
Test 'sfdisk -F' on Debian 10 (second example)
"""
self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_F2, quiet=True), self.debian_10_sfdisk_F2_json)
if __name__ == '__main__':
unittest.main()

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.stat
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):
@@ -19,6 +26,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat-filename-with-spaces.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat_filename_with_spaces = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/stat.out'), 'r', encoding='utf-8') as f:
self.freebsd12_stat = f.read()
@@ -32,6 +42,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat-filename-with-spaces.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat_filename_with_spaces_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/stat.json'), 'r', encoding='utf-8') as f:
self.freebsd12_stat_json = json.loads(f.read())
@@ -59,6 +72,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.stat.parse(self.osx_10_14_6_stat, quiet=True), self.osx_10_14_6_stat_json)
def test_stat_filename_with_spaces_osx_10_14_6(self):
"""
Test 'stat' filename with spaces on OSX 10.14.6
"""
self.assertEqual(jc.parsers.stat.parse(self.osx_10_14_6_stat_filename_with_spaces, quiet=True), self.osx_10_14_6_stat_filename_with_spaces_json)
def test_stat_freebsd12(self):
"""
Test 'stat /foo/*' on FreeBSD12

View File

@@ -1,10 +1,17 @@
import json
import os
import sys
import time
import unittest
import jc.parsers.systeminfo
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):
test_files = [

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.upower
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):

View File

@@ -1,6 +1,14 @@
import os
import sys
import time
import unittest
import jc.utils
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):
@@ -17,6 +25,10 @@ class MyTests(unittest.TestCase):
# Windows english format wint non-UTC tz (found in systeminfo cli output)
'3/22/2021, 1:15:51 PM (UTC-0600)': {'string': '3/22/2021, 1:15:51 PM (UTC-0600)', 'format': 1700, 'naive': 1616444151, 'utc': None},
# Windows english format with UTC tz (found in systeminfo cli output)
'3/22/2021, 1:15:51 PM (UTC)': {'string': '3/22/2021, 1:15:51 PM (UTC)', 'format': 1705, 'naive': 1616444151, 'utc': 1616418951},
# Windows english format with UTC tz in long-form (found in systeminfo cli output)
'3/22/2021, 1:15:51 PM (Coordinated Universal Time)': {'string': '3/22/2021, 1:15:51 PM (Coordinated Universal Time)', 'format': 1705, 'naive': 1616444151, 'utc': 1616418951},
# Windows english format with UTC tz (found in systeminfo cli output)
'3/22/2021, 1:15:51 PM (UTC+0000)': {'string': '3/22/2021, 1:15:51 PM (UTC+0000)', 'format': 1710, 'naive': 1616444151, 'utc': 1616418951},
# en_US.UTF-8 local format (found in upower cli output)
'Tue 23 Mar 2021 04:12:11 PM UTC': {'string': 'Tue 23 Mar 2021 04:12:11 PM UTC', 'format': 2000, 'naive': 1616541131, 'utc': 1616515931},

View File

@@ -1,10 +1,17 @@
import os
import sys
import time
import json
import unittest
import jc.parsers.who
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
class MyTests(unittest.TestCase):