Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a shellscript with connects to a a different machine with ssh and a key so it does not need the username and password.

When i run this script from commandline it works fine.. but when I run this script from php shell_exec it does not work.

If I make an ssh connection with PHP and run the script as my own user it does work.

Now for my question :D Is there a way to just running the script in shell_exec from php without making an connection over ssh as a different user?

share|improve this question
1  
That's a quite poor description of you scenario. Anyway, are you sure that the commandline and the PHP use the same user, i.e. you are running the PHP from the commandline and not from a webserver? –  AndreKR Nov 8 '10 at 10:48
    
This sounds more like a sysadmin question. If the user PHP runs under has the right to execute the program in question, it is possible. Otherwise, it is not possible. –  Pekka 웃 Nov 8 '10 at 10:56
    
Php runs as www-data and when i connect to ssh under php I login under my own user account so definitly not the same user. Where it goes wrong is in the shellscript... ssh -n -i $KEYFILE $USER@$HOST "bash ./jpg2xml.sh /tmp/$tmpfile | ./ocr" which is run from the shellscript by php. It does run the script, only this part that is not working! –  Chris Nov 8 '10 at 11:11

3 Answers 3

up vote 1 down vote accepted

Did you specify the private key file correctly?

share|improve this answer
    
Arrr you are right good sir.. if i change the owner to www-data it works.. it was not readable for apache. –  Chris Nov 10 '10 at 7:20

If you are using Ubuntu or Debian the web server is running with the user name www-data. For other systems please check the web server configuration for the user name. You can simply test if this user (and your php web application) is able to do the SSH connection.

1) Become the user of your web server

sudo su www-data

2) Try connecting the remote host

ssh remoteUser@remoteHost

If you will get connected without entering a password there must be a different problem. If you have to enter a password, the key files were stored for a different user - not for www-data. You have already configured SSH to use the key. Do the same for your local user www-data and it will work.

share|improve this answer
    
But I connect in the shellscript as a different user then www-data so in theory it should not make a difference right? I do the following.. ssh -o StrictHostKeyChecking=no -n -i $KEYFILE $USER@$HOST –  Chris Nov 9 '10 at 7:21

It seems ssh connection does not work with shell_exec. If i run the shellscript under ssh2_exec it does seem to work.

Which is a little strange as the ssh connection is made in the script file with a public and private key.. I would assume this would just run :s

The webserver is allowed to execute the file, as there are other command in there who work as expected.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.