From a7b6057c5352252ea24e4b68ae6e353953783a1e Mon Sep 17 00:00:00 2001 From: jtpereyda Date: Thu, 13 Aug 2015 12:18:01 -0700 Subject: [PATCH 1/2] Minimal fixes to get network_monitor.py running. Moved two declarations and fixed a variable name. --- network_monitor.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/network_monitor.py b/network_monitor.py index e803f87..40d86ca 100644 --- a/network_monitor.py +++ b/network_monitor.py @@ -27,9 +27,10 @@ def create_usage(): [-l|--log_level LEVEL] log level (default 1), increase for more verbosity [--port PORT] TCP port to bind this agent to -Network Device List:""" +Network Device List: +""" for index, pcapy_device in enumerate(pcapy.findalldevs()): - IFS.append(device) + IFS.append(pcapy_device) # if we are on windows, try and resolve the device UUID into an IP address. if sys.platform.startswith("win"): import _winreg @@ -218,9 +219,10 @@ def set_log_path(self, new_log_path): if __name__ == "__main__": + IFS = [] + device = None usage_message = create_usage() rpc_port = 26001 - IFS = [] opts = None # parse command line options. @@ -229,7 +231,6 @@ def set_log_path(self, new_log_path): except getopt.GetoptError: log_error(usage_message) - device = None pcap_filter = "" log_path = "./" log_level = 1 From a26638dfe46e14d6f7f6fa5b5b4f3f4a04a47027 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 13 Aug 2015 15:49:10 -0700 Subject: [PATCH 2/2] Refactored create_usage and main code to avoid global variables. Removed some warnings. --- network_monitor.py | 40 ++++++++++++++++++++++++++++++++-------- setup.py | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/network_monitor.py b/network_monitor.py index 40d86ca..4286ba8 100644 --- a/network_monitor.py +++ b/network_monitor.py @@ -19,7 +19,29 @@ def log_error(message=None): sys.exit(1) -def create_usage(): +def get_ifs(): + """ + Get a list of network interfaces on the system. + + :rtype : list[str] + :return: List of network interfaces. + """ + ifs = [] + for index, pcapy_device in enumerate(pcapy.findalldevs()): + ifs.append(pcapy_device) + return ifs + + +def create_usage(ifs): + """ + Return usage string. + + :type ifs: list[str] + :param ifs: List of network interfaces to include in help text. + + :rtype : str + :return: Usage text. + """ message = """USAGE: network_monitor.py <-d|--device DEVICE #> device to sniff on (see list below) [-f|--filter PCAP FILTER] BPF filter string @@ -29,8 +51,7 @@ def create_usage(): Network Device List: """ - for index, pcapy_device in enumerate(pcapy.findalldevs()): - IFS.append(pcapy_device) + for index, pcapy_device in enumerate(ifs): # if we are on windows, try and resolve the device UUID into an IP address. if sys.platform.startswith("win"): import _winreg @@ -218,10 +239,9 @@ def set_log_path(self, new_log_path): self.log_path = new_log_path -if __name__ == "__main__": - IFS = [] - device = None - usage_message = create_usage() +def main(): + ifs = get_ifs() + usage_message = create_usage(ifs) rpc_port = 26001 opts = None @@ -231,13 +251,14 @@ def set_log_path(self, new_log_path): except getopt.GetoptError: log_error(usage_message) + device = None pcap_filter = "" log_path = "./" log_level = 1 for opt, arg in opts: if opt in ("-d", "--device"): - device = IFS[int(arg)] + device = ifs[int(arg)] if opt in ("-f", "--filter"): pcap_filter = arg if opt in ("-P", "--log_path"): @@ -255,3 +276,6 @@ def set_log_path(self, new_log_path): servlet.serve_forever() except: pass + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/setup.py b/setup.py index 1161a97..b41003f 100644 --- a/setup.py +++ b/setup.py @@ -20,5 +20,5 @@ 'utils': './utils', 'web': './web' }, - install_requires=['pydot2==1.0.33', 'tornado==4.0.2', 'Flask==0.10.1'] + install_requires=['pydot2==1.0.33', 'tornado==4.0.2', 'Flask==0.10.1', 'pcapy', 'impacket'] ) \ No newline at end of file