mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-08-10 22:41:51 +02:00
add lsof parser
This commit is contained in:
5
jc/jc.py
5
jc/jc.py
@@ -14,6 +14,7 @@ import jc.parsers.iptables
|
||||
import jc.parsers.jobs
|
||||
import jc.parsers.ls
|
||||
import jc.parsers.lsblk
|
||||
import jc.parsers.lsof
|
||||
import jc.parsers.mount
|
||||
import jc.parsers.netstat
|
||||
import jc.parsers.ps
|
||||
@@ -32,6 +33,7 @@ def helptext():
|
||||
print(' --jobs jobs parser', file=sys.stderr)
|
||||
print(' --ls ls parser', file=sys.stderr)
|
||||
print(' --lsblk lsblk parser', file=sys.stderr)
|
||||
print(' --lsof lsof parser', file=sys.stderr)
|
||||
print(' --mount mount parser', file=sys.stderr)
|
||||
print(' --netstat netstat parser', file=sys.stderr)
|
||||
print(' --ps ps parser', file=sys.stderr)
|
||||
@@ -81,6 +83,9 @@ def main():
|
||||
elif '--lsblk' in sys.argv:
|
||||
result = jc.parsers.lsblk.parse(data)
|
||||
|
||||
elif '--lsof' in sys.argv:
|
||||
result = jc.parsers.lsof.parse(data)
|
||||
|
||||
elif '--mount' in sys.argv:
|
||||
result = jc.parsers.mount.parse(data)
|
||||
|
||||
|
54
jc/parsers/lsof.py
Normal file
54
jc/parsers/lsof.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""jc - JSON CLI output utility lsof Parser
|
||||
|
||||
Usage:
|
||||
specify --lsof as the first argument if the piped input is coming from lsof
|
||||
|
||||
Limitations:
|
||||
No additional columns are supported
|
||||
|
||||
Example:
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def parse(data):
|
||||
output = []
|
||||
|
||||
linedata = data.splitlines()
|
||||
|
||||
# Clear any blank lines
|
||||
cleandata = list(filter(None, linedata))
|
||||
|
||||
if cleandata:
|
||||
|
||||
# find column value of last character of each header
|
||||
header_row = cleandata.pop(0)
|
||||
headers = header_row.split()
|
||||
header_spec = []
|
||||
|
||||
for i, h in enumerate(headers):
|
||||
# header tuple is (index, header_name, col)
|
||||
header_spec.append((i, h, header_row.find(h) + len(h)))
|
||||
|
||||
# parse lines
|
||||
for entry in cleandata:
|
||||
output_line = {}
|
||||
|
||||
# normalize data by inserting -- for missing data
|
||||
temp_line = entry.split(maxsplit=len(headers) - 1)
|
||||
|
||||
for spec in header_spec:
|
||||
if spec[1] == 'COMMAND' or spec[1] == 'NAME':
|
||||
continue
|
||||
if entry[spec[2] - 1] == ' ':
|
||||
temp_line.insert(spec[0], '--')
|
||||
|
||||
name = ' '.join(temp_line[9:])
|
||||
fixed_line = temp_line[0:9]
|
||||
fixed_line.append(name)
|
||||
|
||||
output_line = dict(zip(headers, fixed_line))
|
||||
output.append(output_line)
|
||||
|
||||
return output
|
Reference in New Issue
Block a user