Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/223585-php-and-ssh/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/223585-php-and-ssh/#findComment-1156882
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.