in reply to Re^2: Asynchronous Processing a command execution
in thread Asynchronous Processing a command execution

Please show some effort on your part by reducing the problems to the absolute minimum of code. In your case, the problem is that your child process running the ssh command does not produce output. This is because you wrongly transcribed merlyn's code:

# Original code: =42= my $buf = ""; =43= while (<F>) { =44= $buf .= $_; =45= $cache->set($session, [0, $buf]); =46= } =47= $cache->set($session, [1, $buf]); =48= exit 0;

Your code does not read from the STDOUT of the SSH process. In fact, I don't think you understood how Net::SSH works at all. I think it directly returns the output of the command after the command has run, so using Net::SSH won't help you, at least in the way you've used it here:

$cmd="ls -l"; my($stdout, $stderr, $exit) = $ssh->cmd($cmd); my $buf = ""; while ($stdout) { $buf .= $_; $cache->set($session, [0, $buf +]); } $cache->set($session, [1, $buf]);

Please review your code and the documentation for Net::SSH and consider how they can be used for your program.

Replies are listed 'Best First'.
Re^4: Asynchronous Processing a command execution
by Alfaromeo (Initiate) on Jul 04, 2008 at 08:49 UTC
    Corion , Please check the below code.I have slightly modified the code by excluding SSH and putting in the old code in itself.But this page is called from another HTML. If this works , I will modify the section in fork for SSH.Right now it shows premature end of script headers error.
    !/usr/bin/perl use strict; $l++; use CGI qw(:all); # import shortcuts use Fcntl qw(:flock); # imports LOCK_EX, LOCK_SH, LOCK_NB use CGI::Carp qw(warningsToBrowser fatalsToBrowser); # For Debugging use Net::SSH::Perl; use CGI qw(:all delete_all escapeHTML); use Net::SSH::Perl::Constants qw( :msg ); my ($TITLE,$DeploySvr,$KIT,$ssh,$UNM,$Pass,$stdout,$stderr,$exit,$cmd, +$session,$cache,$data,$pid,$packet,); if (my $session = param('session')) { # returning to pick up session data $cache = get_cache_handle(); $data = $cache->get($session); unless ($data and ref $data eq "ARRAY") { # something is wrong exit 0; } print header; print start_html(-title => "Logging...",($data->[0] ? () + : (-head => ["<meta http-equiv=refresh content=5>"]))); print h1("Logging..."); print pre(escapeHTML($data->[1])); print p(i("... continuing ...")) unless $data->[0]; print end_html; } else { $session = get_session_id(); $cache = get_cache_handle(); $cache->set($session, [0, ""]); # no data yet $DeploySvr=param('DrpServer'); $KIT=param('TxtKit'); $UNM=param('username'); $Pass=param('password'); if ($pid = fork) { # parent does delete_all(); # clear parameters param('session', $session); print redirect(self_url()); } elsif (defined $pid) { # child does close STDOUT; # so parent can go o +n $DeploySvr="113.128.122.27"; unless (open F, "-|") { open STDERR, ">&=1"; exec "/usr/sbin/traceroute", $DeploySv +r; die "Cannot execute traceroute: $!"; } my $buf = ""; while (<F>) { $buf .= $_; $cache->set($session, [0, $buf]); } $cache->set($session, [1, $buf]); } else { die "Cannot fork: $!"; } } sub get_cache_handle { require Cache::FileCache; Cache::FileCache->new ({ namespace => 'LogOutput', username => 'nobody', default_expires_in => '30 minutes', auto_purge_interval => '4 hours', }) } sub get_session_id { require Digest::MD5; Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().$$)); }

      You don't show any effort in fixing your errors. See the webserver error log for why your program does not run at all. This is a basic CGI debugging problem and has little if nothing to do with SSH at all.

      A reply falls below the community's threshold of quality. You may see it by logging in.