ValentinBELYN / icmplib

Easily forge ICMP packets and make your own ping and traceroute.
GNU Lesser General Public License v3.0
276 stars 46 forks source link

UnboundLocalError: local variable 'hop' referenced before assignment #1

Closed rifen closed 4 years ago

rifen commented 4 years ago

Using your library I am having issues with the traceroute portion. Below is my code. I feel like I have practically followed your example besides using input instead of a pre-defined IP.

from icmplib import ping, multiping, traceroute, Host, Hop def trace(addresses): for address in addresses: hops = traceroute( "{address}", count=3, interval=0.5, timeout=2, max_hops=10, fast_mode=False, ) print(address) print("Distance (ttl) Address Average round-trip time") last_distance = 0 for hop in hops: if last_distance + 1 != hop.distance: print("****") print(f"{hop.distance} {hop.address} {hop.avg_rtt} ms") last_distance = hop.distance

I am a beginner so maybe I am doing something wrong but it seems like hop isn't defined in your library.

ValentinBELYN commented 4 years ago

Hi, thank you for using my library 😃

The error you get may be related to an indentation error. Can you repost your indented code into a code block (or take a screenshot of your code)? https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks

Otherwise, this line is incorrect: hops = traceroute("{address}", count=3, interval=0.5, timeout=2, max_hops=10, fast_mode=False)

"{address}" is a string. It is not the address variable. Use instead: hops = traceroute(address, count=3, interval=0.5, timeout=2, max_hops=10, fast_mode=False)

I hope this will help you.