我正在尝试通过网页连接到远程Linux机器,提交命令并在网页本身上查看其输出。

这是我到目前为止所做的(在每个步骤之后,我都会回显文本,以便我可以跟进代码可以到达的位置)

如果从网页上摘录,代码将达到“ 2. phpseclib included \ n”

如果我在终端上运行此代码,它将结束!

<html>
<body>

  TEST <br/>

<?php

echo "1. start\n";
define('NET_SSH2_LOGGING', 2);

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib1.0.0');
include('Net/SSH2.php');


echo "2. phpseclib included\n";
$ssh = new Net_SSH2('HOSTNAME');
echo "3. after Net_SSH2\n";

if (!$ssh->login("USERNAME", "PASSWORD")) {
        echo "4. login failed";
        exit('Login Failed');
    } else {
       echo "4. login gained\n";
    }

echo "5. after login\n";


    if (!($stream = $ssh->exec("ls"))) {
            echo "fail: unable to execute command\n";
        } else {
          echo $stream;
            }

         if (!($stream = $ssh->exec("> foo.txt"))) {
            echo "fail: unable to execute command\n";
        } else {
          echo $stream;
            }

echo $ssh->getLog();
echo "6. end of code\n";
?>


</body>
</html>


如此看来,它在连接到远程计算机后似乎停止显示消息了?

如果这确实可行,我将开始涉猎吗?还是由于安全问题?

我已经从这里安装了SSH2库:http://phpseclib.sourceforge.net/

另外,我使用了相同的代码,但是我使用了require:

<html>
<body>

  TEST <br/>

<?php

echo "1. start\n";
define('NET_SSH2_LOGGING', 2);

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib1.0.0');
require('Net/SSH2.php');

echo "2. phpseclib included\n";
$ssh = new Net_SSH2('HOST');
echo "3. after Net_SSH2\n";

if (!$ssh->login("USERNAME", "PASSWORD")) {
        echo "4. login failed";
        exit('Login Failed');
    } else {
       echo "4. login gained\n";
    }

echo "5. after login\n";


    if (!($stream = $ssh->exec("ls"))) {
            echo "fail: unable to execute command\n";
        } else {
          echo $stream;
            }

         if (!($stream = $ssh->exec("> foo.txt"))) {
            echo "fail: unable to execute command\n";
        } else {
          echo $stream;
            }

echo $ssh->getLog();
echo "6. end of code\n";
?>


</body>
</html>


解决了:
通过PEAR安装库

最佳答案

这条线是问题

set_include_path(get_include_path()。get_include_path()。'/ phpseclib');

这里有两个问题,第一个是在注释中提到的,您正在尝试从“ / your / include / path / your / include / path / phpseclib”而不是“ / your / include / path / phpseclib”加载内容

如果您在包含路径中安装了phpseclib,则只需删除get_include_path之一,就可以继续进行代码。还可以阅读评论。

10-08 19:22