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);

In reply to permission problem with Net::SSH::Perl by xorl

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.