Extract and compile a list of IP addresses after doing a system call of traceroute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hutch75
    New Member
    • Feb 2008
    • 12

    Extract and compile a list of IP addresses after doing a system call of traceroute

    Hi All - chasing down a means to initiate a traceroute, record results, and extract IP addresses in an (array?)

    Here's what I'm thinking about so far, wonder if anyone's been down this road before and can offer advice if I'm going the right direction or not.

    untested - pending permissions to initiate traceroute from server
    Code:
    #!/usr/bin/perl -w
    use strict;
    use warnings;
    use CGI qw(:standard);
    
    # assign a variable name to the CGI form value of the IP address
    my $IP = param('csrip');
    
    # only proceed if the user inputs a value that exceeds 6 characters
    # as they may not know the IP address and we want to ignore if left blank
    
    if ($IP = .{6,}) {
    # do a system call to execute the traceroute command? 
    # assign the stderr and stdout response to a variable
    my $trace = `tracert $IP 2>&1`;
    }
    
    # should I try to extract the IP addresses from the long string of stderr & stdout using a find/replace?
    # or something else?
    
    (my $iponly = $trace) =~ s/xxxxxxxxxx;
    
    # assuming the path of substitution is successful
    # convert the new $iponly variable to an array
    
    my @IPLIST= split(/,/,$iponly);
    
    # then extract the IPs as needed 
    
    print "the path is $IPLIST[1], $IPLIST[2]
    Regards,
    Hutch
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    I have not had the need to do any trace route stuff. Have you looked into using Net::Traceroute might make it easier.

    The next line of code won't return anything useful, since you are declaring it in your if statement block. Therefore, it goes out of scope when you try to access it again. Use strict will catch this for you.

    Code:
    my $trace = `tracert $IP 2>&1`;
    This next line does not do anything either.
    Code:
    if ($IP = .{6,})
    If you want to check for the $ip variable for at least six alpha numeric characters then you will need something like this.
    Code:
    if ($IP =~ / \w{6}/)
    --Kevin

    Comment

    • hutch75
      New Member
      • Feb 2008
      • 12

      #3
      Thanks for the Response Kevin,

      The Unix Sys Admin showed me the error of my ways. I thought I couldn't run traceroute from the command line because of a permissions issue, when in fact it just needed to be called differently.

      EG, instead of typing: traceroute 72.14.207.99

      I needed to type: /usr/sbin/traceroute 72.14.207.99

      With that being said, was able to design a script to do what I wanted:

      Here it is if anyone is interested:

      [CODE=Perl]

      #!/usr/bin/perl -w
      use strict;
      use warnings;

      use CGI qw(:standard);

      my $CIP = param('csrip');

      my $trace;
      my $firstIP;
      my $eachHOP;

      # only proceed if an IP address is entered via form
      if ($CIP =~ /\d.{1,3}\.\d.{1 ,3}\.\d.{1,3}\. \d.{1,3}/) {

      # execute traceroute based on IP, do not perform DNS lookup
      # and send the first line of the response to the bit-bucket
      $trace = open(TRACE, "/usr/sbin/traceroute -n $CIP 2>/dev/null |");
      }

      # if no IP address entered via form then die and do not proceed
      else {

      die;
      }

      # loop through the system output
      while ($firstIP = <TRACE>) {

      # only take the first IP address on a line
      $firstIP =~ /\s+(\d{1,3}\.\d {1,3}\.\d{1,3}\ .\d{1,3})\s+/;

      $eachHOP = $1;

      # prints each IP address on the hop path
      print "$eachHOP\n ";
      }

      close TRACE;

      [/CODE]

      Comment

      Working...