From 5418955b0bd82d7e5adbe4b68b56e8578acd41da Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 3 Apr 2020 21:01:34 +0200 Subject: [PATCH 01/47] fix: Add buffer_size as parameter --- pythonping/network.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pythonping/network.py b/pythonping/network.py index 4c06ca9..cb8c6ce 100644 --- a/pythonping/network.py +++ b/pythonping/network.py @@ -4,10 +4,9 @@ class Socket: - BUFFER_SIZE = 1024 DONT_FRAGMENT = (socket.SOL_IP, 10, 1) # Option value for raw socket - def __init__(self, destination, protocol, source=None, options=()): + def __init__(self, destination, protocol, source=None, options=(), buffer_size=2048): """Creates a network socket to exchange messages :param destination: Destination IP address @@ -17,9 +16,12 @@ def __init__(self, destination, protocol, source=None, options=()): :param options: Options to set on the socket :type options: tuple :param source: Source IP to use - implemented in future releases - :type source: Union[None, str]""" + :type source: Union[None, str] + :param buffer_size: Size in bytes of the listening buffer for incoming packets (replies) + :type buffer_size: int""" self.destination = socket.gethostbyname(destination) self.protocol = socket.getprotobyname(protocol) + self.buffer_size = buffer_size if source is not None: raise NotImplementedError('PythonPing currently does not support specification of source IP') self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, self.protocol) @@ -49,7 +51,7 @@ def receive(self, timeout=2): if not data_ready[0]: # Timeout return b'', '', time_left - packet, source = self.socket.recvfrom(self.BUFFER_SIZE) + packet, source = self.socket.recvfrom(self.buffer_size) return packet, source, time_left def __del__(self): From 918572d54396ae21484bce444a939f9e0d079438 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 3 Apr 2020 21:09:45 +0200 Subject: [PATCH 02/47] fix: Create explicit error for dns issue #28 --- pythonping/network.py | 5 ++++- test/test_network.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/test_network.py diff --git a/pythonping/network.py b/pythonping/network.py index cb8c6ce..c58346f 100644 --- a/pythonping/network.py +++ b/pythonping/network.py @@ -19,7 +19,10 @@ def __init__(self, destination, protocol, source=None, options=(), buffer_size=2 :type source: Union[None, str] :param buffer_size: Size in bytes of the listening buffer for incoming packets (replies) :type buffer_size: int""" - self.destination = socket.gethostbyname(destination) + try: + self.destination = socket.gethostbyname(destination) + except socket.gaierror as e: + raise RuntimeError('Cannot resolve address "' + destination + '", try verify your DNS or host file') self.protocol = socket.getprotobyname(protocol) self.buffer_size = buffer_size if source is not None: diff --git a/test/test_network.py b/test/test_network.py new file mode 100644 index 0000000..22b3cca --- /dev/null +++ b/test/test_network.py @@ -0,0 +1,11 @@ +import unittest +from pythonping.network import Socket + + +class UtilsTestCase(unittest.TestCase): + """Tests for Socket class""" + + def test_raise_explicative_error_on_name_resolution_failure(self): + """Test a runtime error is generated if the name cannot be resolved""" + with self.assertRaises(RuntimeError): + Socket('cant.resolve.this.address.localhost', 'ip') From 6df99210f8c06c0ccf4ab92c51218287b22cac48 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 3 Apr 2020 21:12:14 +0200 Subject: [PATCH 03/47] Create CODE_OF_CONDUCT.md --- CODE_OF_CONDUCT.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..2d26031 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,76 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, sex characteristics, gender identity and expression, +level of experience, education, socio-economic status, nationality, personal +appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team by raising an issue on GitHub. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see +https://www.contributor-covenant.org/faq From c6be95fed24134a877453662db6b657bbda1e91a Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 3 Apr 2020 21:13:45 +0200 Subject: [PATCH 04/47] Update issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 38 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 ++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..dd84ea7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. From 4fd79b28ebe7798b1e6e35627f3c9c4324646143 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 3 Apr 2020 21:15:15 +0200 Subject: [PATCH 05/47] Create CONTRIBUTING.md --- CONTRIBUTING.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..cb2c0fd --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,17 @@ +# Contributing + +When contributing to this repository, please first discuss the change you wish to make via issue +here on GitHub. + +Please note we have a code of conduct, please follow it in all your interactions with the project. + +## Pull Request Process + +1. Ensure any install or build dependencies are removed before the end of the layer when doing a + build. +2. Update the README.md with details of changes to the interface, this includes new environment + variables, exposed ports, useful file locations and container parameters. +3. Increase the version numbers in any examples files and the README.md to the new version that this + Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/). +4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you + do not have permission to do that, you may request the second reviewer to merge it for you. From 84144ec8980c88277779a2ede80bd654cae1f6bd Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 3 Apr 2020 21:25:38 +0200 Subject: [PATCH 06/47] refactor: Prepare for release --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++-------- setup.py | 2 +- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 25162f4..b2d5026 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ # pythonping -PythonPing is simple way to ping in Python. With it, you can send ICMP Probes to remote devices like you would do from the terminal. PythonPing is modular, so that you can run it in a script as a standalone function, or integrate its components in a fully-fledged application. +PythonPing is simple way to ping in Python. With it, you can send ICMP Probes to remote devices like +you would do from the terminal. PythonPing is modular, so that you can run it in a script as a +standalone function, or integrate its components in a fully-fledged application. ## Basic Usage -The simplest usage of PythonPing is in a script. You can use the `ping` function to ping a target. If you want to see the output immediately, emulating what happens on the terminal, use the `verbose` flag as below. +The simplest usage of PythonPing is in a script. You can use the `ping` function to ping a target. +If you want to see the output immediately, emulating what happens on the terminal, use the +`verbose` flag as below. + ```python from pythonping import ping @@ -16,17 +21,46 @@ Reply from 127.0.0.1, 9 bytes in 0.14ms Reply from 127.0.0.1, 9 bytes in 0.12ms Reply from 127.0.0.1, 9 bytes in 0.12ms ``` -Regardless of the verbose mode, the `ping` function will always return a `ResponseList` object. This is a special iterable object, containing a list of `Response` items. In each response, you can find the packet received and some meta information, like the time it took to receive the response and any error message. + +Regardless of the verbose mode, the `ping` function will always return a `ResponseList` object. +This is a special iterable object, containing a list of `Response` items. In each response, you can +find the packet received and some meta information, like the time it took to receive the response +and any error message. + You can also tune your ping by using some of its additional parameters: * `size` is an integer that allows you to specify the size of the ICMP payload you desire -* `timeout` is the number of seconds you wish to wait for a response, before assuming the target is unreachable +* `timeout` is the number of seconds you wish to wait for a response, before assuming the target +is unreachable * `payload` allows you to use a specific payload (bytes) * `count` specify allows you to define how many ICMP packets to send -* `sweep_start` and `sweep_end` allows you to perform a ping sweep, starting from payload size defined in `sweep_start` and growing up to size defined in `sweep_end`. Here, we repeat the payload you provided to match the desired size, or we generate a random one if no payload was provided. Note that if you defined `size`, these two fields will be ignored +* `sweep_start` and `sweep_end` allows you to perform a ping sweep, starting from payload size +defined in `sweep_start` and growing up to size defined in `sweep_end`. Here, we repeat the payload +you provided to match the desired size, or we generate a random one if no payload was provided. +Note that if you defined `size`, these two fields will be ignored * `df` is a flag that, if set to True, will enable the *Don't Fragment* flag in the IP header * `verbose` enables the verbose mode, printing output to a stream (see `out`) -* `out` is the target stream of verbose mode. If you enable the verbose mode and do not provide `out`, verbose output will be send to the `sys.stdout` stream. You may want to use a file here, for example. +* `out` is the target stream of verbose mode. If you enable the verbose mode and do not provide +`out`, verbose output will be send to the `sys.stdout` stream. You may want to use a file here. + +## FAQ +### Do I need privileged mode or root? +Yes, you need to be root to use pythonping. + +### Why do I need to be root to use pythonping? +All operating systems allow programs to create TCP or UDP sockets without requiring particular +permissions. However, ping runs in ICMP (which is neither TCP or UDP). This means we have to create +raw IP packets, and sniff the traffic on the network card. +**Operating systems are designed to require root for such operations**. This is because having +unrestricted access to the NIC can expose the user to risks if the application running has bad +intentions. This is not the case with pythonping of course, but nonetheless we need this capability +to create custom IP packets. Unfortunately, there is simply no other way to create ICMP packets. ## Advanced Usage -If you wish to extend PythonPing, or integrate it in your application, we recommend to use the classes that are part of Python Ping instead of the `ping` function. -`executor.Communicator` handles the communication with the target device, it takes care of sending ICMP requests and processing responses. It ultimately produces the `executor.ResponseList` object. The `Communicator` needs to know a target and which payloads to send to the remote device. For that, we have several classes in the `payload_provider` module. You may want to create your own provider by extending `payload_provider.PayloadProvider`. If you are interested in that, you should check the documentation of both `executor` and `payload_provider` module. +If you wish to extend PythonPing, or integrate it in your application, we recommend to use the +classes that are part of Python Ping instead of the `ping` function. `executor.Communicator` +handles the communication with the target device, it takes care of sending ICMP requests and +processing responses. It ultimately produces the `executor.ResponseList` object. The `Communicator` +needs to know a target and which payloads to send to the remote device. For that, we have several +classes in the `payload_provider` module. You may want to create your own provider by extending +`payload_provider.PayloadProvider`. If you are interested in that, you should check the +documentation of both `executor` and `payload_provider` module. diff --git a/setup.py b/setup.py index ff66ff7..4839355 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.8', + version='1.0.9', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From bce42bcc042f06fd28e33807cc07ff1c045fb29b Mon Sep 17 00:00:00 2001 From: Arlind Hoxha Date: Tue, 5 May 2020 14:09:09 +0200 Subject: [PATCH 07/47] Fix error handling when cannot open socket. Useful for detecting packetloss --- pythonping/network.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonping/network.py b/pythonping/network.py index c58346f..0c25e2f 100644 --- a/pythonping/network.py +++ b/pythonping/network.py @@ -59,7 +59,7 @@ def receive(self, timeout=2): def __del__(self): try: - if self.socket: + if hasattr(self, "socket") and self.socket: self.socket.close() except AttributeError: raise AttributeError("Attribute error because of failed socket init. Make sure you have the root privilege." diff --git a/setup.py b/setup.py index 4839355..b82e381 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.9', + version='1.1.0', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From df5eb31ed9c4b4c973aef3d6fd6ac3015b9921bd Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Tue, 5 May 2020 19:10:28 +0200 Subject: [PATCH 08/47] refactor: Fix versioning --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b82e381..53633bd 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.1.0', + version='1.0.10', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From b2536f20ad265135d6f713f8ba451cae1f73e1d0 Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Fri, 7 Aug 2020 16:39:31 -0500 Subject: [PATCH 09/47] update to allow for safe multithreading and payload matching on request reply as an option --- pythonping/__init__.py | 32 +++++++++++++++++++++++++++++--- pythonping/executor.py | 30 ++++++++++++++++++++++++------ test/test_ping.py | 3 +++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 3e9abe5..cc2d576 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -1,6 +1,12 @@ import sys from . import network, executor, payload_provider from .utils import random_text +from random import randint +from typing import List + + +# this needs to be available across all thread usages +SEED_IDs: List[int] = [] def ping(target, @@ -12,7 +18,8 @@ def ping(target, sweep_end=None, df=False, verbose=False, - out=sys.stdout): + out=sys.stdout, + match=False): """Pings a remote host and handles the responses :param target: The remote hostname or IP address to ping @@ -35,6 +42,11 @@ def ping(target, :type verbose: bool :param out: Stream to which redirect the verbose output :type out: stream + :param match: Do payload matching between request and reply (default behaviour follows that of windows which is + by packet identifier only, Linux behaviour counts a non equivalent payload in reply as fail, such as when pinging + 8.8.8.8 with 1000 bytes and reply is truncated to only the first 74 of request payload with packet identifiers + the same in request and reply) + :type match: bool :return: List with the result of each ping :rtype: executor.ResponseList""" provider = payload_provider.Repeat(b'', 0) @@ -49,6 +61,20 @@ def ping(target, options = () if df: options = network.Socket.DONT_FRAGMENT - comm = executor.Communicator(target, provider, timeout, socket_options=options, verbose=verbose, output=out) - comm.run() + + # Fix to allow for pythonping multithreaded usage; + # no need to protect this loop as no one will ever surpass 0xFFFF amount of threads + while True: + # seed_id needs to be less than or equal to 65535 (as original code was seed_id = getpid() & 0xFFFF) + seed_id = randint(0x1, 0xFFFF) + if seed_id not in SEED_IDs: + SEED_IDs.append(seed_id) + break + + comm = executor.Communicator(target, provider, timeout, socket_options=options, verbose=verbose, output=out, + seed_id=seed_id) + comm.run(match_payloads=match) + + SEED_IDs.remove(seed_id) + return comm.responses diff --git a/pythonping/executor.py b/pythonping/executor.py index 6c9b9de..05ff80b 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -261,13 +261,17 @@ def send_ping(self, packet_id, sequence_number, payload): payload=payload, identifier=packet_id, sequence_number=sequence_number).packet) - def listen_for(self, packet_id, timeout): + def listen_for(self, packet_id, timeout, payload_pattern=None): """Listens for a packet of a given id for a given timeout + (maintain pythonping legacy implementation (windows like) that didn't match payloads, + but allow for it to be set to be linux-like) :param packet_id: The ID of the packet to listen for, the same for request and response :type packet_id: int :param timeout: How long to listen for the specified packet, in seconds :type timeout: float + :param payload_pattern: Payload reply pattern to match to request, if set to None, match by ID only + :type payload_pattern: Union[None, bytes] :return: The response to the request with the specified packet_id :rtype: Response""" time_left = timeout @@ -278,9 +282,15 @@ def listen_for(self, packet_id, timeout): # If we actually received something if raw_packet != b'': response.unpack(raw_packet) + # to ensure legacy path (which was to not do payload inspection but only match packet identifiers), + # simply allow for it to be an always true in the legacy usage case + if payload_pattern is None: + payload_pattern = response.payload + # Ensure we have not unpacked the packet we sent (RHEL will also listen to outgoing packets) - if response.id == packet_id and response.message_type != icmp.Types.EchoRequest.type_id: - return Response(Message('', response, source_socket[0]), timeout-time_left) + if (response.id == packet_id and response.message_type != icmp.Types.EchoRequest.type_id + and response.payload == payload_pattern): + return Response(Message('', response, source_socket[0]), timeout - time_left) return Response(None, timeout) @staticmethod @@ -296,12 +306,20 @@ def increase_seq(sequence_number): sequence_number = 1 return sequence_number - def run(self): - """Performs all the pings and stores the responses""" + def run(self, match_payloads=False): + """Performs all the pings and stores the responses + (maintain legacy usage path of not matching payload but allow for it to be set) + + :param match_payloads: optional to set to True to make sure requests and replies have equivalent payloads + :type match_payloads: bool""" self.responses.clear() identifier = self.seed_id seq = 1 for payload in self.provider: self.send_ping(identifier, seq, payload) - self.responses.append(self.listen_for(identifier, self.timeout)) + if not match_payloads: + self.responses.append(self.listen_for(identifier, self.timeout)) + else: + self.responses.append(self.listen_for(identifier, self.timeout, payload)) + seq = self.increase_seq(seq) diff --git a/test/test_ping.py b/test/test_ping.py index 4799ff5..74976f7 100644 --- a/test/test_ping.py +++ b/test/test_ping.py @@ -10,3 +10,6 @@ def test_ping_execution(self): # NOTE, this may be considered an e2e test self.assertEqual(len(ping('10.127.0.1', count=4, size=10)), 4, 'Sent 4 pings to localhost, but not received 4 responses') + + self.assertEqual(ping('8.8.8.8', count=4, size=992, match=True).success(), False, + 'Sent 4 large pings to google DNS A, expected all to fail since they truncate large payloads') From 8cfdf60def488014c5cf116e1df850bceacc0107 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sat, 8 Aug 2020 17:44:00 +0200 Subject: [PATCH 10/47] docs: Rename legacy implementation to Windows-like in docs --- pythonping/__init__.py | 2 +- pythonping/executor.py | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index cc2d576..3fc40a0 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -42,7 +42,7 @@ def ping(target, :type verbose: bool :param out: Stream to which redirect the verbose output :type out: stream - :param match: Do payload matching between request and reply (default behaviour follows that of windows which is + :param match: Do payload matching between request and reply (default behaviour follows that of Windows which is by packet identifier only, Linux behaviour counts a non equivalent payload in reply as fail, such as when pinging 8.8.8.8 with 1000 bytes and reply is truncated to only the first 74 of request payload with packet identifiers the same in request and reply) diff --git a/pythonping/executor.py b/pythonping/executor.py index 05ff80b..545672d 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -263,8 +263,6 @@ def send_ping(self, packet_id, sequence_number, payload): def listen_for(self, packet_id, timeout, payload_pattern=None): """Listens for a packet of a given id for a given timeout - (maintain pythonping legacy implementation (windows like) that didn't match payloads, - but allow for it to be set to be linux-like) :param packet_id: The ID of the packet to listen for, the same for request and response :type packet_id: int @@ -282,7 +280,7 @@ def listen_for(self, packet_id, timeout, payload_pattern=None): # If we actually received something if raw_packet != b'': response.unpack(raw_packet) - # to ensure legacy path (which was to not do payload inspection but only match packet identifiers), + # To allow Windows-like behaviour (no payload inspection, but only match packet identifiers), # simply allow for it to be an always true in the legacy usage case if payload_pattern is None: payload_pattern = response.payload @@ -308,7 +306,6 @@ def increase_seq(sequence_number): def run(self, match_payloads=False): """Performs all the pings and stores the responses - (maintain legacy usage path of not matching payload but allow for it to be set) :param match_payloads: optional to set to True to make sure requests and replies have equivalent payloads :type match_payloads: bool""" From 813c57dc8c1bd3bd03dabbc715be6138c31ce469 Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Sat, 8 Aug 2020 12:14:39 -0500 Subject: [PATCH 11/47] add inverse happy path case for payload match on --- test/test_ping.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/test_ping.py b/test/test_ping.py index 74976f7..b8a3fe3 100644 --- a/test/test_ping.py +++ b/test/test_ping.py @@ -11,5 +11,9 @@ def test_ping_execution(self): self.assertEqual(len(ping('10.127.0.1', count=4, size=10)), 4, 'Sent 4 pings to localhost, but not received 4 responses') + self.assertEqual(ping('8.8.8.8', count=4, size=992).success(), True, + 'Sent 4 large pings to google DNS A with payload match off, received all replies') + self.assertEqual(ping('8.8.8.8', count=4, size=992, match=True).success(), False, - 'Sent 4 large pings to google DNS A, expected all to fail since they truncate large payloads') + 'Sent 4 large pings to google DNS A with payload match on,' + + 'expected all to fail since they truncate large payloads') From 945961a1799ea2859c5c823a5feae6b9eceda0e2 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sun, 9 Aug 2020 12:08:00 +0200 Subject: [PATCH 12/47] Create python-publish.yml --- .github/workflows/python-publish.yml | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/python-publish.yml diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..4e1ef42 --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,31 @@ +# This workflows will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + +name: Upload Python Package + +on: + release: + types: [created] + +jobs: + deploy: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine upload dist/* From 116abab31f569a5360c9bd5395a3ab454f1187c5 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sun, 9 Aug 2020 12:12:30 +0200 Subject: [PATCH 13/47] refactor: Update version patch number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 4839355..53633bd 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.9', + version='1.0.10', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From 071748d4ffc265be978501c8ee75466db98dfbf7 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sun, 9 Aug 2020 12:16:51 +0200 Subject: [PATCH 14/47] refactor: Update version patch number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 53633bd..54461ce 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.10', + version='1.0.11', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From 61ce6ed0fb1cbb2c629e1187a341dd27de6a6f29 Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Mon, 10 Aug 2020 15:00:52 -0500 Subject: [PATCH 15/47] update documentation to reflect Union of string and bytes as payload input internally, correct bug in match code made from wrong assumptions based on previously misleading documentation --- pythonping/executor.py | 15 +++++++++------ pythonping/payload_provider.py | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index 545672d..2531404 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -252,14 +252,17 @@ def send_ping(self, packet_id, sequence_number, payload): :param packet_id: The ID to use for the packet :type packet_id: int - :param sequence_number: The seuqnce number to use for the packet + :param sequence_number: The sequence number to use for the packet :type sequence_number: int :param payload: The payload of the ICMP message - :type payload: bytes""" - self.socket.send(icmp.ICMP( + :type payload: Union[str, bytes] + :rtype: bytes""" + i = icmp.ICMP( icmp.Types.EchoRequest, payload=payload, - identifier=packet_id, sequence_number=sequence_number).packet) + identifier=packet_id, sequence_number=sequence_number) + self.socket.send(i.packet) + return i.payload def listen_for(self, packet_id, timeout, payload_pattern=None): """Listens for a packet of a given id for a given timeout @@ -313,10 +316,10 @@ def run(self, match_payloads=False): identifier = self.seed_id seq = 1 for payload in self.provider: - self.send_ping(identifier, seq, payload) + payload_bytes_sent = self.send_ping(identifier, seq, payload) if not match_payloads: self.responses.append(self.listen_for(identifier, self.timeout)) else: - self.responses.append(self.listen_for(identifier, self.timeout, payload)) + self.responses.append(self.listen_for(identifier, self.timeout, payload_bytes_sent)) seq = self.increase_seq(seq) diff --git a/pythonping/payload_provider.py b/pythonping/payload_provider.py index c456295..25fea77 100644 --- a/pythonping/payload_provider.py +++ b/pythonping/payload_provider.py @@ -38,7 +38,7 @@ def __init__(self, pattern, count): """Creates a provider of many identical payloads :param pattern: The existing payload - :type pattern: bytes + :type pattern: Union[str, bytes] :param count: How many payloads to generate :type count: int""" self.pattern = pattern @@ -61,7 +61,7 @@ def __init__(self, pattern, start_size, end_size): """Creates a provider of payloads of increasing size :param pattern: The existing payload, may be cut or replicated to fit the size - :type pattern: bytes + :type pattern: Union[str, bytes] :param start_size: The first payload size to start with, included :type start_size: int :param end_size: The payload size to end with, included From d3efdeec0f69ccf74703a8c5d9705735e5a7d6b1 Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Wed, 12 Aug 2020 12:35:55 -0500 Subject: [PATCH 16/47] fix broken linux legacy match off case in threaded scenario --- pythonping/executor.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index 2531404..d95808e 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -283,15 +283,18 @@ def listen_for(self, packet_id, timeout, payload_pattern=None): # If we actually received something if raw_packet != b'': response.unpack(raw_packet) - # To allow Windows-like behaviour (no payload inspection, but only match packet identifiers), - # simply allow for it to be an always true in the legacy usage case - if payload_pattern is None: - payload_pattern = response.payload # Ensure we have not unpacked the packet we sent (RHEL will also listen to outgoing packets) - if (response.id == packet_id and response.message_type != icmp.Types.EchoRequest.type_id - and response.payload == payload_pattern): - return Response(Message('', response, source_socket[0]), timeout - time_left) + if response.id == packet_id and response.message_type != icmp.Types.EchoRequest.type_id: + if payload_pattern is None: + # To allow Windows-like behaviour (no payload inspection, but only match packet identifiers), + # simply allow for it to be an always true in the legacy usage case + payload_matched = True + else: + payload_matched = (payload_pattern == response.payload) + + if payload_matched: + return Response(Message('', response, source_socket[0]), timeout - time_left) return Response(None, timeout) @staticmethod From 6f6bc1fdbcc0da55acea3c4be3be3b57db5a3c1f Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Wed, 12 Aug 2020 20:43:15 +0200 Subject: [PATCH 17/47] refactor: Update version number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 54461ce..7ed0b9a 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.11', + version='1.0.12', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From aee087851dabfc4c5bf5bba19c6a933b872d015d Mon Sep 17 00:00:00 2001 From: Peter Meissner Date: Mon, 24 Aug 2020 07:27:53 +0200 Subject: [PATCH 18/47] Expand type annotation for ping()'s timout parameter --- pythonping/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 3fc40a0..03e3b62 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -25,7 +25,7 @@ def ping(target, :param target: The remote hostname or IP address to ping :type target: str :param timeout: How long before considering each non-arrived reply permanently lost - :type timeout: int + :type timeout: int, float :param count: How many times to attempt the ping :type count: int :param size: Size of the entire packet to send From 7024ed9027247d26031e48684daf92629ce59596 Mon Sep 17 00:00:00 2001 From: Peter Meissner Date: Mon, 24 Aug 2020 07:29:10 +0200 Subject: [PATCH 19/47] Expand pings()' timeout parameter description --- pythonping/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 03e3b62..5b36775 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -24,7 +24,7 @@ def ping(target, :param target: The remote hostname or IP address to ping :type target: str - :param timeout: How long before considering each non-arrived reply permanently lost + :param timeout: Time in seconds before considering each non-arrived reply permanently lost. :type timeout: int, float :param count: How many times to attempt the ping :type count: int From 956969785e8439f9c79707411488b26351bc41b2 Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Mon, 24 Aug 2020 16:17:52 -0500 Subject: [PATCH 20/47] remove typing annotation for thread safe implementation of SEED_IDs to keep backwards compatibility with python 3.5 --- pythonping/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 3fc40a0..5abd97d 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -2,11 +2,10 @@ from . import network, executor, payload_provider from .utils import random_text from random import randint -from typing import List -# this needs to be available across all thread usages -SEED_IDs: List[int] = [] +# this needs to be available across all thread usages and will hold ints +SEED_IDs = [] def ping(target, From 1088882ec656c1e3c501e06bf83951fb42fc8d0e Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Mon, 24 Aug 2020 16:18:37 -0500 Subject: [PATCH 21/47] add ping match parameter documenation and expand advanced users section to explain thread safe necessity --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b2d5026..a6b7d2b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,11 @@ Note that if you defined `size`, these two fields will be ignored * `verbose` enables the verbose mode, printing output to a stream (see `out`) * `out` is the target stream of verbose mode. If you enable the verbose mode and do not provide `out`, verbose output will be send to the `sys.stdout` stream. You may want to use a file here. +* `match` is a flag that, if set to True, will enable payload matching between a ping request +and reply (default behaviour follows that of Windows which counts a successful reply by a +matched packet identifier only; Linux behaviour counts a non equivalent payload with a matched +packet identifier in reply as fail, such as when pinging 8.8.8.8 with 1000 bytes and the reply +is truncated to only the first 74 of request payload with a matching packet identifier) ## FAQ ### Do I need privileged mode or root? @@ -59,8 +64,10 @@ to create custom IP packets. Unfortunately, there is simply no other way to crea If you wish to extend PythonPing, or integrate it in your application, we recommend to use the classes that are part of Python Ping instead of the `ping` function. `executor.Communicator` handles the communication with the target device, it takes care of sending ICMP requests and -processing responses. It ultimately produces the `executor.ResponseList` object. The `Communicator` -needs to know a target and which payloads to send to the remote device. For that, we have several -classes in the `payload_provider` module. You may want to create your own provider by extending +processing responses (note that for it to be thread safe you must then handle making a unique +seed ID for each thread instance, see ping.__init\__ for an example of this). It ultimately +produces the `executor.ResponseList` object. The `Communicator` needs to know a target and +which payloads to send to the remote device. For that, we have several classes in the +`payload_provider` module. You may want to create your own provider by extending `payload_provider.PayloadProvider`. If you are interested in that, you should check the documentation of both `executor` and `payload_provider` module. From f1d86a4b856f6067273aebcc93e7a4e534f8a440 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Wed, 26 Aug 2020 20:31:10 +0200 Subject: [PATCH 22/47] docs: Correct documentation of timeout --- pythonping/__init__.py | 2 +- pythonping/executor.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 7c39040..11b84a8 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -24,7 +24,7 @@ def ping(target, :param target: The remote hostname or IP address to ping :type target: str :param timeout: Time in seconds before considering each non-arrived reply permanently lost. - :type timeout: int, float + :type timeout: Union[int, float] :param count: How many times to attempt the ping :type count: int :param size: Size of the entire packet to send diff --git a/pythonping/executor.py b/pythonping/executor.py index d95808e..7bca61c 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -227,7 +227,7 @@ def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id :param payload_provider: An iterable list of payloads to send :type payload_provider: PayloadProvider :param timeout: Timeout that will apply to all ping messages, in seconds - :type timeout: int + :type timeout: Union[int, float] :param socket_options: Options to specify for the network.Socket :type socket_options: tuple :param seed_id: The first ICMP packet ID to use From cf20b6656912be94862f718d392855a1c09b52c2 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Wed, 26 Aug 2020 20:31:40 +0200 Subject: [PATCH 23/47] refactor: Increase version patch --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7ed0b9a..8345f51 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.12', + version='1.0.13', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From 3c957e1d05b6598ce679a4b6c94b1e33f0914afb Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Wed, 2 Sep 2020 11:41:41 -0500 Subject: [PATCH 24/47] add a thread safe note comment about seed ID in executor for expert users --- pythonping/executor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pythonping/executor.py b/pythonping/executor.py index 7bca61c..4668352 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -241,6 +241,7 @@ def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id self.timeout = timeout self.responses = ResponseList(verbose=verbose, output=output) self.seed_id = seed_id + # note that to make Communicator instances thread safe, the seed ID must be unique per thread if self.seed_id is None: self.seed_id = os.getpid() & 0xFFFF From e3cf32840e32dddbe9f7e87ae96527f5af43c897 Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Wed, 2 Sep 2020 11:44:46 -0500 Subject: [PATCH 25/47] make network.py thread safe as it was intermittenly failing rarely --- pythonping/network.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pythonping/network.py b/pythonping/network.py index 0c25e2f..b30b627 100644 --- a/pythonping/network.py +++ b/pythonping/network.py @@ -23,7 +23,14 @@ def __init__(self, destination, protocol, source=None, options=(), buffer_size=2 self.destination = socket.gethostbyname(destination) except socket.gaierror as e: raise RuntimeError('Cannot resolve address "' + destination + '", try verify your DNS or host file') - self.protocol = socket.getprotobyname(protocol) + + # Implementing a version of socket.getprotobyname for this library since built-in is not thread safe + # for python 3.5, 3.6, and 3.7: + # https://bugs.python.org/issue30482 + # This bug was causing failures as it would occasionally return a 0 (incorrect) instead of a 1 (correct) + # for the 'icmp' string, causing a OSError for "Protocol not supported" in multi-threaded usage: + # https://github.com/alessandromaggio/pythonping/issues/40 + self.protocol = Socket.getprotobyname(protocol) self.buffer_size = buffer_size if source is not None: raise NotImplementedError('PythonPing currently does not support specification of source IP') @@ -31,6 +38,12 @@ def __init__(self, destination, protocol, source=None, options=(), buffer_size=2 if options: self.socket.setsockopt(*options) + @staticmethod + def getprotobyname(name): + lookup = {"icmp": socket.IPPROTO_ICMP, "tcp": socket.IPPROTO_TCP, "udp": socket.IPPROTO_UDP, + "ip": socket.IPPROTO_IP, "raw": socket.IPPROTO_RAW} + return lookup[name.lower()] + def send(self, packet): """Sends a raw packet on the stream From 85d6fbbf224a2f2c8ed6617eaa6f03fa6771793a Mon Sep 17 00:00:00 2001 From: Micah Albers Date: Sat, 5 Sep 2020 10:27:02 -0500 Subject: [PATCH 26/47] make a single static class level declaration for network proto dictionary to be more efficient and faster for lookup --- pythonping/network.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pythonping/network.py b/pythonping/network.py index b30b627..2845fe4 100644 --- a/pythonping/network.py +++ b/pythonping/network.py @@ -5,6 +5,8 @@ class Socket: DONT_FRAGMENT = (socket.SOL_IP, 10, 1) # Option value for raw socket + PROTO_LOOKUP = {"icmp": socket.IPPROTO_ICMP, "tcp": socket.IPPROTO_TCP, "udp": socket.IPPROTO_UDP, + "ip": socket.IPPROTO_IP, "raw": socket.IPPROTO_RAW} def __init__(self, destination, protocol, source=None, options=(), buffer_size=2048): """Creates a network socket to exchange messages @@ -24,12 +26,6 @@ def __init__(self, destination, protocol, source=None, options=(), buffer_size=2 except socket.gaierror as e: raise RuntimeError('Cannot resolve address "' + destination + '", try verify your DNS or host file') - # Implementing a version of socket.getprotobyname for this library since built-in is not thread safe - # for python 3.5, 3.6, and 3.7: - # https://bugs.python.org/issue30482 - # This bug was causing failures as it would occasionally return a 0 (incorrect) instead of a 1 (correct) - # for the 'icmp' string, causing a OSError for "Protocol not supported" in multi-threaded usage: - # https://github.com/alessandromaggio/pythonping/issues/40 self.protocol = Socket.getprotobyname(protocol) self.buffer_size = buffer_size if source is not None: @@ -38,11 +34,19 @@ def __init__(self, destination, protocol, source=None, options=(), buffer_size=2 if options: self.socket.setsockopt(*options) + # Implementing a version of socket.getprotobyname for this library since built-in is not thread safe + # for python 3.5, 3.6, and 3.7: + # https://bugs.python.org/issue30482 + # This bug was causing failures as it would occasionally return a 0 (incorrect) instead of a 1 (correct) + # for the 'icmp' string, causing a OSError for "Protocol not supported" in multi-threaded usage: + # https://github.com/alessandromaggio/pythonping/issues/40 @staticmethod def getprotobyname(name): - lookup = {"icmp": socket.IPPROTO_ICMP, "tcp": socket.IPPROTO_TCP, "udp": socket.IPPROTO_UDP, - "ip": socket.IPPROTO_IP, "raw": socket.IPPROTO_RAW} - return lookup[name.lower()] + try: + return Socket.PROTO_LOOKUP[name.lower()] + except KeyError: + raise KeyError("'" + str(name) + "' is not in the list of supported proto types: " + + str(list(Socket.PROTO_LOOKUP.keys()))) def send(self, packet): """Sends a raw packet on the stream From 56bf53143a1521485bc7119f1319086c86a08fef Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sat, 12 Sep 2020 15:29:53 +0200 Subject: [PATCH 27/47] refactor: Increase version patch number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8345f51..f626f67 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.13', + version='1.0.14', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From b7fdd373756b7ef720ae9922267dab23d9095275 Mon Sep 17 00:00:00 2001 From: Sam Gomena Date: Thu, 5 Nov 2020 13:50:41 -0800 Subject: [PATCH 28/47] feat: Add naive calculation for determining packet loss --- pythonping/executor.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pythonping/executor.py b/pythonping/executor.py index 4668352..5490de9 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -169,6 +169,10 @@ def success(self, option=SuccessOn.One): result = False not in success_list return result + def packet_loss(self): + success_list = [resp.success for resp in self._responses] + return (success_list.count(False) / len(success_list)) * 100 + @property def rtt_min_ms(self): return represent_seconds_in_ms(self.rtt_min) From dfc6777d3820c2de9550d90fd08dbc92f891268e Mon Sep 17 00:00:00 2001 From: Sam Gomena Date: Fri, 6 Nov 2020 16:38:44 -0800 Subject: [PATCH 29/47] fix: Calculate packet loss as a running average --- pythonping/executor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index 5490de9..d777a62 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -148,6 +148,7 @@ def __init__(self, initial_set=[], verbose=False, output=sys.stdout): self.rtt_avg = 0 self.rtt_min = 0 self.rtt_max = 0 + self.packets_lost = 0 for response in initial_set: self.append(response) @@ -169,9 +170,9 @@ def success(self, option=SuccessOn.One): result = False not in success_list return result + @property def packet_loss(self): - success_list = [resp.success for resp in self._responses] - return (success_list.count(False) / len(success_list)) * 100 + return self.packets_lost @property def rtt_min_ms(self): @@ -201,6 +202,9 @@ def append(self, value): self.rtt_max = value.time_elapsed if value.time_elapsed < self.rtt_min: self.rtt_min = value.time_elapsed + + self.packets_lost = self.packets_lost + (0 if value.success else 1 - self.packets_lost) / len(self) + if self.verbose: print(value, file=self.output) From b46bcefbc4319b84f58ce76404a03b29f0a50b65 Mon Sep 17 00:00:00 2001 From: Sam Gomena Date: Sun, 8 Nov 2020 13:24:44 -0800 Subject: [PATCH 30/47] feat: Add tests for packet loss --- test/test_executor.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test_executor.py b/test/test_executor.py index 1fb4364..46970ad 100644 --- a/test/test_executor.py +++ b/test/test_executor.py @@ -247,6 +247,47 @@ def test_success_half_success(self): 'Unable to calculate success on all with half responses successful' ) + def test_no_packets_lost(self): + rs = executor.ResponseList([ + SuccessfulResponseMock(None, 1), + SuccessfulResponseMock(None, 1), + SuccessfulResponseMock(None, 1), + SuccessfulResponseMock(None, 1) + ]) + + self.assertEqual( + rs.packet_loss, + 0.0, + "Unable to calculate packet loss correctly when all resposes successful" + ) + + def test_all_packets_lost(self): + rs = executor.ResponseList([ + FailingResponseMock(None, 1), + FailingResponseMock(None, 1), + FailingResponseMock(None, 1), + FailingResponseMock(None, 1) + ]) + + self.assertEqual( + rs.packet_loss, + 1.0, + "Unable to calculate packet loss correctly when all resposes failed" + ) + + def test_some_packets_lost(self): + rs = executor.ResponseList([ + SuccessfulResponseMock(None, 1), + SuccessfulResponseMock(None, 1), + FailingResponseMock(None, 1), + FailingResponseMock(None, 1) + ]) + + self.assertEqual( + rs.packet_loss, + 0.5, + "Unable to calculate packet loss correctly when some of the responses failed" + ) class CommunicatorTestCase(unittest.TestCase): """Tests for Communicator""" From 3140e3f88bfc76e44bf14497983e1ffd7d7b762f Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Thu, 12 Nov 2020 19:05:25 +0100 Subject: [PATCH 31/47] feat: Integrate packet lost statistics from PR #58 --- setup.py | 2 +- test/test_executor.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index f626f67..aea55fe 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.14', + version='1.0.15', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', diff --git a/test/test_executor.py b/test/test_executor.py index 46970ad..aa5fbdc 100644 --- a/test/test_executor.py +++ b/test/test_executor.py @@ -258,7 +258,7 @@ def test_no_packets_lost(self): self.assertEqual( rs.packet_loss, 0.0, - "Unable to calculate packet loss correctly when all resposes successful" + "Unable to calculate packet loss correctly when all responses successful" ) def test_all_packets_lost(self): @@ -272,7 +272,7 @@ def test_all_packets_lost(self): self.assertEqual( rs.packet_loss, 1.0, - "Unable to calculate packet loss correctly when all resposes failed" + "Unable to calculate packet loss correctly when all responses failed" ) def test_some_packets_lost(self): @@ -289,6 +289,7 @@ def test_some_packets_lost(self): "Unable to calculate packet loss correctly when some of the responses failed" ) + class CommunicatorTestCase(unittest.TestCase): """Tests for Communicator""" From a82029bc6159ef3512389c0b99a5802ab955207a Mon Sep 17 00:00:00 2001 From: aryzach Date: Thu, 17 Dec 2020 13:52:14 -0800 Subject: [PATCH 32/47] added -I flag functionality (specify source IP) --- pythonping/__init__.py | 6 ++++-- pythonping/executor.py | 4 ++-- pythonping/network.py | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 11b84a8..4fccb91 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -18,7 +18,9 @@ def ping(target, df=False, verbose=False, out=sys.stdout, - match=False): + match=False, + source=None): + """Pings a remote host and handles the responses :param target: The remote hostname or IP address to ping @@ -71,7 +73,7 @@ def ping(target, break comm = executor.Communicator(target, provider, timeout, socket_options=options, verbose=verbose, output=out, - seed_id=seed_id) + seed_id=seed_id, source=source) comm.run(match_payloads=match) SEED_IDs.remove(seed_id) diff --git a/pythonping/executor.py b/pythonping/executor.py index d777a62..7474efe 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -227,7 +227,7 @@ def __iter__(self): class Communicator: """Instance actually communicating over the network, sending messages and handling responses""" def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id=None, - verbose=False, output=sys.stdout): + verbose=False, output=sys.stdout, source=None): """Creates an instance that can handle communication with the target device :param target: IP or hostname of the remote device @@ -244,7 +244,7 @@ def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id :type verbose: bool :param output: File where to write verbose output, defaults to stdout :type output: file""" - self.socket = network.Socket(target, 'icmp', source=None, options=socket_options) + self.socket = network.Socket(target, 'icmp', options=socket_options, source=source) self.provider = payload_provider self.timeout = timeout self.responses = ResponseList(verbose=verbose, output=output) diff --git a/pythonping/network.py b/pythonping/network.py index 2845fe4..080b9b0 100644 --- a/pythonping/network.py +++ b/pythonping/network.py @@ -8,7 +8,7 @@ class Socket: PROTO_LOOKUP = {"icmp": socket.IPPROTO_ICMP, "tcp": socket.IPPROTO_TCP, "udp": socket.IPPROTO_UDP, "ip": socket.IPPROTO_IP, "raw": socket.IPPROTO_RAW} - def __init__(self, destination, protocol, source=None, options=(), buffer_size=2048): + def __init__(self, destination, protocol, options=(), buffer_size=2048, source=None): """Creates a network socket to exchange messages :param destination: Destination IP address @@ -28,9 +28,8 @@ def __init__(self, destination, protocol, source=None, options=(), buffer_size=2 self.protocol = Socket.getprotobyname(protocol) self.buffer_size = buffer_size - if source is not None: - raise NotImplementedError('PythonPing currently does not support specification of source IP') self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, self.protocol) + self.source = source if options: self.socket.setsockopt(*options) @@ -53,6 +52,8 @@ def send(self, packet): :param packet: The raw packet to send :type packet: bytes""" + if self.source: + self.socket.bind((self.source, 0)) self.socket.sendto(packet, (self.destination, 0)) def receive(self, timeout=2): @@ -80,4 +81,4 @@ def __del__(self): self.socket.close() except AttributeError: raise AttributeError("Attribute error because of failed socket init. Make sure you have the root privilege." - " This error may also be caused from DNS resolution problems.") \ No newline at end of file + " This error may also be caused from DNS resolution problems.") From 92e9232cd74829d6384f369e213422db5737c52e Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 12 Mar 2021 07:46:53 +0100 Subject: [PATCH 33/47] feat: Enable ping sweep without forcing size to None --- pythonping/__init__.py | 10 +++++----- setup.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 11b84a8..939f721 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -49,14 +49,14 @@ def ping(target, :return: List with the result of each ping :rtype: executor.ResponseList""" provider = payload_provider.Repeat(b'', 0) - if size and size > 0: - if not payload: - payload = random_text(size) - provider = payload_provider.Repeat(payload, count) - elif sweep_start and sweep_end and sweep_end >= sweep_start: + if sweep_start and sweep_end and sweep_end >= sweep_start: if not payload: payload = random_text(sweep_start) provider = payload_provider.Sweep(payload, sweep_start, sweep_end) + elif size and size > 0: + if not payload: + payload = random_text(size) + provider = payload_provider.Repeat(payload, count) options = () if df: options = network.Socket.DONT_FRAGMENT diff --git a/setup.py b/setup.py index aea55fe..0e65265 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.15', + version='1.0.16', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From 39594cc1ef38c40fa01aa56b15804ddfca3ba55c Mon Sep 17 00:00:00 2001 From: Handy Menny Date: Mon, 19 Apr 2021 00:10:16 +0200 Subject: [PATCH 34/47] Fix Packet loss calculation and add new test When failed requests were mixed with successful ones, the calculation was wrong due to missing parentheses --- pythonping/executor.py | 2 +- test/test_executor.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index d777a62..229bdf9 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -203,7 +203,7 @@ def append(self, value): if value.time_elapsed < self.rtt_min: self.rtt_min = value.time_elapsed - self.packets_lost = self.packets_lost + (0 if value.success else 1 - self.packets_lost) / len(self) + self.packets_lost = self.packets_lost + ((0 if value.success else 1) - self.packets_lost) / len(self) if self.verbose: print(value, file=self.output) diff --git a/test/test_executor.py b/test/test_executor.py index aa5fbdc..ac8eef7 100644 --- a/test/test_executor.py +++ b/test/test_executor.py @@ -289,6 +289,20 @@ def test_some_packets_lost(self): "Unable to calculate packet loss correctly when some of the responses failed" ) + def test_some_packets_lost_mixed(self): + rs = executor.ResponseList([ + FailingResponseMock(None, 1), + SuccessfulResponseMock(None, 1), + FailingResponseMock(None, 1), + SuccessfulResponseMock(None, 1), + ]) + + self.assertEqual( + rs.packet_loss, + 0.5, + "Unable to calculate packet loss correctly when failing responses are mixed with successful responses" + ) + class CommunicatorTestCase(unittest.TestCase): """Tests for Communicator""" From 29bf82660aa05f87e409648c3346ae2d47922d7d Mon Sep 17 00:00:00 2001 From: deltronzero Date: Thu, 3 Jun 2021 08:31:02 -0700 Subject: [PATCH 35/47] add interval option --- README.md | 2 +- pythonping/__init__.py | 5 ++++- pythonping/executor.py | 9 ++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a6b7d2b..8692a31 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ If you want to see the output immediately, emulating what happens on the termina ```python from pythonping import ping - ping('127.0.0.1', verbose=True) ``` This will yeld the following result. @@ -33,6 +32,7 @@ You can also tune your ping by using some of its additional parameters: is unreachable * `payload` allows you to use a specific payload (bytes) * `count` specify allows you to define how many ICMP packets to send +* `interval` the time to wait between pings, in seconds * `sweep_start` and `sweep_end` allows you to perform a ping sweep, starting from payload size defined in `sweep_start` and growing up to size defined in `sweep_end`. Here, we repeat the payload you provided to match the desired size, or we generate a random one if no payload was provided. diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 939f721..53176fd 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -12,6 +12,7 @@ def ping(target, timeout=2, count=4, size=1, + interval=0, payload=None, sweep_start=None, sweep_end=None, @@ -29,6 +30,8 @@ def ping(target, :type count: int :param size: Size of the entire packet to send :type size: int + :param interval: Interval to wait between pings + :type interval: int :param payload: Payload content, leave None if size is set to use random text :type payload: Union[str, bytes] :param sweep_start: If size is not set, initial size in a sweep of sizes @@ -70,7 +73,7 @@ def ping(target, SEED_IDs.append(seed_id) break - comm = executor.Communicator(target, provider, timeout, socket_options=options, verbose=verbose, output=out, + comm = executor.Communicator(target, provider, timeout, interval, socket_options=options, verbose=verbose, output=out, seed_id=seed_id) comm.run(match_payloads=match) diff --git a/pythonping/executor.py b/pythonping/executor.py index d777a62..a0c5891 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -2,6 +2,7 @@ import os import sys +import time from . import icmp from . import network @@ -226,7 +227,7 @@ def __iter__(self): class Communicator: """Instance actually communicating over the network, sending messages and handling responses""" - def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id=None, + def __init__(self, target, payload_provider, timeout, interval, socket_options=(), seed_id=None, verbose=False, output=sys.stdout): """Creates an instance that can handle communication with the target device @@ -236,6 +237,8 @@ def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id :type payload_provider: PayloadProvider :param timeout: Timeout that will apply to all ping messages, in seconds :type timeout: Union[int, float] + :param interval: Interval to wait between pings, in seconds + :type interval: int :param socket_options: Options to specify for the network.Socket :type socket_options: tuple :param seed_id: The first ICMP packet ID to use @@ -247,6 +250,7 @@ def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id self.socket = network.Socket(target, 'icmp', source=None, options=socket_options) self.provider = payload_provider self.timeout = timeout + self.interval = interval self.responses = ResponseList(verbose=verbose, output=output) self.seed_id = seed_id # note that to make Communicator instances thread safe, the seed ID must be unique per thread @@ -335,3 +339,6 @@ def run(self, match_payloads=False): self.responses.append(self.listen_for(identifier, self.timeout, payload_bytes_sent)) seq = self.increase_seq(seq) + + if self.interval: + time.sleep(self.interval) From 9e504b5f61fa4b2e61e2861f02537fd91beade64 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 4 Jun 2021 19:02:27 +0200 Subject: [PATCH 36/47] Release version 1.0.17 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0e65265..546f293 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.16', + version='1.0.17', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From a59ce65a72a1f49f0ec0b1fa9bec23abc6456555 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 4 Jun 2021 19:13:17 +0200 Subject: [PATCH 37/47] Release 1.1.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 546f293..b82e381 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.0.17', + version='1.1.0', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From 145af9222982e497e0fb9f678bcfeb79ef70e6ae Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Wed, 29 Dec 2021 15:35:52 +0100 Subject: [PATCH 38/47] feat: Display payload size in ping --- pythonping/executor.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index ec7ee6e..f1cd140 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -124,7 +124,7 @@ def __repr__(self): return 'Request timed out' elif self.success: return 'Reply from {0}, {1} bytes in {2}ms'.format(self.message.source, - len(self.message.packet.packet), + len(self.message.packet.payload), self.time_elapsed_ms) else: # Not successful, but with some code (e.g. destination unreachable) diff --git a/setup.py b/setup.py index b82e381..7940719 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.1.0', + version='1.1.1', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From 5b6d67228d982caf3503a3f26b6721aa7b3a612e Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 31 Dec 2021 10:08:37 +0100 Subject: [PATCH 39/47] fix: Fix packet length representation --- pythonping/__init__.py | 7 ++++-- pythonping/executor.py | 54 +++++++++++++++++++++++++++++++----------- pythonping/icmp.py | 11 +++++++-- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 53176fd..9a2ac9b 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -19,7 +19,8 @@ def ping(target, df=False, verbose=False, out=sys.stdout, - match=False): + match=False, + out_format='legacy'): """Pings a remote host and handles the responses :param target: The remote hostname or IP address to ping @@ -49,6 +50,8 @@ def ping(target, 8.8.8.8 with 1000 bytes and reply is truncated to only the first 74 of request payload with packet identifiers the same in request and reply) :type match: bool + :param repr_format: How to __repr__ the response. Allowed: legacy, None + :type repr_format: str :return: List with the result of each ping :rtype: executor.ResponseList""" provider = payload_provider.Repeat(b'', 0) @@ -74,7 +77,7 @@ def ping(target, break comm = executor.Communicator(target, provider, timeout, interval, socket_options=options, verbose=verbose, output=out, - seed_id=seed_id) + seed_id=seed_id, repr_format=out_format) comm.run(match_payloads=match) SEED_IDs.remove(seed_id) diff --git a/pythonping/executor.py b/pythonping/executor.py index f1cd140..7e42001 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -52,6 +52,9 @@ def send(self, source_socket): :type source_socket: network.Socket""" source_socket.send(self.packet.packet) + def __repr__(self): + return repr(self.packet) + def represent_seconds_in_ms(seconds): """Converts seconds into human-readable milliseconds with 2 digits decimal precision @@ -65,15 +68,21 @@ def represent_seconds_in_ms(seconds): class Response: """Represents a response to an ICMP message, with metadata like timing""" - def __init__(self, message, time_elapsed): + def __init__(self, message, time_elapsed, source_request=None, repr_format=None): """Creates a representation of ICMP message received in response :param message: The message received :type message: Union[None, Message] :param time_elapsed: Time elapsed since the original request was sent, in seconds - :type time_elapsed: float""" + :type time_elapsed: float + :param source_request: ICMP packet represeting the request that originated this response + :type source_request: ICMP + :param repr_format: How to __repr__ the response. Allowed: legacy, None + :type repr_format: str""" self.message = message self.time_elapsed = time_elapsed + self.source_request = source_request + self.repr_format = repr_format @property def success(self): @@ -119,17 +128,31 @@ def error_message(self): def time_elapsed_ms(self): return represent_seconds_in_ms(self.time_elapsed) - def __repr__(self): + def legacy_repr(self): if self.message is None: return 'Request timed out' elif self.success: return 'Reply from {0}, {1} bytes in {2}ms'.format(self.message.source, - len(self.message.packet.payload), + len(self.message.packet.raw), self.time_elapsed_ms) else: # Not successful, but with some code (e.g. destination unreachable) return '{0} from {1} in {2}ms'.format(self.error_message, self.message.source, self.time_elapsed_ms) + def __repr__(self): + if self.repr_format == 'legacy': + return self.legacy_repr() + if self.message is None: + return 'Timed out' + elif self.success: + return 'status=OK\tfrom={0}\tms={1}\t\tbytes\tsnt={2}\trcv={3}'.format( + self.message.source, + self.time_elapsed_ms, + len(self.source_request.raw)+20, + len(self.message.packet.raw) + ) + else: + return 'status=ERR\tfrom={1}\terror="{0}"'.format(self.message.source, self.error_message) class ResponseList: """Represents a series of ICMP responses""" @@ -228,7 +251,7 @@ def __iter__(self): class Communicator: """Instance actually communicating over the network, sending messages and handling responses""" def __init__(self, target, payload_provider, timeout, interval, socket_options=(), seed_id=None, - verbose=False, output=sys.stdout): + verbose=False, output=sys.stdout, repr_format=None): """Creates an instance that can handle communication with the target device :param target: IP or hostname of the remote device @@ -246,13 +269,16 @@ def __init__(self, target, payload_provider, timeout, interval, socket_options=( :param verbose: Flag to enable verbose mode, defaults to False :type verbose: bool :param output: File where to write verbose output, defaults to stdout - :type output: file""" + :type output: file + :param repr_format: How to __repr__ the response. Allowed: legacy, None + :type repr_format: str""" self.socket = network.Socket(target, 'icmp', source=None, options=socket_options) self.provider = payload_provider self.timeout = timeout self.interval = interval self.responses = ResponseList(verbose=verbose, output=output) self.seed_id = seed_id + self.repr_format = repr_format # note that to make Communicator instances thread safe, the seed ID must be unique per thread if self.seed_id is None: self.seed_id = os.getpid() & 0xFFFF @@ -269,15 +295,15 @@ def send_ping(self, packet_id, sequence_number, payload): :type sequence_number: int :param payload: The payload of the ICMP message :type payload: Union[str, bytes] - :rtype: bytes""" + :rtype: ICMP""" i = icmp.ICMP( icmp.Types.EchoRequest, payload=payload, identifier=packet_id, sequence_number=sequence_number) self.socket.send(i.packet) - return i.payload + return i - def listen_for(self, packet_id, timeout, payload_pattern=None): + def listen_for(self, packet_id, timeout, payload_pattern=None, source_request=None): """Listens for a packet of a given id for a given timeout :param packet_id: The ID of the packet to listen for, the same for request and response @@ -307,8 +333,8 @@ def listen_for(self, packet_id, timeout, payload_pattern=None): payload_matched = (payload_pattern == response.payload) if payload_matched: - return Response(Message('', response, source_socket[0]), timeout - time_left) - return Response(None, timeout) + return Response(Message('', response, source_socket[0]), timeout - time_left, source_request, repr_format=self.repr_format) + return Response(None, timeout, source_request, repr_format=self.repr_format) @staticmethod def increase_seq(sequence_number): @@ -332,11 +358,11 @@ def run(self, match_payloads=False): identifier = self.seed_id seq = 1 for payload in self.provider: - payload_bytes_sent = self.send_ping(identifier, seq, payload) + icmp_out = self.send_ping(identifier, seq, payload) if not match_payloads: - self.responses.append(self.listen_for(identifier, self.timeout)) + self.responses.append(self.listen_for(identifier, self.timeout, None, icmp_out)) else: - self.responses.append(self.listen_for(identifier, self.timeout, payload_bytes_sent)) + self.responses.append(self.listen_for(identifier, self.timeout, icmp_out.payload, icmp_out)) seq = self.increase_seq(seq) diff --git a/pythonping/icmp.py b/pythonping/icmp.py index a5e3aeb..e060735 100644 --- a/pythonping/icmp.py +++ b/pythonping/icmp.py @@ -154,11 +154,14 @@ def __init__(self, message_type=Types.EchoReply, payload=None, identifier=None, self.id = identifier & 0xFFFF # Prevent identifiers bigger than 16 bits self.sequence_number = sequence_number self.received_checksum = None + self.raw = None @property def packet(self): """The raw packet with header, ready to be sent from a socket""" - return self._header(check=self.expected_checksum) + self.payload + p = self._header(check=self.expected_checksum) + self.payload + if (self.raw is None): self.raw = p + return p def _header(self, check=0): """The raw ICMP header @@ -175,6 +178,9 @@ def _header(self, check=0): self.id, self.sequence_number) + def __repr__(self): + return ' '.join('{:02x}'.format(b) for b in self.raw) + @property def is_valid(self): """True if the received checksum is valid, otherwise False""" @@ -209,9 +215,10 @@ def unpack(self, raw): :param raw: The raw packet, including payload :type raw: bytes""" + self.raw = raw self.message_type, \ self.message_code, \ self.received_checksum, \ self.id, \ - sequence = struct.unpack("bbHHh", raw[20:28]) + self.sequence_number = struct.unpack("bbHHh", raw[20:28]) self.payload = raw[28:] From 9e39f46c8df0bbdc5c98b05de8986b5525f16702 Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 1 Feb 2022 20:54:34 -0800 Subject: [PATCH 40/47] Add Github Actions Python Test invalid network dns changed to RFC suggested invalid to avoid resolution of localhost domain Active ping tests avoided on Github Actions since it does not support ICMP --- .github/workflows/python-test.yml | 18 ++++++++++++++++++ test/test_network.py | 2 +- test/test_ping.py | 13 ++++++++----- 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/python-test.yml diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml new file mode 100644 index 0000000..d76f8fe --- /dev/null +++ b/.github/workflows/python-test.yml @@ -0,0 +1,18 @@ +name: Python test + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Test with pytest + run: | + sudo pip install pytest pytest-cov + sudo -E pytest --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html diff --git a/test/test_network.py b/test/test_network.py index 22b3cca..b7cba82 100644 --- a/test/test_network.py +++ b/test/test_network.py @@ -8,4 +8,4 @@ class UtilsTestCase(unittest.TestCase): def test_raise_explicative_error_on_name_resolution_failure(self): """Test a runtime error is generated if the name cannot be resolved""" with self.assertRaises(RuntimeError): - Socket('cant.resolve.this.address.localhost', 'ip') + Socket('invalid', 'raw') diff --git a/test/test_ping.py b/test/test_ping.py index b8a3fe3..f3e2413 100644 --- a/test/test_ping.py +++ b/test/test_ping.py @@ -1,4 +1,5 @@ import unittest +import os from pythonping import ping @@ -11,9 +12,11 @@ def test_ping_execution(self): self.assertEqual(len(ping('10.127.0.1', count=4, size=10)), 4, 'Sent 4 pings to localhost, but not received 4 responses') - self.assertEqual(ping('8.8.8.8', count=4, size=992).success(), True, - 'Sent 4 large pings to google DNS A with payload match off, received all replies') + # Github Actions does not support ICMP + if os.getenv("GITHUB_ACTIONS") is None: + self.assertEqual(ping('8.8.8.8', count=4, size=992).success(), True, + 'Sent 4 large pings to google DNS A with payload match off, received all replies') - self.assertEqual(ping('8.8.8.8', count=4, size=992, match=True).success(), False, - 'Sent 4 large pings to google DNS A with payload match on,' - + 'expected all to fail since they truncate large payloads') + self.assertEqual(ping('8.8.8.8', count=4, size=992, match=True).success(), False, + 'Sent 4 large pings to google DNS A with payload match on,' + + 'expected all to fail since they truncate large payloads') From db176845a2f4d9d17d0aa5aec44e7d45158734f0 Mon Sep 17 00:00:00 2001 From: ilyapashuk Date: Sat, 25 Jun 2022 18:25:07 +0300 Subject: [PATCH 41/47] added a variable to hold the number of lost packets this commits adds a "packets_lost_number" variable to a ResponseList object. this variable holds an integer number of the lost packets. --- pythonping/executor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pythonping/executor.py b/pythonping/executor.py index 7e42001..b57a896 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -173,6 +173,8 @@ def __init__(self, initial_set=[], verbose=False, output=sys.stdout): self.rtt_min = 0 self.rtt_max = 0 self.packets_lost = 0 + # this variable holds int number of lost packets + self.packets_lost_number = 0 for response in initial_set: self.append(response) @@ -228,6 +230,7 @@ def append(self, value): self.rtt_min = value.time_elapsed self.packets_lost = self.packets_lost + ((0 if value.success else 1) - self.packets_lost) / len(self) + if not value.success: self.packets_lost_number += 1 if self.verbose: print(value, file=self.output) From 9156dc3ec86f53e9f12b76b6839a186f3a8d24b9 Mon Sep 17 00:00:00 2001 From: ilyapashuk Date: Fri, 29 Jul 2022 15:39:55 +0300 Subject: [PATCH 42/47] added stats variables for packets --- pythonping/executor.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index b57a896..dc9544a 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -172,9 +172,8 @@ def __init__(self, initial_set=[], verbose=False, output=sys.stdout): self.rtt_avg = 0 self.rtt_min = 0 self.rtt_max = 0 - self.packets_lost = 0 - # this variable holds int number of lost packets - self.packets_lost_number = 0 + self.stats_packets_sent = 0 + self.stats_packets_returned = 0 for response in initial_set: self.append(response) @@ -214,9 +213,13 @@ def rtt_avg_ms(self): def clear(self): self._responses = [] + self.stats_packets_sent = 0 + self.stats_packets_returned = 0 + def append(self, value): self._responses.append(value) + self.stats_packets_sent += 1 if len(self) == 1: self.rtt_avg = value.time_elapsed self.rtt_max = value.time_elapsed @@ -228,13 +231,18 @@ def append(self, value): self.rtt_max = value.time_elapsed if value.time_elapsed < self.rtt_min: self.rtt_min = value.time_elapsed - - self.packets_lost = self.packets_lost + ((0 if value.success else 1) - self.packets_lost) / len(self) - if not value.success: self.packets_lost_number += 1 + if value.success: self.stats_packets_returned += 1 if self.verbose: print(value, file=self.output) - + @property + def stats_packets_lost(self): return self.stats_packets_sent - self.stats_packets_returned + @property + def stats_success_ratio(self): return self.stats_packets_returned / self.stats_packets_sent + @property + def stats_lost_ratio(self): return 1 - self.stats_success_ratio + @property + def packets_lost(self): return self.stats_lost_ratio def __len__(self): return len(self._responses) From 5959052f47b35e9487983c477add70a1b8566c20 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sat, 30 Jul 2022 18:31:17 +0200 Subject: [PATCH 43/47] fix: Restore interval as optional parameter --- pythonping/__init__.py | 2 +- pythonping/executor.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index c01351f..78cfecb 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -78,7 +78,7 @@ def ping(target, break - comm = executor.Communicator(target, provider, timeout, socket_options=options, verbose=verbose, output=out, + comm = executor.Communicator(target, provider, timeout, interval, socket_options=options, verbose=verbose, output=out, seed_id=seed_id, source=source, repr_format=out_format) comm.run(match_payloads=match) diff --git a/pythonping/executor.py b/pythonping/executor.py index b750cf4..50ab95c 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -250,7 +250,7 @@ def __iter__(self): class Communicator: """Instance actually communicating over the network, sending messages and handling responses""" - def __init__(self, target, payload_provider, timeout, socket_options=(), seed_id=None, + def __init__(self, target, payload_provider, timeout, interval, socket_options=(), seed_id=None, verbose=False, output=sys.stdout, source=None, repr_format=None): """Creates an instance that can handle communication with the target device From 03ad4aef82c14589d523bd13108bf9d20a8ef362 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sat, 30 Jul 2022 18:37:00 +0200 Subject: [PATCH 44/47] refactor: increase version number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7940719..977cd45 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.1.1', + version='1.1.2', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From abc4fc567c8c271e85a96a4627ee4878ee71a7de Mon Sep 17 00:00:00 2001 From: ilyapashuk Date: Sun, 31 Jul 2022 13:43:22 +0300 Subject: [PATCH 45/47] added tests for changes in ResponseList object --- test/test_executor.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/test_executor.py b/test/test_executor.py index ac8eef7..cfa16d6 100644 --- a/test/test_executor.py +++ b/test/test_executor.py @@ -255,9 +255,10 @@ def test_no_packets_lost(self): SuccessfulResponseMock(None, 1) ]) + self.assertEqual(rs.stats_packets_sent, rs.stats_packets_returned, 'unable to correctly count sent and returned packets when all responses successful') self.assertEqual( - rs.packet_loss, - 0.0, + rs.stats_packets_lost, + 0, "Unable to calculate packet loss correctly when all responses successful" ) @@ -268,9 +269,9 @@ def test_all_packets_lost(self): FailingResponseMock(None, 1), FailingResponseMock(None, 1) ]) - + self.assertEqual(rs.stats_packets_returned, 0, 'unable to correctly count sent and returned packets when all responses failed') self.assertEqual( - rs.packet_loss, + rs.stats_lost_ratio, 1.0, "Unable to calculate packet loss correctly when all responses failed" ) @@ -282,9 +283,10 @@ def test_some_packets_lost(self): FailingResponseMock(None, 1), FailingResponseMock(None, 1) ]) - + self.assertEqual(rs.stats_packets_sent, 4, 'unable to correctly count sent packets when some of the responses failed') + self.assertEqual(rs.stats_packets_returned, 2, 'unable to correctly count returned packets when some of the responses failed') self.assertEqual( - rs.packet_loss, + rs.stats_lost_ratio, 0.5, "Unable to calculate packet loss correctly when some of the responses failed" ) @@ -296,7 +298,8 @@ def test_some_packets_lost_mixed(self): FailingResponseMock(None, 1), SuccessfulResponseMock(None, 1), ]) - + self.assertEqual(rs.stats_packets_sent, 4, 'unable to correctly count sent packets when when failing responses are mixed with successful responses') + self.assertEqual(rs.stats_packets_returned, 2, 'unable to correctly count returned packets when failing responses are mixed with successful responses') self.assertEqual( rs.packet_loss, 0.5, From 44e76d4b0e5489442d7b03020472d7bbe676ea92 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 19 Aug 2022 15:11:42 +0200 Subject: [PATCH 46/47] feat: Add stats properties --- pythonping/executor.py | 5 +++++ setup.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index 8498bc5..58bdcf3 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -235,14 +235,19 @@ def append(self, value): if self.verbose: print(value, file=self.output) + @property def stats_packets_lost(self): return self.stats_packets_sent - self.stats_packets_returned + @property def stats_success_ratio(self): return self.stats_packets_returned / self.stats_packets_sent + @property def stats_lost_ratio(self): return 1 - self.stats_success_ratio + @property def packets_lost(self): return self.stats_lost_ratio + def __len__(self): return len(self._responses) diff --git a/setup.py b/setup.py index 977cd45..e460001 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.1.2', + version='1.1.3', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', From afa1e9588002bb2cade23451326bdbea06e7496e Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Tue, 25 Oct 2022 11:31:35 +0100 Subject: [PATCH 47/47] feat: Release 1.1.4 (#96) * fix(icmp.py): ICMP header format string All values in the struct should be unsigned. * pylint issues resolved Fixed a bunch of pylint issues * First Readme push headers updated. * Update README.md * Update README.md * UMLs created. * UMLs created. * Folder Structure Created. * backticks added. * Supporting words added. * Grammer fix * Code Styles section finished. * Fix made * Fix made * Readme fixed. * Readme fixed. * Ready for pull request. * Finally ready for pull request. * Testcase added, * feat: integrate #89, #92, and #93 Allow count greater than 16-bit, fix pylint issues, update and expand readme Co-authored-by: David A. Bell <8835326+dabell-cc@users.noreply.github.com> Co-authored-by: Mark Mayo Co-authored-by: Aleksa Zatezalo Co-authored-by: Aleksa Zatezalo <59803757+AleksaZatezalo@users.noreply.github.com> --- .coverage | Bin 0 -> 69632 bytes .vscode/settings.json | 7 +++ CONTRIBUTING.md | 9 ++-- README.md | 88 ++++++++++++++++++++++++++++++++++ docs/UML-Diagram.png | Bin 0 -> 30585 bytes pythonping/__init__.py | 2 +- pythonping/executor.py | 86 +++++++++++++++++---------------- pythonping/icmp.py | 37 +++++++------- requirements.txt | 1 + setup.py | 4 +- test/test_executor.py | 12 ++--- test/test_network.py | 5 +- test/test_payload_provider.py | 7 +++ 13 files changed, 180 insertions(+), 78 deletions(-) create mode 100644 .coverage create mode 100644 .vscode/settings.json create mode 100644 docs/UML-Diagram.png create mode 100644 requirements.txt diff --git a/.coverage b/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..2770929603245d4235c6e9a56eb416a01a15ba14 GIT binary patch literal 69632 zcmeI533MFAnSiU0xo5gZhjm!C)Rtw-hol+ZlI3G0OSa_uwwG%>8nvv!b9rWDTY$|7 zyb!#tON%_VnYH;;(#v#8z2j|6CiTH*6d$3r+j33#9PAh zs-&5}ySo3XzrOmbs=KRudiBaBu8=JC`T`zDNUB9<#4yMvNeCf5e6rvZ4;onD<0~MU zl;z7U>QR+_yh&V#jOu%kc#Ua;SZVx@$zpijxK($j;Ze=CIyY>fffAquC;>`<5+DQy zR~j|>g@w#bJ3@|bw;T#MoN^%gZJNKTqjgP(w5GLfNrx1Tlcwn<2$hvms}%5Ull*c( z>UFtg$>r^FIUOOFcas$AljD2$2jw0-;z1mYd3eVvqK}vCt{%_|$(tYvf57E&1O}wd z@<0XIA(mT6-X4lZKq9hhlNawHO^c~dmjZIH9FVNW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VivSKW7Wj` z|J2#sj*JMWPN7cG9vj<$o2k7RdGwHfaue5|k-hQx|5DvLRG-EfF5Vn%d7o+iUow=V zA6%UB7d1vP-{?&TiS{-jQ%6dj|1Zig*M#SfSL{g_&Hoo>mS^(Rpuq4NYRJ6$nz?Nm zIc{S9KW`3qV@8^z=0V4qu0fAvZtdr(k*VjT&i`j+iZwC+Z&vI&7tQ~Rb(}|GjU?R$ z-Z062!7JkPtaO^!7mon`{l7+T7>O^6cZeIr$-;ZWwiig)_3ajbjNky)A@CE+7WG;TE~Bv?^8deIjMO-eTqM! zdQr7om8)8*>E*5JezlDo*3@WU(B7$qw^PzU2~Yz6G6D(?gpwC`Q|3iBG;+ao!(c)& zWnN^xk~7ML;w9!q*41-9#TiHS;p*mE1*3K%*Z9218U+U@qm@$TMOJ45C}mz`)jWQX zQCvV`D$Erm=S5bINk4h0vZ8_8qB#5HyvXuQ0LSM=mStq?#q%OdGx6TU>;|~Q5+&z$ ziKyFRT4B(+981!z-n?vhkUoeZ3(084yoCw`G? zwvw8=#M6;&)!e4EC5-7sr;+AokW=b2igT6p9~VBOI7dnQPbj54Z`h*1P2vnw9C?-k zHzD`cj=JqxSHro|K_ptqyOGsORbm4qbZJ_pwvD<%pi!zd>i%L@0r%+r#TVR}Zd6dq zQN@(|;7wLeR>TjwJ93{jufS`gxy0{(Hz?(uy!%^~flBOVZqC9vl@%mb7d{MGrNns&+mxrsCMiZc`Z2jOB~6TE7JY;(agn@4LDv>7UsN&n?AOHq E1#GrB5&!@I literal 0 HcmV?d00001 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b2b8866 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "test" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cb2c0fd..dd4b587 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,12 +6,11 @@ here on GitHub. Please note we have a code of conduct, please follow it in all your interactions with the project. ## Pull Request Process +** All PRs must merge into the dev branch, and not into master**. The dev branch is the only one that can merge into master directly. 1. Ensure any install or build dependencies are removed before the end of the layer when doing a build. -2. Update the README.md with details of changes to the interface, this includes new environment +2. Make sure all corresponding test cases pass. +3. Update the README.md with details of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters. -3. Increase the version numbers in any examples files and the README.md to the new version that this - Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/). -4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you - do not have permission to do that, you may request the second reviewer to merge it for you. +4. Version number will be increased only when merging dev into master, so that a version update may carry multiple PRs from various developers. The versioning scheme we use is [SemVer](http://semver.org/). diff --git a/README.md b/README.md index 8692a31..0101415 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,91 @@ which payloads to send to the remote device. For that, we have several classes i `payload_provider` module. You may want to create your own provider by extending `payload_provider.PayloadProvider`. If you are interested in that, you should check the documentation of both `executor` and `payload_provider` module. + +## Code Structure + +### Top Level Directory Layout +Our project directory structure contains all src files in the pythonping folder, test cases in another folder, and helping documentation in on the top level directory. + +``` +. +├── pythonping # Source files +├── test # Automated Testcases for the package +├── CODE_OF_CONDUCT # An md file containing code of conduct +├── CONTRIBUTING # Contributing Guidlins +├── LICENSE # MIT License +├── README.md # An md file +└── setup.py # Instalation +``` + +A UML Diagram of the code structure is below: + +![ER1](https://raw.githubusercontent.com/alessandromaggio/pythonping/master/docs/UML-Diagram.png) + +As per the uml diagram above five distinct classes outside of init exist in this package: Executor, Icmp, Payload Provider, and Utils. Each of them rely on attributes which have been listed as sub-classes for brevities sake. An overview of each class is as follows. + +### Utils +Simply generates random text. See function random_text. + +### Network +Opens a socket to send and recive data. See functions send, recv, and del. + +### Payload Provider +Generates ICMP Payloads with no Headers. It's functionaly a interface. It has three +functions init, iter, and next, which are all implmented by subclasses List, Repeat, and Sweep which store payloads in diffrent lists. + +### ICMP +Generates the ICMP heaser through subclass ICMPType, and various helper functions. + +### Executor +Has various subclasses including Message, Response, Success, and Communicator used for sending icmp packets and collecting data. + +### Init +Uses network, executor, payload_provider and utils.random_text to construct and send ICMP packets to ping a network. + +## Tests +A test package exists under the folder test, and contains a serise of unit tests. Before commiting changes make sure to run the test bench and make sure all corrisponding cases pass. For new functionality new test cases must be added and documented. + +To run testcases we can simply use the ```unitest discover``` utility by running the following command: + +``` +python -m unittest discover +``` + +To run the test cases in a specific file FILE we must run the following command: + +``` +python -m unittest discover -s -p FILE +``` + +Another option is to run the following from the top level directory: + +``` +pytest test +``` + +To test for coverage simply run: + +``` +coverage run -m pytest test +``` + +## Contributing +Before contributing read through the contribution guidlines found the CONTRIBUTING file. + +### Code Style +A few key points when contributing to this repo are as follows: +1. Use tabs over spaces. +2. Format doc strings as such: + ``` + DESCRIPTION + + :param X: DESCRIPTION + :type X: Type + :param Y: DESCRIPTION + :type Y: Type + ``` + Please add doc strings to all functions added. +3. Do not add spaces between docstring and first function line. +4. Do not go over 200 characters per line. +5. When closing multiline items under brackets('()', '[]', ... etc) put the closing bracket on it's own line. \ No newline at end of file diff --git a/docs/UML-Diagram.png b/docs/UML-Diagram.png new file mode 100644 index 0000000000000000000000000000000000000000..6766f5f8f31e0770ce87c65a5fda06535d74b0f2 GIT binary patch literal 30585 zcmZsD2{_el_q8UPXdXFKic*ryN2tg=lm?QiR0v6(jGwT}MC(il(?t9;Ruf5jVx7$&51=gjTmohOiu_`IbX)-a* zr{LeeEM~_4e^y!3#>BLRNlEU|3ESYlTsz$p6S;F5{$IW{jW?HCT)ylZ)EaZT>9dKd zh+DOM(235~2~E#a5+6Nya6Z0dQD@@w=UW!b9+NvHD#-ULD_LmY&H3B*hHshnc(ru7 zko zmzQy7Z4C`OEml~F>8i2EH#}j!=RmCN{UTP^k{0*Il;G%&+oz0r<<#pPam((OHY52olWiO?Tu#D&aF}+ zzS8Vl_tSNJQ_n5=&!@$nX7f-o=8YZP-jmd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7 zV!mN@iJ=cM5$7|46fes7IWu$mj)$i{?Dsm8J9oGmSdAJg@)gbIip@l{I*S$^ zW|74o@tR}!fp5j&jW2QIvM7(d*#+OUKA3YNK4)ddfvhZKC%uWPE-PEOY}v%bBHT@* zdA#s0`+rMeAmNv!y@sj;-K>-A;j5&J`s04H~LGygQxuV$Iv{-$dFU@TyVUV?h(|3Ztmv7-JMgCjABVfwh z!{p=*xTl$=ywggLu4vPEo11mYmD6M19$d_Ke)5DlV%*}6KKy=q`qx`_#(ejb3GR=l zPr)rkpIzXcrrf08t=p!7w&0p9S7_e^^?fpYbH;bdXtBC!4HQA4*-wsvl*FK#k{!z*Lh?K ziHYI-Ifli)R{bkW>S}B4EW3kKs-CW^nsnK9Zt}}HXDr;UZH@+7KbukPyX&&s!Qcnynb8_D&rjL7}c`u+Uj4aHZ_%?rwJ fp6&Ab!{_ghw=oV_w~`}uH6~4%JpSld&maB+8fS@N literal 0 HcmV?d00001 diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 78cfecb..2b3c29e 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -1,7 +1,7 @@ import sys +from random import randint from . import network, executor, payload_provider from .utils import random_text -from random import randint # this needs to be available across all thread usages and will hold ints diff --git a/pythonping/executor.py b/pythonping/executor.py index 58bdcf3..085c6b1 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -92,35 +92,34 @@ def success(self): def error_message(self): if self.message is None: return 'No response' - else: - if self.message.packet.message_type == 0 and self.message.packet.message_code == 0: - # Echo Reply, response OK - no error - return None - elif self.message.packet.message_type == 3: - # Destination unreachable, returning more details based on message code - unreachable_messages = [ - 'Network Unreachable', - 'Host Unreachable', - 'Protocol Unreachable', - 'Port Unreachable', - 'Fragmentation Required', - 'Source Route Failed', - 'Network Unknown', - 'Host Unknown', - 'Source Host Isolated', - 'Communication with Destination Network is Administratively Prohibited', - 'Communication with Destination Host is Administratively Prohibited', - 'Network Unreachable for ToS', - 'Host Unreachable for ToS', - 'Communication Administratively Prohibited', - 'Host Precedence Violation', - 'Precedence Cutoff in Effect' - ] - try: - return unreachable_messages[self.message.packet.message_code] - except IndexError: - # Should never generate IndexError, this serves as additional protection - return 'Unreachable' + if self.message.packet.message_type == 0 and self.message.packet.message_code == 0: + # Echo Reply, response OK - no error + return None + if self.message.packet.message_type == 3: + # Destination unreachable, returning more details based on message code + unreachable_messages = [ + 'Network Unreachable', + 'Host Unreachable', + 'Protocol Unreachable', + 'Port Unreachable', + 'Fragmentation Required', + 'Source Route Failed', + 'Network Unknown', + 'Host Unknown', + 'Source Host Isolated', + 'Communication with Destination Network is Administratively Prohibited', + 'Communication with Destination Host is Administratively Prohibited', + 'Network Unreachable for ToS', + 'Host Unreachable for ToS', + 'Communication Administratively Prohibited', + 'Host Precedence Violation', + 'Precedence Cutoff in Effect' + ] + try: + return unreachable_messages[self.message.packet.message_code] + except IndexError: + # Should never generate IndexError, this serves as additional protection + return 'Unreachable' # Error was not identified return 'Network Error' @@ -131,28 +130,26 @@ def time_elapsed_ms(self): def legacy_repr(self): if self.message is None: return 'Request timed out' - elif self.success: + if self.success: return 'Reply from {0}, {1} bytes in {2}ms'.format(self.message.source, len(self.message.packet.raw), self.time_elapsed_ms) - else: - # Not successful, but with some code (e.g. destination unreachable) - return '{0} from {1} in {2}ms'.format(self.error_message, self.message.source, self.time_elapsed_ms) + # Not successful, but with some code (e.g. destination unreachable) + return '{0} from {1} in {2}ms'.format(self.error_message, self.message.source, self.time_elapsed_ms) def __repr__(self): if self.repr_format == 'legacy': return self.legacy_repr() if self.message is None: return 'Timed out' - elif self.success: + if self.success: return 'status=OK\tfrom={0}\tms={1}\t\tbytes\tsnt={2}\trcv={3}'.format( self.message.source, self.time_elapsed_ms, len(self.source_request.raw)+20, len(self.message.packet.raw) ) - else: - return 'status=ERR\tfrom={1}\terror="{0}"'.format(self.message.source, self.error_message) + return 'status=ERR\tfrom={1}\terror="{0}"'.format(self.message.source, self.error_message) class ResponseList: """Represents a series of ICMP responses""" @@ -231,23 +228,28 @@ def append(self, value): self.rtt_max = value.time_elapsed if value.time_elapsed < self.rtt_min: self.rtt_min = value.time_elapsed - if value.success: self.stats_packets_returned += 1 + if value.success: + self.stats_packets_returned += 1 if self.verbose: print(value, file=self.output) @property - def stats_packets_lost(self): return self.stats_packets_sent - self.stats_packets_returned + def stats_packets_lost(self): + return self.stats_packets_sent - self.stats_packets_returned @property - def stats_success_ratio(self): return self.stats_packets_returned / self.stats_packets_sent + def stats_success_ratio(self): + return self.stats_packets_returned / self.stats_packets_sent @property - def stats_lost_ratio(self): return 1 - self.stats_success_ratio + def stats_lost_ratio(self): + return 1 - self.stats_success_ratio @property - def packets_lost(self): return self.stats_lost_ratio - + def packets_lost(self): + return self.stats_lost_ratio + def __len__(self): return len(self._responses) diff --git a/pythonping/icmp.py b/pythonping/icmp.py index e060735..2814a99 100644 --- a/pythonping/icmp.py +++ b/pythonping/icmp.py @@ -1,9 +1,5 @@ import os -import socket import struct -import select -import time - def checksum(data): """Creates the ICMP checksum as in RFC 1071 @@ -61,64 +57,64 @@ class DestinationUnreachable(ICMPType): class SourceQuench(ICMPType): type_id = 4 SOURCE_QUENCH = (type_id, 0,) - + class Redirect(ICMPType): type_id = 5 FOR_NETWORK = (type_id, 0,) FOR_HOST = (type_id, 1,) FOR_TOS_AND_NETWORK = (type_id, 2,) FOR_TOS_AND_HOST = (type_id, 3,) - + class EchoRequest(ICMPType): type_id = 8 ECHO_REQUEST = (type_id, 0,) - + class RouterAdvertisement(ICMPType): type_id = 9 ROUTER_ADVERTISEMENT = (type_id, 0,) - + class RouterSolicitation(ICMPType): type_id = 10 ROUTER_SOLICITATION = (type_id, 0) # Aliases ROUTER_DISCOVERY = ROUTER_SOLICITATION ROUTER_SELECTION = ROUTER_SOLICITATION - + class TimeExceeded(ICMPType): type_id = 11 TTL_EXPIRED_IN_TRANSIT = (type_id, 0) FRAGMENT_REASSEMBLY_TIME_EXCEEDED = (type_id, 1) - + class BadIPHeader(ICMPType): type_id = 12 POINTER_INDICATES_ERROR = (type_id, 0) MISSING_REQUIRED_OPTION = (type_id, 1) BAD_LENGTH = (type_id, 2) - + class Timestamp(ICMPType): type_id = 13 TIMESTAMP = (type_id, 0) - + class TimestampReply(ICMPType): type_id = 14 TIMESTAMP_REPLY = (type_id, 0) - + class InformationRequest(ICMPType): type_id = 15 INFORMATION_REQUEST = (type_id, 0) - + class InformationReply(ICMPType): type_id = 16 INFORMATION_REPLY = (type_id, 0) - + class AddressMaskRequest(ICMPType): type_id = 17 ADDRESS_MASK_REQUEST = (type_id, 0) - + class AddressMaskReply(ICMPType): type_id = 18 ADDRESS_MASK_REPLY = (type_id, 0) - + class Traceroute(ICMPType): type_id = 30 INFORMATION_REQUEST = (type_id, 30) @@ -160,7 +156,8 @@ def __init__(self, message_type=Types.EchoReply, payload=None, identifier=None, def packet(self): """The raw packet with header, ready to be sent from a socket""" p = self._header(check=self.expected_checksum) + self.payload - if (self.raw is None): self.raw = p + if self.raw is None: + self.raw = p return p def _header(self, check=0): @@ -171,7 +168,7 @@ def _header(self, check=0): :return: The packed header :rtype: bytes""" # TODO implement sequence number - return struct.pack("bbHHh", + return struct.pack("BBHHH", self.message_type, self.message_code, check, @@ -220,5 +217,5 @@ def unpack(self, raw): self.message_code, \ self.received_checksum, \ self.id, \ - self.sequence_number = struct.unpack("bbHHh", raw[20:28]) + self.sequence_number = struct.unpack("BBHHH", raw[20:28]) self.payload = raw[28:] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e7af550 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pexpect==4.0.8 \ No newline at end of file diff --git a/setup.py b/setup.py index e460001..379ca3f 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,10 @@ from setuptools import setup -with open('README.md', 'r') as file: +with open('README.md', 'r', encoding='utf-8') as file: long_description = file.read() setup(name='pythonping', - version='1.1.3', + version='1.1.4', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio', diff --git a/test/test_executor.py b/test/test_executor.py index cfa16d6..0b62a5d 100644 --- a/test/test_executor.py +++ b/test/test_executor.py @@ -257,8 +257,8 @@ def test_no_packets_lost(self): self.assertEqual(rs.stats_packets_sent, rs.stats_packets_returned, 'unable to correctly count sent and returned packets when all responses successful') self.assertEqual( - rs.stats_packets_lost, - 0, + rs.stats_packets_lost, + 0, "Unable to calculate packet loss correctly when all responses successful" ) @@ -271,8 +271,8 @@ def test_all_packets_lost(self): ]) self.assertEqual(rs.stats_packets_returned, 0, 'unable to correctly count sent and returned packets when all responses failed') self.assertEqual( - rs.stats_lost_ratio, - 1.0, + rs.stats_lost_ratio, + 1.0, "Unable to calculate packet loss correctly when all responses failed" ) @@ -286,8 +286,8 @@ def test_some_packets_lost(self): self.assertEqual(rs.stats_packets_sent, 4, 'unable to correctly count sent packets when some of the responses failed') self.assertEqual(rs.stats_packets_returned, 2, 'unable to correctly count returned packets when some of the responses failed') self.assertEqual( - rs.stats_lost_ratio, - 0.5, + rs.stats_lost_ratio, + 0.5, "Unable to calculate packet loss correctly when some of the responses failed" ) diff --git a/test/test_network.py b/test/test_network.py index b7cba82..144a58a 100644 --- a/test/test_network.py +++ b/test/test_network.py @@ -1,11 +1,12 @@ import unittest from pythonping.network import Socket - class UtilsTestCase(unittest.TestCase): """Tests for Socket class""" + + def test_raise_explicative_error_on_name_resolution_failure(self): """Test a runtime error is generated if the name cannot be resolved""" with self.assertRaises(RuntimeError): - Socket('invalid', 'raw') + Socket('invalid', 'raw') \ No newline at end of file diff --git a/test/test_payload_provider.py b/test/test_payload_provider.py index 48a20ef..9cbc474 100644 --- a/test/test_payload_provider.py +++ b/test/test_payload_provider.py @@ -1,10 +1,17 @@ import unittest + +from pexpect import ExceptionPexpect from pythonping import payload_provider class PayloadProviderTestCase(unittest.TestCase): """Tests for PayloadProvider class""" + def test_basic(self): + """Verifies that a provider raises error when calling own functions""" + self.assertRaises(NotImplementedError, payload_provider.PayloadProvider) + + def test_list(self): """Verifies that a list provider generates the correct payloads""" payloads = [