Ejemplo n.º 1
0
    def get_context_data(self, **kwargs):
        context = super(RunPing, self).get_context_data(**kwargs)
        # todo add something to connect to the router and do the ping

        ping = self.object

        command = RunCommand(username=ping.login.user, password=ping.login.password, host=ping.hostip)
        ping_result = command.run("ping %s source %s repeat %s " % (ping.destination, ping.source, ping.count))

        match = re.search(
            r"Success rate is (\d+) percent \(\d+/\d+\), round-trip min/avg/max = (\d+)/(\d+)/(\d+)", ping_result[5]
        )
        if match:
            loss = int(match.group(1))
            min = int(match.group(2))
            avg = int(match.group(3))
            max = int(match.group(4))

            # save the result in the database
            resultDB, created = Result.objects.update_or_create(ping=ping, percent=loss, min=min, avg=avg, max=max)
            context["output"] = ping_result
            context["result"] = resultDB

        else:
            context["ping_error"] = "could not match ping out put"
            context["ping_error_output"] = ping_result

        return context
Ejemplo n.º 2
0
    def get_context_data(self, **kwargs):
        context = super(RunTrace, self).get_context_data(**kwargs)

        command = RunCommand(
            username=self.object.login.user, password=self.object.login.password, host=self.object.hostip
        )
        trace_result = command.run("traceroute %s source %s" % (self.object.destination, self.object.source))
        context["output"] = trace_result

        return context