Пример #1
0
    def test_imports(self):
        from pyroute2 import IPRSocket
        from pyroute2.netlink import NLM_F_REQUEST
        from pyroute2.netlink import NLM_F_DUMP
        from pyroute2.netlink import NLM_F_ROOT
        from pyroute2.netlink import NLM_F_MATCH
        from pyroute2.netlink import NLMSG_DONE
        from pyroute2.netlink import NLMSG_ERROR
        from pyroute2.netlink.iproute import RTM_GETLINK
        from pyroute2.netlink.iproute import RTM_NEWLINK
        from pyroute2.netlink.generic import nlmsg
        from pyroute2.netlink.rtnl.ifinfmsg import ifinfmsg

        ip = IPRSocket()
        ip.bind()
        ip.close()

        assert issubclass(IPRSocket, socket.socket)
        assert issubclass(ifinfmsg, nlmsg)
        assert NLM_F_REQUEST == 1
        assert NLM_F_ROOT == 0x100
        assert NLM_F_MATCH == 0x200
        assert NLM_F_DUMP == (NLM_F_ROOT | NLM_F_MATCH)
        assert NLMSG_DONE == 0x3
        assert NLMSG_ERROR == 0x2
        assert RTM_GETLINK == 0x12
        assert RTM_NEWLINK == 0x10
Пример #2
0
    def test_basic(self):
        from pyroute2 import IPRSocket
        from pyroute2.netlink import NLM_F_REQUEST
        from pyroute2.netlink import NLM_F_DUMP
        from pyroute2.netlink import NLM_F_ROOT
        from pyroute2.netlink import NLM_F_MATCH
        from pyroute2.netlink import NLMSG_DONE
        from pyroute2.netlink import NLMSG_ERROR
        from pyroute2.netlink import nlmsg
        from pyroute2.iproute import RTM_GETLINK
        from pyroute2.iproute import RTM_NEWLINK
        from pyroute2.netlink.rtnl.ifinfmsg import ifinfmsg

        ip = IPRSocket()
        ip.bind()

        # check the `socket` interface compliance
        poll = select.poll()
        poll.register(ip, select.POLLIN | select.POLLPRI)
        poll.unregister(ip)
        ip.close()

        assert issubclass(ifinfmsg, nlmsg)
        assert NLM_F_REQUEST == 1
        assert NLM_F_ROOT == 0x100
        assert NLM_F_MATCH == 0x200
        assert NLM_F_DUMP == (NLM_F_ROOT | NLM_F_MATCH)
        assert NLMSG_DONE == 0x3
        assert NLMSG_ERROR == 0x2
        assert RTM_GETLINK == 0x12
        assert RTM_NEWLINK == 0x10
Пример #3
0
    def reconnect_netlink(self):
        if self._nl_socket != None:
            self._nl_socket.close()
            self._nl_socket = None
        self._nl_socket = IPRSocket()
        self._nl_socket.bind(groups=NL_GROUPS)

        self.rescan_devices()
Пример #4
0
 def __init__(self, context):
     threading.Thread.__init__(self)
     self.logger = logging.getLogger(__name__)
     self.context = context
     self.ip = IPRSocket()
     self.ip.bind(rtnl.RTNLGRP_LINK | rtnl.RTNLGRP_NOTIFY)
     self.control = None
     self.state = ''
     self.carrier = -1
Пример #5
0
    def __init__(self, server_handler):
        self._devices = {} #if_index to device
        self._id_mapping = {} #id from the ctl to if_index
        self._tmp_mapping = {} #id from the ctl to newly created device

        self._nl_socket = IPRSocket()
        self._nl_socket.bind()

        self.rescan_devices()

        self._server_handler = server_handler
Пример #6
0
    def __init__(self, server_handler):
        self._device_classes = {}

        self._devices = {} #if_index to device

        self._nl_socket = IPRSocket()
        self._nl_socket.bind(groups=NL_GROUPS)

        self._dl_manager = DevlinkManager()

        self._server_handler = server_handler
Пример #7
0
    def __init__(self, server_handler):
        self._devices = {} #if_index to device
        self._id_mapping = {} #id from the ctl to if_index
        self._tmp_mapping = {} #id from the ctl to newly created device

        self._nl_socket = IPRSocket()
        self._nl_socket.bind(groups=NL_GROUPS)

        self._dl_manager = DevlinkManager()

        self.rescan_devices()

        self._server_handler = server_handler
