コード例 #1
0
    def __init__(self, ipdb: IPDB, nsp_name: str):
        """
        Creats a namespace for a specific vlan_iface

        :param nsp_name:
        :param ipdb: IPDB is a transactional database, containing records, representing network stack objects.
                    Any change in the database is not reflected immidiately in OS, but waits until commit() is called.
        """
        logging.debug("%sCreate Namespace ...", LoggerSetup.get_log_deep(2))
        self.ipdb = ipdb if ipdb else IPDB()
        self.ipdb_netns = None
        self.nsp_name = nsp_name
        try:
            self.ipdb_netns = IPDB(nl=NetNS(nsp_name))
            self.ipdb_netns.interfaces['lo'].up().commit()
            logging.debug(
                "%s[+] Namespace(" + nsp_name + ") successfully created",
                LoggerSetup.get_log_deep(3))
            # self.encapsulate_interface()
        except Exception as e:
            logging.debug("%s[-] Couldn't create Namespace(" + nsp_name + ")",
                          LoggerSetup.get_log_deep(3))
            for tb in traceback.format_tb(sys.exc_info()[2]):
                logging.error("%s" + tb, LoggerSetup.get_log_deep(3))
            logging.error("%s" + str(e), LoggerSetup.get_log_deep(3))
            self.remove()
コード例 #2
0
    def __init__(self, nsp_name: str, ipdb: IPDB):
        """
        Creats a namespace for a specific vlan_iface

        :param nsp_name:
        :param vlan_iface_name:
        :param ipdb: IPDB is a transactional database, containing records, representing network stack objects.
                    Any change in the database is not reflected immidiately in OS, but waits until commit() is called.
        """
        Logger().debug("Create Namespace ...", 2)
        self.nsp_name = nsp_name
        self.id = id
        self.vlan_iface_name = ""
        self.vlan_iface_ip = "0.0.0.0"
        self.ipdb = ipdb
        self.ipdb_netns = None
        try:
            self.ipdb_netns = IPDB(nl=NetNS(nsp_name))
            netns.setns(nsp_name)
            self.ipdb_netns.interfaces['lo'].up().commit()
            Logger().debug("[+] Namespace(" + nsp_name + ") successfully created", 3)
            # self.encapsulate_interface()
        except Exception as e:
            Logger().debug("[-] Couldn't create Namespace(" + nsp_name + ")", 3)
            for tb in traceback.format_tb(sys.exc_info()[2]):
                Logger().error(tb, 3)
            Logger().error(str(e), 3)
            self.remove()
コード例 #3
0
    def test_create_namespace(self):
        router = self._create_router()

        # Create VLAN
        ipdb = IPDB()
        vlan = Vlan(ipdb, router, "eth0")
        assert isinstance(vlan, Vlan)
        vlan.create_interface()

        # Create Namespace
        namespace = Namespace(ipdb, router.namespace_name)
        assert isinstance(namespace, Namespace)

        # encapsulate VLAN
        namespace.encapsulate_interface(vlan.vlan_iface_name)

        # Test if the namespace now exists
        process = Popen(["ip", "netns"], stdout=PIPE, stderr=PIPE)
        stdout, sterr = process.communicate()
        assert sterr.decode('utf-8') == ""
        assert namespace.nsp_name in stdout.decode('utf-8')

        # Remove the Namespace
        vlan.delete_interface(close_ipdb=True)
        namespace.remove()
        ipdb.release()
        process = Popen(["ip", "netns"], stdout=PIPE, stderr=PIPE)
        stdout, sterr = process.communicate()
        assert stdout.decode('utf-8') == ""
