Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Net::OpenSSH in a mod_perl handler

by eserte (Deacon)
on Mar 24, 2010 at 14:38 UTC ( [id://830581]=perlquestion: print w/replies, xml ) Need Help??

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

I have no success in getting Net::OpenSSH working within a mod_perl handler, neither with mod_perl 1 nor with mod_perl 2. Here's some test code to exhibit the problem:
package test::openssh; use strict; use warnings; use constant MP2 => (($ENV{MOD_PERL_API_VERSION} || 0) >= 2 or eval "require Apache2::ServerUtil; Apache2::Se +rverUtil::server_root ()"); BEGIN { if (MP2) { require Apache2::Const; require Apache2::RequestRec; require Apache2::RequestUtil; Apache2::Const->import(qw(OK)); } else { require Apache::Constants; Apache::Constants->import(qw(OK)); } } use Net::OpenSSH; use File::Temp qw(tempdir); # Please change the following two lines: use constant SSH_HOST => 'user@host'; use constant SSH_PRIVATE_KEY => "/path/to/private/key/id_dsa"; sub handler ($$) { my($class, $r) = @_; my $ctl_dir = tempdir(CLEANUP => 1, TMPDIR => 1) or die $!; my $ssh = Net::OpenSSH->new(SSH_HOST, master_opts => ["-vvv", -i => SSH_PRIV +ATE_KEY, -o => "stricthostkeychecking=no"], ctl_dir => $ctl_dir, strict_mode => 0, ); die $ssh->error if $ssh->error; my $date = $ssh->capture("date"); $r->send_http_header; $r->print("hello, the date at " . $ssh->get_host . " is $date\n"); OK; } 1; __END__
The handler is installed into the Apache using this snippet in the httpd.conf:
<Perl> #use lib "/tmp/lib"; # if necessary </Perl> PerlModule test::openssh <Location "/mytest/openssh"> SetHandler perl-script PerlHandler test::openssh->handler </Location>
After setting up the private key and adding the public key to authorized_keys I tried to using the handler, but had no success. On an old Debian Linux system (perl 5.8.8, mod_perl 1.29) the error is:
[Wed Mar 24 15:31:29 2010] [error] unable to establish master SSH conn +ection: Unknown error at .../test/openssh.pm line 33
On a FreeBSD 8.0 system (perl 5.8.9, mod_perl 2.0.4) the error is a little bit different:
[Wed Mar 24 14:59:55 2010] [error] [client 127.0.0.1] child process ST +DIN is not a real system file handle at /tmp/lib/test/openssh.pm line + 29\n\t(in cleanup) child process STDIN is not a real system file han +dle at -e line 0\n
Any suggestions? I tried some variations with setting the stdin/stdout/stderr options of Net::OpenSSH, but still had no success.

Replies are listed 'Best First'.
Re: Net::OpenSSH in a mod_perl handler
by salva (Canon) on Mar 24, 2010 at 15:26 UTC
    Could you try using /dev/null as the default STDIN?
    open my $stdin_fh, '<', '/dev/null' or die "unable to open /dev/null"; my $ssh = Net::OpenSSH->new(SSH_HOST, default_stdin_fh => $stdin_fh, ... ); ...

    mod_perl is probably tie'ing STDIN to something that does not have a real file handle behind.

    update: besides that, it seems that there are some issue with capture not capturing anything... I am investigating it!

      This got me one step further: [Wed Mar 24 18:16:06 2010] [error] [client 127.0.0.1] child process STDOUT is not a real system file handle at /tmp/lib/test/openssh.pm line 40\n

      After setting the default_stdout_fh option using the same schema the $ssh->system command works, at least with mod_perl 2. $ssh->capture still does not work; there is no error, but also nothing is captured.

        The issue was related to STDIN and STDOUT not being attached to file descriptors 0 and 1 respectively under mod_perl.

        Can you try version 0.46_01 that I have just uploaded to CPAN?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://830581]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found