Skip to content

Commit

Permalink
Merge pull request #1455 from chadell/ping-source-interface
Browse files Browse the repository at this point in the history
Add source_interface argument to ping
  • Loading branch information
mirceaulinic authored May 27, 2021
2 parents 009d545 + 7b56cdd commit 56b341d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions napalm/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ def ping(
size=c.PING_SIZE,
count=c.PING_COUNT,
vrf=c.PING_VRF,
source_interface=c.PING_SOURCE_INTERFACE,
):
"""
Executes ping on the device and returns a dictionary with the result
Expand All @@ -1290,6 +1291,8 @@ def ping(
:type count: optional
:param vrf: Use a specific VRF to execute the ping
:type vrf: optional
:param source_interface: Use an IP from a source interface as source address of echo request
:type source_interface: optional
Output dictionary has one of following keys:
Expand Down
1 change: 1 addition & 0 deletions napalm/base/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
PING_SIZE = 100
PING_COUNT = 5
PING_VRF = ""
PING_SOURCE_INTERFACE = ""

NETMIKO_MAP = {
"ios": "cisco_ios",
Expand Down
3 changes: 3 additions & 0 deletions napalm/eos/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,7 @@ def ping(
size=c.PING_SIZE,
count=c.PING_COUNT,
vrf=c.PING_VRF,
source_interface=c.PING_SOURCE_INTERFACE,
):
"""
Execute ping on the device and returns a dictionary with the result.
Expand Down Expand Up @@ -2036,6 +2037,8 @@ def ping(
command += " repeat {}".format(count)
if source != "":
command += " source {}".format(source)
if source_interface != "":
command += " interface {}".format(source_interface)

commands.append(command)
output = self.device.run_commands(commands, encoding="text")[-1]["output"]
Expand Down
3 changes: 3 additions & 0 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -3178,6 +3178,7 @@ def ping(
size=C.PING_SIZE,
count=C.PING_COUNT,
vrf=C.PING_VRF,
source_interface=C.PING_SOURCE_INTERFACE,
):
"""
Execute ping on the device and returns a dictionary with the result.
Expand Down Expand Up @@ -3208,6 +3209,8 @@ def ping(
command += " repeat {}".format(count)
if source != "":
command += " source {}".format(source)
elif source_interface != "":
command += " source {}".format(source_interface)

output = self._send_command(command)
if "%" in output:
Expand Down
25 changes: 16 additions & 9 deletions napalm/junos/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ def ping(
size=C.PING_SIZE,
count=C.PING_COUNT,
vrf=C.PING_VRF,
source_interface=C.PING_SOURCE_INTERFACE,
):

ping_dict = {}
Expand All @@ -2071,6 +2072,7 @@ def ping(
size_str = ""
count_str = ""
vrf_str = ""
source_interface_str = ""

if source:
source_str = " source {source}".format(source=source)
Expand All @@ -2084,17 +2086,22 @@ def ping(
count_str = " count {count}".format(count=count)
if vrf:
vrf_str = " routing-instance {vrf}".format(vrf=vrf)
if source_interface:
source_interface_str = " interface {source_interface}".format(
source_interface=source_interface
)

ping_command = (
"ping {destination}{source}{ttl}{timeout}{size}{count}{vrf}".format(
destination=destination,
source=source_str,
ttl=maxttl_str,
timeout=timeout_str,
size=size_str,
count=count_str,
vrf=vrf_str,
)
"ping {destination}{source}{ttl}{timeout}{size}{count}{vrf}{source_interface}"
).format(
destination=destination,
source=source_str,
ttl=maxttl_str,
timeout=timeout_str,
size=size_str,
count=count_str,
vrf=vrf_str,
source_interface=source_interface_str,
)

ping_rpc = E("command", ping_command)
Expand Down
3 changes: 3 additions & 0 deletions napalm/nxos/nxos.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def ping(
size=c.PING_SIZE,
count=c.PING_COUNT,
vrf=c.PING_VRF,
source_interface=c.PING_SOURCE_INTERFACE,
):
"""
Execute ping on the device and returns a dictionary with the result.
Expand Down Expand Up @@ -311,6 +312,8 @@ def ping(
command += " count {}".format(count)
if source != "":
command += " source {}".format(source)
elif source_interface != "":
command += " source {}".format(source_interface)

if vrf != "":
command += " vrf {}".format(vrf)
Expand Down

0 comments on commit 56b341d

Please sign in to comment.