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

Hello,

I originally posted a question a couple days ago but I didn't say precisely what I wanted. Many offered good assistance on how I can graphically display a network with GD or GraphViz. Although I've learned a lot about these modules, they are not needed at this time. My problem lies before GD/GrapViz can even be used.

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. I would like the final output to look distantly similar to this: http://www.qualys.com/demo/fo_2.9/map-result.html , but not Java or with all that movement/fanciness. See how different subnets/nodes are differentiated? Please read my original post at: http://www.perlmonks.org/index.pl?node_id=108574&lastnode_id=479

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

I need to take the output of traceroute each time it is run (already captured in an array with the code directly below), compare it to a previous (array?) route, remove duplicates, and store in an array. Or maybe I could store everything in a huge multidimensional array? Other techniques? All I want to do is to graphically display a network topology. However that is done is fine by me.
my @ip_addrs = map /\(([^)]*)\)/, qx( /usr/sbin/traceroute $target);
HELP!
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
by Zaxo (Archbishop) on Aug 30, 2001 at 11:47 UTC

    Your question is about the data representation of the network topology. The CPAN Graph::Base module defines methods for representing networks efficiently. Given one of your @ip_addrs arrays,

    use Graph; my $G = Graph->new(); # Create Graph object with listed verteces # Obtain a route in @ip_addrs, then: add_chain $G, @ip_addrs; # lather, rinse, and repeat sub add_chain { my $g = shift; # add_edge produces any vertices not already present $g->add_edge($_[$_-1],$_[$_]) for 1..scalar(@_)-1; }

    There are many advantages to this. Graph takes care of nasties like cyclic graphs, which would give you trouble in a hand-rolled representation. It has methods like neighbors($vertex), successors($vertex), predecessors($vertex), and many more ('man Graph::Base'). Edges can be weighted by, say, bandwidth to provide support for network traffic analysis.

    Hope this helps.

    Update: Graph::Base is under the hood of the Graph module. I cited Graph::Base rather than Graph because cpan search on 'Graph' turns up lots of irrelevancies. Here's a link to the tarball, or (with CPAN.pm):
    $ perl -MCPAN -e 'shell'
    cpan> install Graph
    Re: Masem's response, mr_linux wants to scan a 192.168.x/23 network, about 29 IP's. That net may be sparse enough for a simple homegrown representation, but I like the painlessness and power of the Graph module.

    After Compline,
    Zaxo

      While I agree that a Graph is the best way to go, if you are pretty sure that the routes remain fixed, you could also easily use a tree structure; each node has an array of children, the top node being your target IP that you're tracerouting too. For each traceroute, from the target back, start at the top of the tree, and if the next IP exists as a child, move to that node and do the next IP. If the IP doesn't exist as a child, create a new one, attach to your current node, then move down into that and repeat from there (of course, you'll be making new nodes all down to your final IP from that).

      Again, there might be problems with cycles resulting from this, depending on when routes change, etc. and the speed at which you collect the tracerouts.

      -----------------------------------------------------
      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      It's not what you know, but knowing how to find it if you don't know that's important

      Sounds like some good advice. There is a problem. I'm embarassed by this, but I can't find find out where to download Graph::Base at CPAN. Where can I find it? Thanks a lot.
      -AC
      Thanks, I appreciate it. I'll start downloading it in the next few minutes. Thanks a lot.