mr_linux has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm writing a script that takes the output of traceroute and makes a graphical map (CGI, in a web browser) of a network topology. It will distinguish different subnets by the number of hops it takes to reach a certain IP.

My script currently takes a starting IP and an ending IP (i.e., 192.168.0.1 to 192.168.1.254). It then runs traceroute on the first IP, prints all hops, goes to the next IP, and does the same thing to each subsequent IP until it reaches the last IP.

I want a single map, not a bunch of separate useless ones like I have now. How can I make ONE SINGLE MAP that differentiates subnets and lists all nodes specified between my starting an ending IP's?

I would speculate that I would need to add elements to an array (hash?), remove duplicates, etc. Any help will be EXTREMELY welcome since I am stumped. This is my first real Perl program, so be gentle.

Thanks in advance,
Adam.

#!/usr/bin/perl -w use CGI qw(:standard); print "Content-type: text/html\n\n"; $first = param("first"); $last = param("last"); &form_check; &validated; sub form_check { #Splits IP addresses (octets) on '.' @first = split(/\./,$first); @last = split(/\./,$last); #THIS IS WHERE $first AND $last ARE CHECKED TO SEE IF THEY ARE #IN PROPER FORM...I CUT THIS CODE OUT TO SHORTEN THIS POST } sub validated { #Calls trace for first IP (only used once) $run_once == 0; while($run_once == 0) { &trace; $run_once++; } #While 3rd octet of $first is less than or equal to 3rd octet of $last +, #and 4th octet of $first is less than or equal to 4th octet of $last while($first[2] <= $last[2] && $first[3] < $last[3]){ #While 4th octet of $first is less than 4th octet of $last while($first[3] < $last[3]){ $first[3]++; #Adds 1 to 4th octet, calls sub trace &trace; } #Is the 3rd octet of $first less than the 3rd of $last? #i.e, 192.168.0.1 and 192.168.1.254 while($first[2] < $last[2]) { $first[3] = 1; #Resets 4th octet to 1 $first[2]++; #Adds 1 to 3rd octet of $first } } } sub trace { print "<h1>Tracing route, one moment...</h1>"; #Rejoin octets into $target for tracerouting... $target = join('.', $first[0],$first[1],$first[2],$first[3]); #Run traceroute on $target, only capture IP address to array @ip_addrs my @ip_addrs = map /\(([^)]*)\)/, qx( /usr/sbin/traceroute $target); #Cycle through @ip_addrs, print compy.gif for each hop foreach $element (@ip_addrs) { print "<center><img src=\"compy.gif\"><\/a><br>$element<br><br><\/cent +er>"; $|++; } #Re-splits $target for octet analysis @first = split(/\./,$target); }

Replies are listed 'Best First'.
Re: Network topology mapping in single map
by dondelelcaro (Monk) on Aug 29, 2001 at 04:07 UTC
    First, you probably want to look at GD which will enable you to draw the graph that you are talking about. There are a couple of scripts that do network diagrams, and you probably want to look at them as well. Some reinventing of the wheel is a good thing, but too much is a waste of time.

    Second, if you want to generate a single map with all of the computers, you are going to need to learn a bit about trees and how to describe the connections between routers. Basically, you'll need to do some sort hash that lists each gateway and describes the gateways that it is connected to, and where on your map you want to place the nodes (gateways). This is definetly non-trivial, so I would start working on it one slice at a time.
      Thanks, there is some good information in your posting. I was unaware that GD had any scripts that do network diagrams. I am uncertain as to how I wish to represent the map of computers. Right now my concern is that I do not know how to sort/collect the data properly and insert it into an array/hash that would allow a logical extraction. I came up with a couple crazy ideas tonight, but I have yet to test them. Help. Thanks.
Re: Network topology mapping in single map
by toma (Vicar) on Aug 29, 2001 at 11:16 UTC
    Drawing network graphs is simple, easy and fun with GraphViz.

    Installing GraphViz becomes the challenge.

    It should work perfectly the first time! - toma

Re: Network topology mapping in single map
by Amoe (Friar) on Aug 29, 2001 at 00:40 UTC
    The best advice I can give you, not knowing much about CGI or traceroute, is to not do something as complex as this for your first Perl program. Oh, and use strict and -w ;)
      Thanks for the advice, but I still am going to do it. Or die trying. The only thing left to do is to insert all routes into an array, sort it by removing duplicates, and properly display subnets. I don't think that is a issue of using Graph::, but it's an issue of proper sorting and representation. I think. Who knows, maybe I'm not explaining myself properly. Help.