http://www.perlmonks.org?node_id=965249

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

Hi, I'm wondering if someone can help me with an issue I'm having when using OpenSSH. I am setting up a connection and executing a command using capture. The command runs successfully and I get the output. However, the page does not finish loading (its status remains "Transferring data from 192.168.0.1"). Consequently, javascript code which should run when the page completes loading does not run.

I have tried various options and combinations but have not managed to solve this. Can I force closing the connection?

This is the code I'm using:
my ($def_in, $def_out); open $def_in, '<', '/dev/null' or die "unable to open /dev/null"; open $def_out, '>', '/dev/null' or die "unable to open /dev/null"; my $host = '192.168.0.1'; my %opts = ( user => 'root', batch_mode => 1, key_path => '~/.ssh/id_rsa', ssh_cmd => '/usr/bin/ssh', default_stdin_fh => $def_in, default_stdout_fh => $def_out, kill_ssh_on_timeout => 1, timeout => 5 ); my $ssh = Net::OpenSSH->new( $host, %opts ); $ssh->error and die "Unable to connect to server. ".$ssh->error; my $text = $ssh->capture({stderr_to_stdout => 1, timeout=>1, stdin +_data => "cat /myfile.txt;exit\n"}); #$ssh->capture({stdin_data => "exit\n"}); undef $ssh;
Many thanks.

Replies are listed 'Best First'.
Re: Net::OpenSSH - page not loading completely
by Eliya (Vicar) on Apr 16, 2012 at 09:51 UTC

    You might want to use lsof to figure out what open file handle is keeping the respective webserver process from considering the CGI call done.  Apache, for example, would wait for all stdout/stderr handles (i.e. of the CGI itself and any children the CGI forked) to be closed.

    You probably also need to point default_stderr_fh to /dev/null (or simply set master_stdout_discard => 1, master_stderr_discard => 1), but that's just a guess (i.e. untested).

      Tried this but did not work.
      my %opts = ( user => 'root', batch_mode => 1, key_path => '~/.ssh/id_rsa', #passwd => '', ssh_cmd => '/usr/bin/ssh', default_stdin_fh => $def_in, #default_stdout_fh => $def_out, master_stdout_discard => 1, master_stderr_discard => 1, kill_ssh_on_timeout => 1, timeout => 5 );
      I haven't found the file handle in question. Will need to read further how to track it. Thanks.

        Just tried it (on Ubuntu, apache 2.2.20), and it worked fine for me, even without fiddling with the default_* / master_* options.   I.e., the ssh master connection etc. is automatically being terminated properly.

        Maybe enabling debugging

        $Net::OpenSSH::debug |= 0x7f;

        will shed some light on what isn't working in your case (output ends up in the webserver's error log).