func main() { var m = flag.Int("m", traceroute.DEFAULT_MAX_HOPS, `Set the max time-to-live (max number of hops) used in outgoing probe packets (default is 64)`) var q = flag.Int("q", 1, `Set the number of probes per "ttl" to nqueries (default is one probe).`) flag.Parse() host := flag.Arg(0) options := traceroute.TracerouteOptions{} options.SetRetries(*q - 1) options.SetMaxHops(*m + 1) ipAddr, err := net.ResolveIPAddr("ip", host) if err != nil { return } fmt.Printf("traceroute to %v (%v), %v hops max, %v byte packets\n", host, ipAddr, options.MaxHops(), options.PacketSize()) c := make(chan traceroute.TracerouteHop, 0) go func() { for { hop, ok := <-c if !ok { fmt.Println() return } printHop(hop) } }() _, err = traceroute.Traceroute(host, &options, c) if err != nil { fmt.Printf("Error: ", err) } }
func routetrace(hostname string, nodes chan string, end chan bool) { c := make(chan traceroute.TracerouteHop, 0) go func() { for { hop, ok := <-c if !ok { end <- true return } log.Println("hop is there") log.Println(hop.AddressString()) nodes <- hop.AddressString() } }() log.Printf("trying to reach %v servers\n", hostname) out, err := traceroute.Traceroute(hostname, new(traceroute.TracerouteOptions), c) if err == nil { if len(out.Hops) == 0 { log.Println("Expected at least one hop") } } else { log.Printf("Traceroute failed due to an error: %v", err) } }
func main() { var m = flag.Int("m", traceroute.DEFAULT_MAX_HOPS, `Set the max time-to-live (max number of hops) used in outgoing probe packets (default is 64)`) var q = flag.Int("q", 1, `Set the number of probes per "ttl" to nqueries (default is one probe).`) var d = flag.String("d", "GeoLite2-City.mmdb", `IP locate database path (default is 17monipdb.dat)`) flag.Parse() host := flag.Arg(0) db, err := geoip2.Open(*d) if err != nil { fmt.Printf("Unvalied IP locate database (%s) with error: %s\n", d, err.Error()) fmt.Println("Please provider a vailided IP locate database (download from here: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz)") os.Exit(1) } defer db.Close() options := traceroute.TracerouteOptions{} options.SetRetries(*q - 1) options.SetMaxHops(*m + 1) ipAddr, err := net.ResolveIPAddr("ip", host) if err != nil { return } fmt.Printf("traceroute to %v (%v), %v hops max, %v byte packets\n", host, ipAddr, options.MaxHops(), options.PacketSize()) c := make(chan traceroute.TracerouteHop, 0) coords := make([]Coord, 0) lastCity := "" go func() { for { hop, ok := <-c if !ok { fmt.Println() return } coord, err := printHop(db, hop) if err == nil && coord.City != lastCity { lastCity = coord.City coords = append(coords, coord) } } }() _, err = traceroute.Traceroute(host, &options, c) if err != nil { if err.Error() == "operation not permitted" { fmt.Println("Operation not permitted (Maybe with sudo?)") } else { fmt.Printf("Error: ", err) } } else { fmt.Printf("Visualize URL: %s\n", imageURL(coords)) } }