mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add hosts parser
This commit is contained in:
@ -14,6 +14,7 @@ import jc.parsers.dig
|
|||||||
import jc.parsers.env
|
import jc.parsers.env
|
||||||
import jc.parsers.free
|
import jc.parsers.free
|
||||||
import jc.parsers.history
|
import jc.parsers.history
|
||||||
|
import jc.parsers.hosts
|
||||||
import jc.parsers.ifconfig
|
import jc.parsers.ifconfig
|
||||||
import jc.parsers.iptables
|
import jc.parsers.iptables
|
||||||
import jc.parsers.jobs
|
import jc.parsers.jobs
|
||||||
@ -49,6 +50,7 @@ def helptext(message):
|
|||||||
--env env parser
|
--env env parser
|
||||||
--free free parser
|
--free free parser
|
||||||
--history history parser
|
--history history parser
|
||||||
|
--hosts /etc/hosts file parser
|
||||||
--ifconfig iconfig parser
|
--ifconfig iconfig parser
|
||||||
--iptables iptables parser
|
--iptables iptables parser
|
||||||
--jobs jobs parser
|
--jobs jobs parser
|
||||||
@ -112,6 +114,7 @@ def main():
|
|||||||
'--env': jc.parsers.env.parse,
|
'--env': jc.parsers.env.parse,
|
||||||
'--free': jc.parsers.free.parse,
|
'--free': jc.parsers.free.parse,
|
||||||
'--history': jc.parsers.history.parse,
|
'--history': jc.parsers.history.parse,
|
||||||
|
'--hosts': jc.parsers.hosts.parse,
|
||||||
'--ifconfig': jc.parsers.ifconfig.parse,
|
'--ifconfig': jc.parsers.ifconfig.parse,
|
||||||
'--iptables': jc.parsers.iptables.parse,
|
'--iptables': jc.parsers.iptables.parse,
|
||||||
'--jobs': jc.parsers.jobs.parse,
|
'--jobs': jc.parsers.jobs.parse,
|
||||||
|
89
jc/parsers/hosts.py
Normal file
89
jc/parsers/hosts.py
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
"""jc - JSON CLI output utility hosts Parser
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
specify --hosts as the first argument if the piped input is coming from hosts
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ hosts | jc --hosts -p
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ hosts | jc --hosts -p -r
|
||||||
|
[]
|
||||||
|
"""
|
||||||
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
|
def process(proc_data):
|
||||||
|
"""
|
||||||
|
Final processing to conform to the schema.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
proc_data: (dictionary) raw structured data to process
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
dictionary structured data with the following schema:
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"hosts": string,
|
||||||
|
"bar": boolean,
|
||||||
|
"baz": integer
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"""
|
||||||
|
|
||||||
|
# rebuild output for added semantic information
|
||||||
|
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:
|
||||||
|
|
||||||
|
dictionary raw or processed structured data
|
||||||
|
"""
|
||||||
|
|
||||||
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
||||||
|
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
||||||
|
|
||||||
|
if not quiet:
|
||||||
|
jc.utils.compatibility(__name__, compatible)
|
||||||
|
|
||||||
|
raw_output = []
|
||||||
|
cleandata = data.splitlines()
|
||||||
|
|
||||||
|
# Clear any blank lines
|
||||||
|
cleandata = list(filter(None, cleandata))
|
||||||
|
|
||||||
|
if cleandata:
|
||||||
|
for line in cleandata:
|
||||||
|
output_line = {}
|
||||||
|
# ignore commented lines
|
||||||
|
if line.strip().find('#') == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
line_list = line.split(maxsplit=1)
|
||||||
|
ip = line_list[0]
|
||||||
|
hosts = line_list[1]
|
||||||
|
hosts_list = hosts.split()
|
||||||
|
|
||||||
|
output_line['ip'] = ip
|
||||||
|
output_line['hostname'] = hosts_list
|
||||||
|
|
||||||
|
raw_output.append(output_line)
|
||||||
|
|
||||||
|
if raw:
|
||||||
|
return raw_output
|
||||||
|
else:
|
||||||
|
return process(raw_output)
|
Reference in New Issue
Block a user