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

Hello Monks,
I need to log the traceroute output to a file (this is a continuos process)... i have written this code ... what are the drawbacks here and any suggestions on how to improve this ... please let me know your thoughts
#!/usr/bin/perl use POSIX qw(setsid); print "Starting Fork \n"; defined($pid=fork) or die "Can't fork: $!"; print"pid is $pid \n"; if($pid) { ### this is parent print " In Parent \n"; exit(0); }else { print " In child\n"; open(F1, ">>/home/kirankm/trace.log") || die "Cannot open file + $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $ +!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $ +!"; setsid or die "Can't start a new session: $!"; umask 0; while(1) { $retval=`traceroute mysite.com`; print F1 "$retval \n"; } }

Replies are listed 'Best First'.
Re: Log Traceroute
by valdez (Monsignor) on May 10, 2003 at 12:20 UTC

    If your purpose is only to log the output of traceroute, then you should follow LameNerd's suggestion. If you need also to parse the logged output, then using Net::Traceroute is a simpler solution. It is currently implemented as a parser around the system traceroute command and produces informations that can be easily accessed. For example:

    #!/usr/bin/perl use strict; use warnings; use Net::Traceroute; my $tr = Net::Traceroute->new( host => 'www.students.cs.unibo.it', queries => 1); if ($tr->found) { my $hops = $tr->hops; foreach (1..$hops) { printf("%02d: %-15s %d %.3f ms\n", $_, $tr->hop_query_host($_, 1), $tr->hop_query_stat($_, 1), $tr->hop_query_time($_, 1)); } }

    Given this sample code, it should fairly easy to do whatever you want with the retrieved data.

    Ciao, Valerio

Re: Log Traceroute
by LameNerd (Hermit) on May 10, 2003 at 10:26 UTC
    Why not just do this ...
    $ while true > do > traceroute mysite.com >> /home/kirankm/trace.log > done