Ejemplo n.º 1
0
async def launch_probes(screen, hostname):
    all_records = []

    #  When one of the probes has a result to display, we'll use
    #  this callback to display it
    def redraw_hops():
        redraw(screen, hostname, all_records)

    async with mtrpacket.MtrPacket() as mtr:
        probe_tasks = []

        try:
            for ttl in range(1, 32):
                #  We need a new ProbeRecord for each ttl value
                record = ProbeRecord(ttl)
                all_records.append(record)

                #  Start a new asyncio task for this probe
                probe_coro = probe_ttl(mtr, hostname, ttl, record, redraw_hops)
                probe_tasks.append(asyncio.ensure_future(probe_coro))

                #  Give each probe a slight delay to avoid flooding
                #  the network interface, which might perturb the
                #  results
                await asyncio.sleep(0.05)

            await asyncio.gather(*probe_tasks)
        finally:
            #  We may have been cancelled, so we should cancel
            #  the probe tasks we started to clean up
            for task in probe_tasks:
                task.cancel()
Ejemplo n.º 2
0
async def trace(host):
    async with mtrpacket.MtrPacket() as mtr:

        #
        #  The time-to-live (TTL) value of the probe determines
        #  the number of network hops the probe will take
        #  before its status is reported
        #
        for ttl in range(1, 256):
            result = await mtr.probe(host, ttl=ttl)

            #  Format the probe results for printing
            line = '{}.'.format(ttl)
            if result.time_ms:
                line += ' {}ms'.format(result.time_ms)
            line += ' {}'.format(result.result)
            if result.responder:
                line += ' from {}'.format(result.responder)

            print(line)

            #  If a probe arrived at its destination IP address,
            #  there is no need for further probing.
            if result.success:
                break
Ejemplo n.º 3
0
async def trace():
    async with mtrpacket.MtrPacket() as mtr:
        for ttl in range(1, 256):
            result = await mtr.probe('localhost', ttl=ttl)
            print(result)

            if result.success:
                break
Ejemplo n.º 4
0
 async def async_launch(self):
     async with mtrpacket.MtrPacket() as mtr:
         coro = self.command_wait(mtr)
         task = asyncio.ensure_future(coro)
         await asyncio.sleep(1)
         task.cancel()
         try:
             await task
         except asyncio.CancelledError:
             pass
Ejemplo n.º 5
0
async def probe(host):
    async with mtrpacket.MtrPacket() as mtr:
        result = await mtr.probe(host)

        #  If the ping got a reply, report the IP address and time
        if result.success:
            print('reply from {} in {} ms'.format(
                result.responder, result.time_ms))
        else:
            print('no reply ({})'.format(result.result))
Ejemplo n.º 6
0
    async def async_commands(self):
        in_queue = asyncio.Queue()
        out_queue = asyncio.Queue()

        server = await asyncio.start_server(
            self.gen_handle_command(in_queue, out_queue), '127.0.0.1', 8901)

        try:
            async with mtrpacket.MtrPacket() as mtr:
                await self.send_probes(mtr, in_queue, out_queue)
        finally:
            server.close()
Ejemplo n.º 7
0
    async def probe(self, host, port=None):
        ''' We send the probe in a coroutine since mtrpacket operates
        asynchronously. In a more complex program, it will allow
        other asynchronous operations to be processed concurrently
        with the probe.
        '''
        async with mtrpacket.MtrPacket() as mtr:
            # port must be provied explicitly and when provided
            # explicitly it cannot be None
            if port:
                result = await mtr.probe(
                    host,
                    port=port,
                    timeout=settings.PING_TIMEOUT,
                )
            else:
                result = await mtr.probe(host, timeout=settings.PING_TIMEOUT)

            #  If the ping got a reply, report the IP address and time
            if result.success:
                self._log('...got a reply from {} in {} ms'.format(
                    result.responder,
                    result.time_ms,
                ))
            else:
                message = f'...no reply from {host}'

                if port:
                    message += f':{port}'

                self._log(message)

            return {
                'host': host,
                'port': port,
                'success': result.success,
                'timems': result.time_ms,
            }
Ejemplo n.º 8
0
 async def async_missing_exec(self):
     async with mtrpacket.MtrPacket() as mtr:
         pass
Ejemplo n.º 9
0
 async def async_proc_exit(self):
     async with mtrpacket.MtrPacket() as mtr:
         await mtr.probe('127.0.0.1')
Ejemplo n.º 10
0
async def probe(target, ttl):
    async with mtrpacket.MtrPacket() as mtr:
        return await mtr.probe(target, local_ip=local_addr, ttl=ttl)