コード例 #4
0
    def __init__(self,
                 ipdb: IPDB,
                 veth_iface_name1: str,
                 veth_iface_name2: str,
                 veth_iface_ip1: str = None,
                 veth_iface_ip_mask1: int = None,
                 veth_iface_ip2: str = None,
                 veth_iface_ip_mask2: int = None):
        """
        Represents a 'veth'-interface.

        :param ipdb: IPDB is a transactional database, containing records, representing network stack objects.
                    Any change in the database is not reflected immidiately in OS, but waits until commit() is called.
        :param veth_iface_name1: like veth00
        :param veth_iface_name2: like veth0
        :param veth_iface_ip1:
        :param veth_iface_ip_mask1:
        :param veth_iface_ip2:
        :param veth_iface_ip_mask2:
        """
        self.ipdb = ipdb if ipdb else IPDB()
        self.veth_iface_name1 = veth_iface_name1
        self.veth_iface_name2 = veth_iface_name2
        self.veth_iface_ip1 = veth_iface_ip1
        self.veth_iface_ip_mask1 = veth_iface_ip_mask1
        self.veth_iface_ip2 = veth_iface_ip2
        self.veth_iface_ip_mask2 = veth_iface_ip_mask2
コード例 #5
0
    def test_timeout(self):
        print("Test Dhclient: timeout")
        router = self._create_router()
        # Set VLAN that doesn't exist, so no response occur.
        router._vlan_iface_id = 99
        router._vlan_iface_name = "vlan99"
        ipdb = IPDB()

        try:
            print("Get link-interface ...")
            # Get the real link interface
            link_iface = ipdb.interfaces["eth0"]
            # Create a Vlan
            print("Create VLAN ...")
            with ipdb.create(kind="vlan",
                             ifname=router.vlan_iface_name,
                             link=link_iface,
                             vlan_id=router.vlan_iface_id).commit() as iface:
                print("Update IP with Dhclient ...")
                Dhclient.update_ip(router.vlan_iface_name, timeout=10)
                iface.mtu = 1400
        except TimeoutError:
            print("[+] Test was successful")
        except Exception as e:
            print("Error: " + str(e))
            raise e
        finally:
            print("Delete VLAN ...")
            ipdb.interfaces[router.vlan_iface_name].remove().commit()
            ipdb.release()
コード例 #6
0
    def test_create_vlan(self):
        print("Test if a VLAN can be created")
        router = self._create_router()

        # Create VLAN
        ipdb = IPDB()
        vlan = Vlan(ipdb, router, "eth0")
        vlan.create_interface()
        assert isinstance(vlan, Vlan)

        # Test if the VLAN now exists
        process = Popen(["ip", "link", "show", "dev", vlan.vlan_iface_name],
                        stdout=PIPE,
                        stderr=PIPE)
        stdout, sterr = process.communicate()
        assert sterr.decode('utf-8') == ""
        assert vlan.vlan_iface_name in stdout.decode('utf-8')

        # Remove the VLAN
        vlan.delete_interface(close_ipdb=True)
        process = Popen(["ip", "link", "show", "dev", vlan.vlan_iface_name],
                        stdout=PIPE,
                        stderr=PIPE)
        stdout, sterr = process.communicate()
        assert stdout.decode('utf-8') == ""
コード例 #7
0
ファイル: nv_assist.py プロジェクト: wj188888/TestFramework
 def __init__(self, link_iface_name: str):
     """
     :param link_iface_name: 'physical'-interface like 'eth0' to which the VLAN is connected to
     """
     self.ipdb = IPDB()
     self.link_iface_name = link_iface_name
     self.vlan_dict = dict()
     self.nsp_dict = dict()
コード例 #8
0
ファイル: vlan.py プロジェクト: wj188888/TestFramework
 def __init__(self, ipdb: IPDB, remote_system: RemoteSystem, link_iface_name):
     """
     :param ipdb: IPDB is a transactional database, containing records, representing network stack objects.
                 Any change in the database is not reflected immidiately in OS, but waits until commit() is called.
     :param link_iface_name: name of the existing interface (eth0, wlan0, ...)
     """
     self.ipdb = ipdb if ipdb else IPDB()
     self.remote_system = remote_system
     self.link_iface_name = link_iface_name
     self.vlan_iface_name = str(remote_system.vlan_iface_name)
     self.vlan_iface_id = int(remote_system.vlan_iface_id)
コード例 #9
0
    def __init__(self, link_iface_name: str):
        """
        Initialize the IPDB for the interfaces and creates a Bridge (virtual switch).

        :param link_iface_name: 'physical'-interface like 'eth0'
        """
        self.ipdb = IPDB()
        self.link_iface_name = link_iface_name
        self.vlan_dict = dict()
        self.veth_dict = dict()
        self.nsp_dict = dict()
