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); }

In reply to Network topology mapping in single map by mr_linux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.