diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 2b3c29e..4a35681 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -51,7 +51,7 @@ 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 + :param repr_format: How to __repr__ the response. Allowed: legacy, cisco, None :type repr_format: str :return: List with the result of each ping :rtype: executor.ResponseList""" diff --git a/pythonping/executor.py b/pythonping/executor.py index 085c6b1..fd1a9fe 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -77,7 +77,7 @@ def __init__(self, message, time_elapsed, source_request=None, repr_format=None) :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 + :param repr_format: How to __repr__ the response. Allowed: legacy, cisco, None :type repr_format: str""" self.message = message self.time_elapsed = time_elapsed @@ -137,9 +137,19 @@ def legacy_repr(self): # 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 cisco_repr(self): + if self.message is None: + return '.' + elif self.success: + return '!' + else: + return 'U' + def __repr__(self): if self.repr_format == 'legacy': return self.legacy_repr() + if self.repr_format == 'cisco': + return self.cisco_repr() if self.message is None: return 'Timed out' if self.success: @@ -215,7 +225,11 @@ def clear(self): def append(self, value): - self._responses.append(value) + if value.repr_format == "cisco" and self._responses: + last_value = self._responses[0] + self._responses[0]=f'{last_value}{value}' + else: + self._responses.append(value) self.stats_packets_sent += 1 if len(self) == 1: self.rtt_avg = value.time_elapsed @@ -232,7 +246,10 @@ def append(self, value): self.stats_packets_returned += 1 if self.verbose: - print(value, file=self.output) + if value.repr_format == "cisco": + print(value, file=self.output, end="") + else: + print(value, file=self.output) @property def stats_packets_lost(self): @@ -288,7 +305,7 @@ def __init__(self, target, payload_provider, timeout, interval, socket_options=( :type verbose: bool :param output: File where to write verbose output, defaults to stdout :type output: file - :param repr_format: How to __repr__ the response. Allowed: legacy, None + :param repr_format: How to __repr__ the response. Allowed: legacy, cisco, None :type repr_format: str""" self.socket = network.Socket(target, 'icmp', options=socket_options, source=source) self.provider = payload_provider