Verified Commit 9968a33b authored by Martin Petráček's avatar Martin Petráček
Browse files

show: arguments (using argparse)

parent afe584fe
......@@ -10,18 +10,9 @@ import errno
import re
import json
import subprocess
import argparse
import socket
if (len(sys.argv)>=2):
query=sys.argv[1]
else:
query='{}'
try:
response=subprocess.check_output(['/usr/bin/python3', '/usr/libexec/pakon-light/dump.py', query]).decode()
except OSError:
print("error calling dump.py")
sys.exit(1)
def print_table(table):
col_width = [max(len(str(x)) for x in col) for col in zip(*table)]
for line in table:
......@@ -35,6 +26,65 @@ def size_fmt(num):
num /= 1024.0
return "%.0f%sB" % (num, 'Ti')
def mac_valid(string):
if not re.match("[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}$", string.lower()):
msg = "%r is not a valid MAC address" % string
raise argparse.ArgumentTypeError(msg)
return string.lower()
def arg_parser():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--start",
help="Start time - number of seconds before NOW",
type=int
)
parser.add_argument("-e", "--end",
help="Start time - number of seconds before NOW",
type=int
)
parser.add_argument("-m", "--mac",
help="Show just records for specified MAC address (multiple such options could be specified)",
action='append',
type=mac_valid
)
parser.add_argument("-H", "--hostname",
help="Show just records for specified hostname (multiple such options could be specified)",
action='append'
)
parser.add_argument("--no-filter",
action='store_true',
help="Don't apply filter to output (hides tracking/advertisements/other rubbish) - enabled by default"
)
parser.add_argument("-A", "--aggregate",
action='store_true',
help="Display aggregate records (instead of timeline)"
)
return parser.parse_args()
args=arg_parser()
query={}
if args.start:
query["start"]=args.start
if args.end:
query["end"]=args.end
if args.mac:
query["mac"]=args.mac
if args.hostname:
query["hostname"]=args.hostname
if args.no_filter:
query["filter"]=False
if args.aggregate:
query["aggregate"]=True
query=json.dumps(query)
try:
response=subprocess.check_output(['/usr/bin/python3', '/usr/libexec/pakon-light/dump.py', query]).decode()
except OSError:
print("error calling dump.py")
sys.exit(1)
data=json.loads(response)
for i in range(len(data)):
if data[i][1]==0:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment