runder Posted January 6, 2011 Share Posted January 6, 2011 Hi all, I'm trying to run a bash script on a remote machine using ssh via a PHP page. I have tried the following: Code #1 define('SSHCMD', 'ssh user@hostname /path/to/myscript'); $ssh = popen(SSHCMD, "r"); $output = fread($ssh, 4096) pclose($ssh) Code #2 exec("/path/to/a_script_that_does_ssh_to_remote_machine") And I also tried using "Passthru" instead of exec. Both code seems to connect correctly to the remote machine, but the script that is on the remote machine requires time to execute. Unfortunately, PHP will disconnect the ssh session without letting time for the script to finish. When monitoring the output, I see that the script executed it first few lines and then got cut-off or killed. I just want to let the remote script execute, let it finish, let it run in the background, focus can be controller to PHP without caring about what the script does. I don't need any return value and I do not care for the outcome of the script. Does anyone have a recommendation on how I can achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/223585-php-and-ssh/ Share on other sites More sharing options...
JonnoTheDev Posted January 6, 2011 Share Posted January 6, 2011 Simple. To send as a background process use the following: exec("php /path/to/script.php > /dev/null &"); Quote Link to comment https://forums.phpfreaks.com/topic/223585-php-and-ssh/#findComment-1155731 Share on other sites More sharing options...
runder Posted January 7, 2011 Author Share Posted January 7, 2011 Wow.. that definitely worked! I used ampersand previously, but I did not think of dev/null ampersand! Quote Link to comment https://forums.phpfreaks.com/topic/223585-php-and-ssh/#findComment-1156104 Share on other sites More sharing options...
lastkarrde Posted January 7, 2011 Share Posted January 7, 2011 the extension ssh2 may be of some use to you. (docs). Quote Link to comment https://forums.phpfreaks.com/topic/223585-php-and-ssh/#findComment-1156147 Share on other sites More sharing options...
mozillaen Posted January 9, 2011 Share Posted January 9, 2011 Personally, I would recommend phpseclib, a pure PHP SFTP implementation and replacing fsockopen() with pfsockopen(). The problem with lastkarrde's recommendation is that it, among other things, makes for much less portable code (the ssh2 extension isn't installed on very many hosts), it has a far more intuitive OOP-based API and RSA authentication actually makes sense (the ssh2 extension requires you store the public key and the private key separately on the file system; the fact that they have to be separately provided is silly since most private key formats *include* the public key within them). phpseclib is also faster: http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/#comment_3759 Quote Link to comment https://forums.phpfreaks.com/topic/223585-php-and-ssh/#findComment-1156882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.