Execute a shell command through ssh using PHP

php, shell, ssh

Solution

Try phpseclib, that'll work.

<?php
    include('Net/SSH2.php');

    $server = "myserver";
    $username = "myadmin";
    $password = "mypass";
    $command = "ps";

    $ssh = new Net_SSH2($server);
    if (!$ssh->login($username, $password)) {
        exit('Login Failed');
    }

    echo $ssh->exec($command);
?>

Problem

I'm trying to execute a simple shell command and print the result on a web page but the results are empty. Below is one bit of code I found but nothing has worked thus far. ``` <?php $server = "myserver"; $username = "myadmin"; $command = "ps"; $str = "ssh " .$username. "@" .$server. " " .$command; exec($str, $output); echo '<pre>'; print_r($output); echo '</pre>'; ?> ```

Original source

Related problems