使用PHP通过SSH执行Shell命令 [英] Execute a shell command through ssh using PHP

查看:142
本文介绍了使用PHP通过SSH执行Shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一个简单的shell命令并将结果打印在网页上,但是结果为空.下面是我发现的一小段代码,但到目前为止没有任何效果.

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

推荐答案

尝试 phpseclib ,它将起作用

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

这篇关于使用PHP通过SSH执行Shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