Пример #8
0
    def __init__(self, server_handler):
        self._device_classes = {}

        self._devices = {}  #ifindex to device

        self._nl_socket = IPRSocket()
        self._nl_socket.bind(groups=NL_GROUPS)

        self._msg_queue = deque()

        #TODO split DevlinkManager away from the InterfaceManager
        #self._dl_manager = DevlinkManager()

        self._server_handler = server_handler
Пример #9
0
    def test_isinstance(self):
        from pyroute2 import IPRSocket
        from pyroute2 import IPRoute
        from pyroute2.iproute import IPRoute as IPRoute_real
        from pyroute2.netlink.rtnl.iprsocket import IPRSocket as IPRSocket_real

        ipr1 = IPRoute()
        ipr2 = IPRoute_real()

        ips1 = IPRSocket()
        ips2 = IPRSocket_real()

        # positive
        assert isinstance(ips1, IPRSocket)
        assert isinstance(ips2, IPRSocket)
        assert isinstance(ips1, IPRSocket_real)
        assert isinstance(ips2, IPRSocket_real)

        assert isinstance(ipr1, IPRoute)
        assert isinstance(ipr2, IPRoute)
        assert isinstance(ipr1, IPRoute_real)
        assert isinstance(ipr2, IPRoute_real)

        # negative
        assert not isinstance(ips1, IPRoute)
        assert not isinstance(ips2, IPRoute)
        assert not isinstance(ips1, IPRoute_real)
        assert not isinstance(ips2, IPRoute_real)

        # this must succeed -- IPRoute is a subclass of IPRSocket
        assert isinstance(ipr1, IPRSocket)
        assert isinstance(ipr2, IPRSocket)
        assert isinstance(ipr1, IPRSocket_real)
        assert isinstance(ipr2, IPRSocket_real)

        ips1.close()
        ips2.close()
        ipr1.close()
        ipr2.close()
    def start_listening(self):

        if self.socket is None:
            self.socket = IPRSocket()

        self.socket.bind()

        self.logger.info("Starting listener...")
        self.listen = True
        while self.listen:
            r, _, _ = select((self.socket, ), (), (), 0.5)
            if r:
                msg_array = self.socket.get()
                for event_msg in msg_array:
                    try:
                        self.queue.put(event_msg)
                    except Queue.Full:
                        self.logger.warn(
                            "Message queue is full! it has %s elements",
                            self.queue.qsize())

        # stop after listening
        self.socket.close()
        self.logger.debug("Socket closed")
Пример #11
0
    "ctn2": "10.200.0.3",
}

ctn_arp = {
    "10.200.0.2": "02:42:0a:00:00:02",
    "10.200.0.3": "02:42:0a:00:00:03"
}

ctn_fib = {
    "02:42:0a:00:00:02": "10.200.129.100",
    "02:42:0a:00:00:03": "10.200.128.218"
}

logging.basicConfig(format='%(levelname)s %(message)s', level=logging.INFO)
ipr = IPRoute()
s = IPRSocket()
s.bind()

while True:
    msg = s.get()
    for m in msg:
        logging.debug('Received an event: {}'.format(m['event']))
        if m['event'] != 'RTM_GETNEIGH':
            continue

        logging.debug("Received a Neighbor miss")

        ifindex = m['ifindex']
        ifname = ipr.get_links(ifindex)[0].get_attr("IFLA_IFNAME")
        logging.debug("Family: {}".format(
            if_family.get(m['family'], m['family'])))
Пример #12
0
'''
Simplest example to monitor Netlink events with a Python script.
'''

from pyroute2 import IPRSocket
from pprint import pprint

ip = IPRSocket()
ip.bind()
pprint(ip.get())
ip.close()
    def __init__(self, api, config, *args, **kw):

        super(NetLinkListener, self).__init__(api, config, *args, **kw)
        self.listen = True
        self.socket = IPRSocket()
Пример #14
0
 def reconnect_netlink(self):
     if self._nl_socket != None:
         self._nl_socket.close()
         self._nl_socket = None
     self._nl_socket = IPRSocket()
     self._nl_socket.bind()