By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

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

P: 12
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl -w
  3. use strict;
  4. use warnings;
  5. use CGI qw(:standard);
  6.  
  7. # assign a variable name to the CGI form value of the IP address
  8. my $IP = param('csrip');
  9.  
  10. # only proceed if the user inputs a value that exceeds 6 characters
  11. # as they may not know the IP address and we want to ignore if left blank
  12.  
  13. if ($IP = .{6,}) {
  14. # do a system call to execute the traceroute command? 
  15. # assign the stderr and stdout response to a variable
  16. my $trace = `tracert $IP 2>&1`;
  17. }
  18.  
  19. # should I try to extract the IP addresses from the long string of stderr & stdout using a find/replace?
  20. # or something else?
  21.  
  22. (my $iponly = $trace) =~ s/xxxxxxxxxx;
  23.  
  24. # assuming the path of substitution is successful
  25. # convert the new $iponly variable to an array
  26.  
  27. my @IPLIST= split(/,/,$iponly);
  28.  
  29. # then extract the IPs as needed 
  30.  
  31. print "the path is $IPLIST[1], $IPLIST[2]
  32.  
  33.  
Regards,
Hutch
Apr 18 '08 #1
Share this Question
Share on Google+
2 Replies


eWish
Expert 100+
P: 971
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.

Expand|Select|Wrap|Line Numbers
  1. my $trace = `tracert $IP 2>&1`;
This next line does not do anything either.
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. if ($IP =~ / \w{6}/)
--Kevin
Apr 18 '08 #2

P: 12
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl -w
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI qw(:standard);
  7.  
  8. my $CIP = param('csrip');
  9.  
  10. my $trace;
  11. my $firstIP;
  12. my $eachHOP;
  13.  
  14. # only proceed if an IP address is entered via form
  15. if ($CIP =~ /\d.{1,3}\.\d.{1,3}\.\d.{1,3}\.\d.{1,3}/) {
  16.  
  17. # execute traceroute based on IP, do not perform DNS lookup
  18. # and send the first line of the response to the bit-bucket
  19. $trace = open(TRACE, "/usr/sbin/traceroute -n $CIP 2>/dev/null |");
  20. }
  21.  
  22. # if no IP address entered via form then die and do not proceed
  23. else {
  24.  
  25. die;
  26. }
  27.  
  28. # loop through the system output
  29. while ($firstIP = <TRACE>) {
  30.  
  31. # only take the first IP address on a line
  32. $firstIP =~ /\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/;
  33.  
  34. $eachHOP = $1;
  35.  
  36. # prints each IP address on the hop path
  37. print "$eachHOP\n";
  38. }
  39.  
  40. close TRACE;
  41.  
  42.  
May 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.