This is really just a modified and extended version of merlyn's Watching long processes through CGI. Particularly, I simplified the reading of output from the program that is run. You simply enter a host name, and select either 'Traceroute', 'Ping', or 'wget' and get the results. I learned quite a bit from that column :)
#!/usr/bin/perl -Tw use strict; use CGI qw(:all delete_all escapeHTML); $|++; $ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/ +sbin"; my %progs = ( 'Traceroute' => 'traceroute', 'Ping' => 'ping -c 15', 'wget' => 'wget -q -O -' ); my $cache = get_cache_handle(); if (my $session = param('session')) { my $data = $cache->get($session); unless ($data and ref $data eq "ARRAY") { show_form(); } print header(), start_html( -title => 'Results', ($data->[0] ? () : (-head => ["<meta http-equiv=refresh content= +5>"])) ), h1('Results'), pre( escapeHTML($data->[1]) ); print p( em('... continuing ...')) unless $data->[0]; print end_html(); exit 0; } elsif (my $host = param('host')) { ($host) = $host =~ /^([a-zA-Z0-9.\-]{1,100})\z/ ? $1 : show_form(); my $session = get_session_id(); $cache->set($session, [0, ""]); if (my $pid = fork) { delete_all(); param('session', $session); print redirect(self_url()); } elsif (defined $pid) { close STDOUT; close STDERR; my ($prog) = grep { param($_) } keys %progs; show_form() unless $prog; open STDOUT, "$progs{$prog} $host |"; my $buf; while (<STDOUT>) { $buf .= $_; $cache->set($session, [0, $buf]); } $cache->set($session, [1, $buf]); exit 0; } else { die "Cannot fork: $!"; } } else { show_form(); } sub show_form { print header(), start_html('Select Tool'), h1('Enter Host and Select Tool'), start_form(), textfield('host'), ' ', submit('Traceroute'), ' ', submit('Ping'), ' ', submit('wget'), end_form(), end_html(); exit 0; } sub get_cache_handle { require Cache::FileCache; Cache::FileCache->new({ namespace => 'tools', username => 'nobody', default_expires_in => '10 minutes', auto_purge_interval => '2 hours', }); } sub get_session_id { require Digest::MD5; Digest::MD5::md5_hex( Digest::MD5::md5_hex(time().{}.rand().$$) ); }

In reply to CGI Traceroute, Ping, and wget by Coruscate

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.