Execute a shell command through ssh using PHP

17,850

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);
?>
Share:
17,850
tomtomssi
Author by

tomtomssi

BY DAY: A Software Developer BY NIGHT: Working for Calicode

Updated on August 06, 2022

Comments

  • tomtomssi
    tomtomssi over 1 year

    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>';
        ?>