class Controlador(): def __init__(self, vista): self.app = vista self.cronometro = Cronometro() #self.app.btnIniciar['command'] = self.iniciar self.app.btnIniciar['command'] = self.interruptor self.app.btnReiniciar['command'] = self.reiniciar self.app.root.mainloop() #Metodo donde se reinicio el cronometro def reiniciar(self): self.cronometro.corriendo = False self.app.actualizarTiempo(self.cronometro.reiniciar()) #Metodo para parar el cronometro def interruptor(self): if self.cronometro.corriendo: self.cronometro.corriendo = False self.app.btnIniciar.config(fg='blue', text='Iniciar') else: self.cronometro.corriendo = True self.app.btnIniciar.config(fg='red', text='Parar') self.iniciar() #Metodo para iniciar el cronometro def iniciar(self): self.app.actualizarTiempo(self.cronometro.iniciar()) self.app.root.after(100, self.iniciar)
class ICMP(): "@param host: ICMP Host" def __init__(self, host): self.__host = host self.__ping = Ping() self.__novoQuadro = None self.__quadroRecebido = False self.__quadroTempoVital = 4 # Time in seconds self.__quadroTTL = 64 # ttl linux defaul self.__cronometro = Cronometro() #self.__timer = [0,0] useless self.__enviados = 0 self.__recebidos = 0 "@param ipDest: Destination Ping IP" def iniciaPing(self,ipDest): self.__ping.setipResposta(ipDest) self.__ping.setipOrigin(self.__host.getIP()) self.__quadroRecebido = False self.__cronometro.zerar() self.__cronometro.iniciar() self.__enviados += 1 # Sending ping frame if (self.__host.enviarQuadro(self.enviaICMP(ipDest)) == False): if (self.__enviados > 0): self.__enviados -= 1 else: self.__enviados = 0 self.__ping.setTempo(self.__cronometro.parar()) self.__ping.setMessage('connect: Network is unreachable') #return False # Timeout without answer elif (self.__quadroRecebido == False): self.__ping.setTempo(self.__cronometro.parar()) # Artificial delay time.sleep(self.__quadroTempoVital) self.__ping.setMessage('Timeout without answer') "@param ipDest: ICMP frame IP origin to be created" def respostaPing(self, ipOrigem, ttl): # Check delivery time delivery_time=self.__cronometro.parar() self.__ping.setTempo(delivery_time) self.__quadroRecebido = True # If the frame was delivered in time and ttl more than 0 if delivery_time < self.__quadroTempoVital and ttl > 0: # Add to ping queue self.__recebidos += 1 self.__ping.setTTL(ttl) self.__ping.setFilaPing() # If the frame was delivered in time and ttl less or igual 0 elif delivery_time < self.__quadroTempoVital and ttl <= 0: # Destination Host Unreachable self.__ping.setMessage('Destination Host Unreachable') # If the frame was delivered with time's up elif delivery_time > self.__quadroTempoVital: # Received with time's up self.__ping.setMessage('Time\'s up!') else: self.__ping.setMessage('Erro check network connection') "@param ipDest: Destination ICMP frame IP to be create" def enviaICMP(self, ipDest): # Make ICMP packet pacote_ICMP = Pacote(self.__host.getIP(),self.__ping.getTamanho()) pacote_ICMP.setIPDestino(ipDest) PacPing = PacotePing("request",0) # PING frame TTL PacPing.setTtl(self.__quadroTTL) pacote_ICMP.setDados(PacPing) # Make ICMP frame Quadro_ICMP = self.__host.constroiQuadro(self.__ping.getTamanho(), "000000000000", pacote_ICMP) Quadro_ICMP.setTipo(2) # Return ICMP frame ready return Quadro_ICMP "@param ipDest: Origin frame IP received from Host" "@param macDest: Origin frame MAC received from Host" def respondeICMP(self, ipDest, macDest, ttl): # Make ICMP answer packet pacote_ICMP = Pacote(self.__host.getIP(),self.__ping.getTamanho()) pacote_ICMP.setIPDestino(ipDest) # Make ping answer packet PacPing = PacotePing("reply",8) PacPing.setTtl(ttl) pacote_ICMP.setDados(PacPing) # Make ICMP frame Quadro_ICMP = self.__host.constroiQuadro(self.__ping.getTamanho(), "000000000000", pacote_ICMP) Quadro_ICMP.setDestino(macDest) Quadro_ICMP.setTipo(2) # Return ICMP frame set return Quadro_ICMP def resetStatistics(self): # Restart queue, list and variables self.__ping.setIcmpSeq() self.__ping.zerarFilaPing() self.__enviados = 0 self.__recebidos = 0 # Rebuild the ping fuction # store all data in pingTabela and use it until new ping request def estatisticas(self): statisticsTable = [] pingTabela = self.__ping.getFilaPing() # Lost, sent and received perdidos = self.__enviados - self.__recebidos try: porcentagem = 100*perdidos/self.__enviados except (ZeroDivisionError): porcentagem = 0 statisticsTable.append("--- "+self.__ping.getipResposta()+" ping statistics ---") statisticsTable.append((str(self.__enviados))+" packets transmitted, "+(str(self.__recebidos))+" received, "+(str(porcentagem))+"% packet loss") maior = pingTabela[0][4] menor = pingTabela[0][4] for ptime in pingTabela: if ptime[4] > maior: maior = ptime[4] if ptime[4] < menor: menor = ptime[4] try: media = (maior + menor)/2 except (ZeroDivisionError): media = 0 #except (TypeError): statisticsTable.append("rtt min/avg/max = "+"%1.*f" % (3, menor)+"/"+"%1.*f" % (3, media)+"/"+"%1.*f" % (3, maior)+" ms") self.resetStatistics() perdidos = 0 media = 0 return statisticsTable def getPingResult (self): pingTabela = self.__ping.getFilaPing()[len(self.__ping.getFilaPing())-1] try: pingTabela[3].isalpha() return (str(pingTabela[0]))+' bytes from '+pingTabela[1]+' ('+pingTabela[1]+'): icmp_seq='+(str(pingTabela[2]))+' '+pingTabela[3] except(AttributeError): return (str(pingTabela[0]))+' bytes from '+pingTabela[1]+' ('+pingTabela[1]+'): icmp_seq='+(str(pingTabela[2]))+' ttl='+(str(pingTabela[3]))+' time='+"%1.*f" % (3, pingTabela[4])+' ms'
class VentanaCronometro(): def __init__(self): self.ventana = tk.Tk() self.ventana.geometry('500x200') self.ventana.resizable(0, 0) self.ventana.title('Cronometro') self.ventana.config(background='#72a922') self.cronometro_label = tk.Label(text="", font=('Tahoma', 44), fg='#ffffff', bg='#1f2f3f', pady=10, padx=10) self.cronometro_label.place(x=20, y=30) self.cronometro = Cronometro() self.activo = True self.frame = Frame(self.ventana) self.cronometro_label.configure(text=self.cronometro.mostrarTiempo()) self.btnIniciar = Button(self.frame, fg='blue', text='Iniciar', command=self.actualizar) self.btnIniciar.grid(row=1, column=1) self.btnParar = Button(self.frame, fg='blue', text='Parar', command=self.parar) self.btnParar.grid(row=1, column=2) btnReiniciar = Button(self.frame, fg='blue', text='Reiniciar', command=self.reiniciar) btnReiniciar.grid(row=1, column=3) self.frame.pack(side=tk.BOTTOM) self.ventana.mainloop() def actualizar(self): global proceso self.cronometro.iniciar() self.cronometro_label.configure(text=self.cronometro.mostrarTiempo()) proceso = self.ventana.after(1, self.actualizar) self.btnIniciar.grid_forget() def parar(self): global proceso self.cronometro.detener() self.ventana.after_cancel(proceso) if self.activo: self.btnParar.configure(text='Reanudar') self.activo = False else: self.btnParar.configure(text='Parar') self.activo = True proceso = self.ventana.after(1, self.actualizar) def reiniciar(self): global proceso self.cronometro.reiniciar() self.ventana.after_cancel(proceso) self.cronometro_label.configure(text=self.cronometro.mostrarTiempo()) self.btnIniciar.grid(row=1, column=1) self.frame.pack(side=tk.BOTTOM)
class ICMP(): "@param host: ICMP Host" def __init__(self, host): self.__host = host self.__ping = Ping() self.__novoQuadro = None self.__quadroRecebido = False self.__quadroTempoVital = 4 # Time in seconds self.__quadroTTL = 64 # ttl linux defaul self.__cronometro = Cronometro() #self.__timer = [0,0] useless self.__enviados = 0 self.__recebidos = 0 "@param ipDest: Destination Ping IP" def iniciaPing(self, ipDest): self.__ping.setipResposta(ipDest) self.__ping.setipOrigin(self.__host.getIP()) self.__quadroRecebido = False self.__cronometro.zerar() self.__cronometro.iniciar() self.__enviados += 1 # Sending ping frame if (self.__host.enviarQuadro(self.enviaICMP(ipDest)) == False): if (self.__enviados > 0): self.__enviados -= 1 else: self.__enviados = 0 self.__ping.setTempo(self.__cronometro.parar()) self.__ping.setMessage('connect: Network is unreachable') #return False # Timeout without answer elif (self.__quadroRecebido == False): self.__ping.setTempo(self.__cronometro.parar()) # Artificial delay time.sleep(self.__quadroTempoVital) self.__ping.setMessage('Timeout without answer') "@param ipDest: ICMP frame IP origin to be created" def respostaPing(self, ipOrigem, ttl): # Check delivery time delivery_time = self.__cronometro.parar() self.__ping.setTempo(delivery_time) self.__quadroRecebido = True # If the frame was delivered in time and ttl more than 0 if delivery_time < self.__quadroTempoVital and ttl > 0: # Add to ping queue self.__recebidos += 1 self.__ping.setTTL(ttl) self.__ping.setFilaPing() # If the frame was delivered in time and ttl less or igual 0 elif delivery_time < self.__quadroTempoVital and ttl <= 0: # Destination Host Unreachable self.__ping.setMessage('Destination Host Unreachable') # If the frame was delivered with time's up elif delivery_time > self.__quadroTempoVital: # Received with time's up self.__ping.setMessage('Time\'s up!') else: self.__ping.setMessage('Erro check network connection') "@param ipDest: Destination ICMP frame IP to be create" def enviaICMP(self, ipDest): # Make ICMP packet pacote_ICMP = Pacote(self.__host.getIP(), self.__ping.getTamanho()) pacote_ICMP.setIPDestino(ipDest) PacPing = PacotePing("request", 0) # PING frame TTL PacPing.setTtl(self.__quadroTTL) pacote_ICMP.setDados(PacPing) # Make ICMP frame Quadro_ICMP = self.__host.constroiQuadro(self.__ping.getTamanho(), "000000000000", pacote_ICMP) Quadro_ICMP.setTipo(2) # Return ICMP frame ready return Quadro_ICMP "@param ipDest: Origin frame IP received from Host" "@param macDest: Origin frame MAC received from Host" def respondeICMP(self, ipDest, macDest, ttl): # Make ICMP answer packet pacote_ICMP = Pacote(self.__host.getIP(), self.__ping.getTamanho()) pacote_ICMP.setIPDestino(ipDest) # Make ping answer packet PacPing = PacotePing("reply", 8) PacPing.setTtl(ttl) pacote_ICMP.setDados(PacPing) # Make ICMP frame Quadro_ICMP = self.__host.constroiQuadro(self.__ping.getTamanho(), "000000000000", pacote_ICMP) Quadro_ICMP.setDestino(macDest) Quadro_ICMP.setTipo(2) # Return ICMP frame set return Quadro_ICMP def resetStatistics(self): # Restart queue, list and variables self.__ping.setIcmpSeq() self.__ping.zerarFilaPing() self.__enviados = 0 self.__recebidos = 0 # Rebuild the ping fuction # store all data in pingTabela and use it until new ping request def estatisticas(self): statisticsTable = [] pingTabela = self.__ping.getFilaPing() # Lost, sent and received perdidos = self.__enviados - self.__recebidos try: porcentagem = 100 * perdidos / self.__enviados except (ZeroDivisionError): porcentagem = 0 statisticsTable.append("--- " + self.__ping.getipResposta() + " ping statistics ---") statisticsTable.append((str(self.__enviados)) + " packets transmitted, " + (str(self.__recebidos)) + " received, " + (str(porcentagem)) + "% packet loss") maior = pingTabela[0][4] menor = pingTabela[0][4] for ptime in pingTabela: if ptime[4] > maior: maior = ptime[4] if ptime[4] < menor: menor = ptime[4] try: media = (maior + menor) / 2 except (ZeroDivisionError): media = 0 #except (TypeError): statisticsTable.append("rtt min/avg/max = " + "%1.*f" % (3, menor) + "/" + "%1.*f" % (3, media) + "/" + "%1.*f" % (3, maior) + " ms") self.resetStatistics() perdidos = 0 media = 0 return statisticsTable def getPingResult(self): pingTabela = self.__ping.getFilaPing()[len(self.__ping.getFilaPing()) - 1] try: pingTabela[3].isalpha() return (str(pingTabela[0])) + ' bytes from ' + pingTabela[ 1] + ' (' + pingTabela[1] + '): icmp_seq=' + (str( pingTabela[2])) + ' ' + pingTabela[3] except (AttributeError): return (str(pingTabela[0])) + ' bytes from ' + pingTabela[ 1] + ' (' + pingTabela[1] + '): icmp_seq=' + (str( pingTabela[2])) + ' ttl=' + (str( pingTabela[3])) + ' time=' + "%1.*f" % ( 3, pingTabela[4]) + ' ms'