Search code examples
phpssh2-exec

ssh2_exec password encryption


How can I call ssh2_exec function in php to execute a command in a remote server without hardcoding my server password?

Here's the PHP manual reference:

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'myusername', 'mypasswordisvisibleforeveryone');

$stream = ssh2_exec($connection, '/usr/local/bin/php -i');
?>

Solution

  • I would setup a user just for access from this server then user a public ssh key

    You can connect like that using the following code...

    ssh2_auth_pubkey_file($ssh, 'user', '/location/to/.ssh/id_rsa.pub', '/location/to/.ssh/id_rsa', 'passphrase');
    

    where $ssh is your $connection