X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FIPv4Util.py;h=ca5a1b571fb6e1328695c12ed60d196b4ad8edd2;hp=5ee73c08fc95f49250080eba5d25fbd577ae46a8;hb=c75b9804c9510d0a342563e41407089145b38d50;hpb=395ed47437010c9852d9d620f491f660a085dbfd diff --git a/resources/libraries/python/IPv4Util.py b/resources/libraries/python/IPv4Util.py index 5ee73c08fc..ca5a1b571f 100644 --- a/resources/libraries/python/IPv4Util.py +++ b/resources/libraries/python/IPv4Util.py @@ -18,6 +18,7 @@ from robot.api.deco import keyword from resources.libraries.python.topology import Topology from resources.libraries.python.IPv4Setup import get_node +from resources.libraries.python.ssh import exec_cmd class IPv4Util(object): @@ -160,3 +161,60 @@ class IPv4Util(object): if net is None: raise ValueError('Link "{0}" not found'.format(link)) return net.get('prefix') + + @staticmethod + def send_ping_from_node_to_dst(node, destination, namespace=None, + ping_count=3, interface=None): + """Send a ping from node to destination. Optionally, you can define a + namespace and interface from where to send a ping. + + :param node: Node to start ping on. + :param destination: IPv4 address where to send ping. + :param namespace: Namespace to send ping from. Optional + :param ping_count: Number of pings to send. Default 3 + :param interface: Interface from where to send ping. Optional + :type node: dict + :type destination: str + :type namespace: str + :type ping_count: int + :type interface: str + :raises RuntimeError: If no response for ping, raise error + """ + cmd = '' + if namespace is not None: + cmd = 'ip netns exec {0} ping -c{1} {2}'.format( + namespace, ping_count, destination) + elif interface is not None: + cmd = 'ping -I {0} -c{1} {2}'.format( + interface, ping_count, destination) + else: + cmd = 'ping -c{0} {1}'.format(ping_count, destination) + rc, stdout, stderr = exec_cmd(node, cmd, sudo=True) + if rc != 0: + raise RuntimeError("Ping Not Successful") + + @staticmethod + def set_linux_interface_arp(node, interface, ip, mac, namespace=None): + """Set arp on interface in linux. + + :param node: Node where to execute command. + :param interface: Interface in namespace. + :param ip: IP for arp. + :param mac: MAC address. + :param namespace: Execute command in namespace. Optional + :type node: dict + :type interface: str + :type ip: str + :type mac: str + :type namespace: str + :raises RuntimeError: Could not set ARP properly. + """ + if namespace is not None: + cmd = 'ip netns exec {} arp -i {} -s {} {}'.format( + namespace, interface, ip, mac) + else: + cmd = 'arp -i {} -s {} {}'.format(interface, ip, mac) + rc, _, stderr = exec_cmd(node, cmd, sudo=True) + if rc != 0: + raise RuntimeError("Arp set not successful, reason:{}". + format(stderr))