Skip to content

Commit

Permalink
Add Cisco Like output
Browse files Browse the repository at this point in the history
Add Cisco like output, related to issue alessandromaggio#9
  • Loading branch information
estaji committed Mar 3, 2023
1 parent 2fc8f9c commit 6ddd6fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pythonping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
25 changes: 21 additions & 4 deletions pythonping/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ddd6fc

Please sign in to comment.