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

I have a php script which does an exec on a perl script. The perl script takes the input it has been given and should take a specified file and SCP it to a specified remote box. The script should also check first that the path to the final location of the remote file exists. If it doesn't exist it should create it first before it SCP's the file.

What I've determined is that, I can SCP stuff just fine with Net::SCP::Expect as long as the directory already exists. However if the directory doesn't exist it doesn't work.

So I turn to Net::SSH::Perl to run a few commands to test if the directories exist or not. However my script seems to die there.

But of course if I call the script from the command line instead of PHP all is wonderful.

So figuring it's permissons problem for the apache user, I work on that. I temporarly give the apache user a login shell and su - apache. I then run the script. At the line where it does $ssh->cmd($tcmd) I got a error message:

mkdir /var/www/.ssh: Permission denied at /usr/lib/perl5/site_perl/5.8.5/Net/SSH/Perl/Util/Hosts.pm line 51

So I set up a .ssh dir there with the permissions for the apache user to write to it. I test again as the apache user from the command line and everything is fine.

Ok great. Reset the apache user's shell to /sbin/nologin and try it from the web again. No dice.

So this time I try it with sudo -u apache ... That gives me a similar error except instead of trying to write to /var/www it wants to write to /root

Ok that's not going to happen (unless y'all tell me it has to happen).

So now I'm stuck. What can I do to make this work when called from the php script?

use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new($remote_host, protocol=>2) or die("could + not connect"); $ssh->login($username", $password"); my $tcmd = "ls -la"; print "about to issue tcmd: $tcmd"; my($tout, $terr, $texit) = $ssh->cmd($tcmd); # this is the line that d +ies print "OUT:$tout, ERR:$terr, EX:$texit\n";
I should add both boxes are RHEL 4. The webserver is Apache 2 with PHP5. The PHP code is very simple
$output = system("/export/home/test/public_html/devel/scp_file.pl $loc +al_filename $remote_box $remote_base $remote_path $remote_file", $ret +urn_var);

Replies are listed 'Best First'.
Re: permission problem with Net::SSH::Perl
by almut (Canon) on Feb 08, 2007 at 19:22 UTC
    That gives me a similar error except instead of trying to write to /var/www it wants to write to /root

    Not sure, but maybe it would help to explicitly specify the place where you want the known hosts info to be kept, e.g.

    my $ssh = Net::SSH::Perl->new( $remote_host, protocol=>2, options => [ "UserKnownHostsFile /some/path/.ssh/known_hosts" +] # or "GlobalKnownHostsFile /some/path/.ssh/known_ +hosts" ) or die("could not connect");
Re: permission problem with Net::SSH::Perl
by suaveant (Parson) on Feb 08, 2007 at 20:15 UTC
    Seems like I had a similar problem back when I was playing with this module and fixed it by setting $ENV{HOME} to the proper (read script writable) directory.

                    - Ant
                    - Some of my best work - (1 2 3)