コード例 #10
0
    def __init__(self, link_iface_name: str, vlan_iface_name: str, vlan_iface_id: int, vlan_iface_ip: str=None,
                 vlan_iface_ip_mask: int=None):
        """
        Creats a virtual interface on a existing interface (like eth0).
        It uses IPDB: IPDB is a transactional database, containing records, representing network stack objects.
                    Any change in the database is not reflected immidiately in OS, but waits until commit() is called.

        :param link_iface_name: name of the existing interface (eth0, wlan0, ...)
        :param vlan_iface_name: name of the vlan
        :param vlan_iface_id: the id of the vlan
        :param vlan_iface_ip: ip of the virtual interface
        :param vlan_iface_ip_mask: network-mask of the virtual interface
        """
        self.link_iface_name = link_iface_name
        self.vlan_iface_name = vlan_iface_name
        self.vlan_iface_id = vlan_iface_id
        self.ipdb = IPDB()
コード例 #11
0
    def __init__(self,
                 ipdb: IPDB,
                 bridge_name: str = 'br0',
                 link_iface_name: str = 'eth0'):
        """
        Creates a 'bridge'-interface.

        :param ipdb: IPDB is a transactional database, containing records, representing network stack objects.
                    Any change in the database is not reflected immidiately in OS, but waits until commit() is called.
        :param bridge_name: like 'br0'
        :param link_iface_name: like 'eth0'
        """
        self.ipdb = ipdb if ipdb else IPDB()
        self.bridge_name = bridge_name
        self.ipdb.create(kind='bridge', ifname=self.bridge_name).commit()
        # self.ipdb.interfaces[self.bridge_name].up()
        self.add_iface(link_iface_name)
        self.set_new_ip(dhcp=True)
コード例 #12
0
    def test_normal_functionality(self):
        print("Test Dhclient: update_ip")
        router = self._create_router()
        ipdb = IPDB()

        try:
            print("Get link-interface ...")
            # Get the real link interface
            link_iface = ipdb.interfaces["eth0"]
            # Create a Vlan
            print("Create VLAN ...")
            with ipdb.create(kind="vlan",
                             ifname=router.vlan_iface_name,
                             link=link_iface,
                             vlan_id=router.vlan_iface_id).commit() as iface:
                print("Update IP with Dhclient ...")
                Dhclient.update_ip(router.vlan_iface_name)
                iface.mtu = 1400
            # Because the IPDB need some time to update the given IP
            sleep(10)
            process = Popen(
                ["ping", "-c", "1", "-I", router.vlan_iface_name, router.ip],
                stdout=PIPE,
                stderr=PIPE)
            stdout, sterr = process.communicate()
            if not (sterr.decode('utf-8') == ""
                    and "Unreachable" not in stdout.decode('utf-8')):
                raise Exception(str(sterr.decode('utf-8')))
            print("[+] Test was successful")
        except Exception as e:
            print("Error: " + str(e))
            raise e
        finally:
            print("Delete VLAN ...")
            ipdb.interfaces[router.vlan_iface_name].remove().commit()
            ipdb.release()
コード例 #13
0
import logging
log = logging.getLogger(__name__)

__virtual_name__ = 'network_settings'

ATTRS = [
    'family', 'txqlen', 'ipdb_scope', 'index', 'operstate', 'group',
    'carrier_changes', 'ipaddr', 'neighbours', 'ifname', 'promiscuity',
    'linkmode', 'broadcast', 'address', 'num_tx_queues', 'ipdb_priority',
    'change', 'kind', 'qdisc', 'mtu', 'num_rx_queues', 'carrier', 'flags',
    'ifi_type', 'ports'
]

LAST_STATS = {}

IP = IPDB()


class Hashabledict(dict):
    '''
    Helper class that implements a hash function for a dictionary
    '''
    def __hash__(self):
        return hash(tuple(sorted(self.items())))


def __virtual__():
    if HAS_PYROUTE2:
        return __virtual_name__
    return False