Skip to content

Commit

Permalink
Add ttl option to PingTransmitter and CLI: # 34
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Feb 23, 2020
1 parent 13fb5c2 commit cd59ef0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pingparsing/_cmd_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(
deadline: Optional[hr.Time] = None,
timeout: Optional[hr.Time] = None,
packet_size: Optional[int] = None,
ttl: Optional[int] = None,
interface: Optional[str] = None,
is_ipv6: bool = False,
timestamp: bool = False,
Expand All @@ -28,6 +29,7 @@ def __init__(
self.deadline = deadline
self.timeout = timeout
self._packet_size = packet_size
self._ttl = ttl
self.interface = interface
self._is_ipv6 = is_ipv6
self._timestamp = timestamp
Expand All @@ -48,6 +50,9 @@ def make_cmd(self, destination: str) -> str:
if self._packet_size:
command_items.extend(self._get_packet_size_option())

if self._ttl:
command_items.extend(self._get_ttl_option())

if self._timestamp:
command_items.append(self._get_timestamp_option())

Expand Down Expand Up @@ -93,6 +98,10 @@ def _get_count_option(self) -> str:
def _get_packet_size_option(self) -> List:
raise NotImplementedError()

@abc.abstractmethod
def _get_ttl_option(self) -> List:
raise NotImplementedError()


class PosixPingCmdMaker(PingCmdMaker):
def _get_destination_host(self, destination: str) -> str:
Expand Down Expand Up @@ -123,6 +132,9 @@ def _get_packet_size_option(self) -> List:


class MacosPingCmdMaker(PosixPingCmdMaker):
def _get_ttl_option(self) -> List:
return ["-T", str(self._ttl)]

def _get_deadline_option(self) -> str:
if self.deadline is None:
if self.count:
Expand All @@ -144,6 +156,9 @@ def _get_timeout_option(self) -> str:


class LinuxPingCmdMaker(PosixPingCmdMaker):
def _get_ttl_option(self) -> List:
return ["-t", str(self._ttl)]

def _get_deadline_option(self) -> str:
if self.deadline is None:
if self.count:
Expand Down Expand Up @@ -212,3 +227,6 @@ def _get_count_option(self) -> str:

def _get_packet_size_option(self) -> List:
return ["-l", str(self._packet_size)]

def _get_ttl_option(self) -> List:
return ["-i", str(self._ttl)]
6 changes: 6 additions & 0 deletions pingparsing/_pingtransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class PingTransmitter:
Specifies the number of data bytes to be sent.
.. py:attribute:: ttl
Specifies the Time to Live.
.. py:attribute:: ping_option
Additional ``ping`` command option.
Expand Down Expand Up @@ -202,6 +206,7 @@ def __init__(self) -> None:
self.__destination = ""
self.count = None # type: Optional[int]
self.packet_size = None # type: Optional[int]
self.ttl = None # type: Optional[int]
self.ping_option = ""
self.is_quiet = False
self.interface = None # type: Optional[str]
Expand Down Expand Up @@ -293,6 +298,7 @@ def __make_ping_command(self) -> str:
return maker_class(
count=self.count,
packet_size=self.packet_size,
ttl=self.ttl,
deadline=self.deadline,
timeout=self.timeout,
interface=self.interface,
Expand Down
6 changes: 6 additions & 0 deletions pingparsing/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def parse_option() -> argparse.Namespace:
group.add_argument(
"-s", "--packet-size", type=int, help="Specifies the number of data bytes to be sent.",
)
group.add_argument(
"--ttl", type=int, help="Specifies the Time to Live.",
)
group.add_argument(
"-w",
"--deadline",
Expand Down Expand Up @@ -220,6 +223,7 @@ def parse_ping(
interface: Optional[str],
count: int,
packet_size: Optional[int],
ttl: Optional[int],
deadline: TimeArg,
timeout: TimeArg,
is_parse_icmp_reply: bool,
Expand All @@ -234,6 +238,7 @@ def parse_ping(
transmitter.interface = interface
transmitter.count = count
transmitter.packet_size = packet_size
transmitter.ttl = ttl
transmitter.deadline = deadline
transmitter.timeout = timeout
transmitter.is_quiet = not is_parse_icmp_reply
Expand Down Expand Up @@ -355,6 +360,7 @@ def main() -> int:
options.interface,
count,
options.packet_size,
options.ttl,
deadline,
timeout,
options.icmp_reply,
Expand Down
11 changes: 11 additions & 0 deletions test/test_cmd_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ def test_normal_count(self, maker_class, host, count, expected):
def test_normal_packet_size(self, maker_class, host, packet_size, expected):
assert maker_class(packet_size=packet_size, count=1).make_cmd(destination=host) == expected

@pytest.mark.parametrize(
["maker_class", "host", "ttl", "expected"],
[
[LinuxPingCmdMaker, "localhost", 32, "ping -c 1 -t 32 localhost"],
[MacosPingCmdMaker, "localhost", 32, "ping -c 1 -T 32 localhost"],
[WindowsPingCmdMaker, "localhost", 32, "ping -n 1 -i 32 localhost"],
],
)
def test_normal_ttl(self, maker_class, host, ttl, expected):
assert maker_class(ttl=ttl, count=1).make_cmd(destination=host) == expected

@pytest.mark.parametrize(
["maker_class", "host", "expected"],
[
Expand Down

0 comments on commit cd59ef0

Please sign in to comment.