summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2023-08-25 20:08:01 +0200
committerToni Uhlig <matzeton@googlemail.com>2023-08-27 20:08:01 +0200
commita7bd3570b03f6b2fdc9bab09c956193708723cbf (patch)
treead6f22edea20d3d718459ca6b3da8ee121e59b73 /examples
parentb01498f011eac9b91c076901ffb5c9c04e7691c0 (diff)
Enable custom JSON filter expressions for Python scripts.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/py-flow-info/flow-info.py28
-rwxr-xr-xexamples/py-json-stdout/json-stdout.py3
2 files changed, 24 insertions, 7 deletions
diff --git a/examples/py-flow-info/flow-info.py b/examples/py-flow-info/flow-info.py
index 5bd865a9d..7ef74c6a6 100755
--- a/examples/py-flow-info/flow-info.py
+++ b/examples/py-flow-info/flow-info.py
@@ -120,11 +120,26 @@ class Stats:
flow_count += 1
current_flow = instances[alias][source].flows[flow_id]
- flow_tot_l4_payload_len += current_flow.flow_src_tot_l4_payload_len + current_flow.flow_dst_tot_l4_payload_len
- risky += 1 if len(current_flow.flow_risk) > 0 else 0
- midstream += 1 if current_flow.midstream != 0 else 0
- guessed += 1 if current_flow.guessed != 0 else 0
- not_detected = 1 if current_flow.not_detected != 0 else 0
+ try:
+ flow_src_tot_l4_payload_len = current_flow.flow_src_tot_l4_payload_len
+ flow_dst_tot_l4_payload_len = current_flow.flow_dst_tot_l4_payload_len
+ flow_risk = current_flow.flow_risk
+ midstream = current_flow.midstream
+ guessed = current_flow.guessed
+ not_detected = current_flow.not_detected
+ except AttributeError:
+ flow_src_tot_l4_payload_len = 0
+ flow_dst_tot_l4_payload_len = 0
+ flow_risk = []
+ midstream = 0
+ guessed = 0
+ not_detected = 0
+
+ flow_tot_l4_payload_len += flow_src_tot_l4_payload_len + flow_dst_tot_l4_payload_len
+ risky += 1 if len(flow_risk) > 0 else 0
+ midstream += 1 if midstream != 0 else 0
+ guessed += 1 if guessed != 0 else 0
+ not_detected = 1 if not_detected != 0 else 0
return alias_count, source_count, flow_count, \
flow_tot_l4_payload_len, \
@@ -519,7 +534,7 @@ def onJsonLineRecvd(json_dict, instance, current_flow, global_user_data):
return True
if __name__ == '__main__':
- argparser = nDPIsrvd.defaultArgumentParser('Prettify and print events using the nDPIsrvd Python interface.')
+ argparser = nDPIsrvd.defaultArgumentParser('Prettify and print events using the nDPIsrvd Python interface.', True)
argparser.add_argument('--no-color', action='store_true', default=False,
help='Disable all terminal colors.')
argparser.add_argument('--no-statusbar', action='store_true', default=False,
@@ -577,6 +592,7 @@ if __name__ == '__main__':
sys.stderr.write('Connecting to {} ..\n'.format(address[0]+':'+str(address[1]) if type(address) is tuple else address))
nsock = nDPIsrvdSocket()
+ nDPIsrvd.prepareJsonFilter(args, nsock)
nsock.connect(address)
nsock.timeout(1.0)
stats = Stats(nsock)
diff --git a/examples/py-json-stdout/json-stdout.py b/examples/py-json-stdout/json-stdout.py
index f1aa51b5b..cde22cd9b 100755
--- a/examples/py-json-stdout/json-stdout.py
+++ b/examples/py-json-stdout/json-stdout.py
@@ -15,7 +15,7 @@ def onJsonLineRecvd(json_dict, instance, current_flow, global_user_data):
return True
if __name__ == '__main__':
- argparser = nDPIsrvd.defaultArgumentParser()
+ argparser = nDPIsrvd.defaultArgumentParser('Plain and simple nDPIsrvd JSON event printer with filter capabilities.', True)
args = argparser.parse_args()
address = nDPIsrvd.validateAddress(args)
@@ -23,5 +23,6 @@ if __name__ == '__main__':
sys.stderr.write('Connecting to {} ..\n'.format(address[0]+':'+str(address[1]) if type(address) is tuple else address))
nsock = nDPIsrvdSocket()
+ nDPIsrvd.prepareJsonFilter(args, nsock)
nsock.connect(address)
nsock.loop(onJsonLineRecvd, None, None)