Hi,

I'm trying to connect to a remote machine and execute a command using SSH in PHP's exec function. When I try the complete command on the command prompt it works just fine but in PHP nothing happends. The command looks something like

exec("ssh the command to be executed");

Best Regards
Staffan

    exec() makes the server do something, but don't report anything "to the screen".

    Perhaps you may want to do something like:

    echo ssh whatever;

    This works like an exec() but echoes what would be prompt on the screen.

      Thanks for the reply but I don't really understand what you mean. If I just do an echo it will just print the text, right? I'll try to give a better explanation of what I'm trying to.

      I want to configure a router remotely with a php web-page. The access method from the web server to the router is ssh and I use one input-file and one output-file as an argument in the exec command. So the input-file (xml configuration file) is sent in the ssh session and the output-file is where the xml-reply ends up in. However the output-file is empty and nothing has changed in the router. But doing the same from the command prompt it works just fine. It seems like the exec command does not wait for ssh to finish, since it is completed in notime compared to the command prompt that takes a couple of seconds. Hope this will help.

      // Staffan

        Use system() instead. It will print the output of the program being executed. But ssh <user@host> <command> still requires a password to be provided. Maybe you should write an expect program and execute that from PHP.

        UPDATE you can also use backticks command to execute a shell command

          Hi again,
          We have now solved the problem. After checking all log-files we noticed that the problems were the web-server's permissions. Running one command as root does not mean it will run as another user. So by adding permissions for the apache-user and connecting the first ssh-session from the prompt (you will be prompted the first time if you want to add the destination in your known-hosts) everything works fine. However, a lot of time was spent to find this out, as well as that all users have different home directories, therefore looking for the ssh keys in different directories even though a location is specified.

          Best Regards
          // Staffan Dahlberg

            4 months later

            Originally posted by Staffan
            Hi again,
            We have now solved the problem. After checking all log-files we noticed that the problems were the web-server's permissions. Running one command as root does not mean it will run as another user. So by adding permissions for the apache-user and connecting the first ssh-session from the prompt (you will be prompted the first time if you want to add the destination in your known-hosts) everything works fine. However, a lot of time was spent to find this out, as well as that all users have different home directories, therefore looking for the ssh keys in different directories even though a location is specified.

            Best Regards
            // Staffan Dahlberg

            where you able to run shell or other commands after you succeeded to ssh to the remote box?

              ohh n/m .. schoolboy error.

              something like (using sudo):

              $output = `sudo ssh [email]user@host.host[/email] "command -options param; next command; | piped command; exit 0;"`;
              echo $output; //or I test stristr for something in the returned output
                2 months later

                You can get output from exec like this:

                exec("ls -l", $result);
                
                $buffer="";
                foreach($result as $line) { $buffer.=$line; }
                
                echo $buffer;
                

                  ls -l in the above is just an example, you could use any command here.

                    Write a Reply...