def __init__(self, *argv, **kwarg): self._argv = tuple(argv) self._kwarg = dict(kwarg) if 'router' in kwarg: self._mitogen_broker = None self._mitogen_router = kwarg.pop('router') else: self._mitogen_broker = mitogen.master.Broker() self._mitogen_router = mitogen.master.Router(self._mitogen_broker) netns = kwarg.get('netns', None) if 'context' in kwarg: context = kwarg['context'] else: protocol = kwarg.pop('protocol', 'local') context = getattr(self._mitogen_router, protocol)(*argv, **kwarg) try: ch_in = mitogen.core.Receiver(self._mitogen_router, respondent=context) self._mitogen_call = context.call_async(MitogenServer, ch_out=ch_in.to_sender(), netns=netns) ch_out = ch_in.get().unpickle() super(RemoteIPRoute, self).__init__(Transport(Channel(ch_in)), Transport(Channel(ch_out))) except Exception: if self._mitogen_broker is not None: self._mitogen_broker.shutdown() self._mitogen_broker.join() raise self.marshal = MarshalRtnl()
def MitogenServer(ch_out, netns, router): ch_in = mitogen.core.Receiver(router) ch_out.send(ch_in.to_sender()) trnsp_in = Transport(Channel(ch_in)) trnsp_in.file_obj.start() trnsp_out = Transport(Channel(ch_out)) return Server(trnsp_in, trnsp_out, netns)
def __init__(self, netns, flags=os.O_CREAT, target=None, libc=None): self.netns = netns self.flags = flags target = target or netns trnsp_in, self.remote_trnsp_out = [Transport(FD(x)) for x in os.pipe()] self.remote_trnsp_in, trnsp_out = [Transport(FD(x)) for x in os.pipe()] self.child = os.fork() if self.child == 0: # child process trnsp_in.close() trnsp_out.close() trnsp_in.file_obj.close() trnsp_out.file_obj.close() try: setns(self.netns, self.flags, libc=libc) except OSError as e: (self .remote_trnsp_out .send({'stage': 'init', 'error': e})) os._exit(e.errno) except Exception as e: (self. remote_trnsp_out .send({'stage': 'init', 'error': OSError(errno.ECOMM, str(e), self.netns)})) os._exit(255) try: Server(self.remote_trnsp_in, self.remote_trnsp_out, target=target) finally: os._exit(0) try: self.remote_trnsp_in.close() self.remote_trnsp_out.close() super(NetNS, self).__init__(trnsp_in, trnsp_out) self.target = target except Exception: self.close() raise atexit.register(self.close) self.marshal = MarshalRtnl()
def __init__(self, target): self.target = target cmd = '%s python -m pyroute2.remote' % target self.shell = subprocess.Popen(cmd.split(), bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE) trnsp_in = Transport(self.shell.stdout) trnsp_out = Transport(self.shell.stdin) try: super(ShellIPR, self).__init__(trnsp_in, trnsp_out) except Exception: self.close() raise atexit.register(self.close) self.marshal = MarshalRtnl()
def __init__(self, netns, flags=os.O_CREAT): self.netns = netns self.flags = flags self.trnsp_in, remote_trnsp_out = [Transport(FD(x)) for x in os.pipe()] remote_trnsp_in, self.trnsp_out = [Transport(FD(x)) for x in os.pipe()] self.child = os.fork() if self.child == 0: # child process self.trnsp_in.close() self.trnsp_out.close() try: setns(self.netns, self.flags) except OSError as e: remote_trnsp_out.send({'stage': 'init', 'error': e}) os._exit(e.errno) except Exception as e: remote_trnsp_out.send({ 'stage': 'init', 'error': OSError(errno.ECOMM, str(e), self.netns) }) os._exit(255) try: Server(remote_trnsp_in, remote_trnsp_out) finally: os._exit(0) remote_trnsp_in.close() remote_trnsp_out.close() try: super(NetNS, self).__init__() except Exception: self.close() raise atexit.register(self.close) self.marshal = MarshalRtnl()
import sys from pyroute2.remote import Server from pyroute2.remote import Transport Server(Transport(sys.stdin), Transport(sys.stdout))