无法使用 phpseclib 登录到我的 apache 服务器

Can't login with phpseclib to my apache server

我的 apache 服务器中有一个 php 文件需要 运行 一个 shell 脚本(也在服务器中)。当我在 putty 中尝试代码时,它 运行 是预期的 shell 脚本。但是ssh连接不上,下面的代码给出了echos

"Disconnected from server!" "Login Failed".

<?php

$script = "sudo bash /path/script.sh parameter";
set_include_path( __DIR__ .'/phpseclib/' . PATH_SEPARATOR . 
get_include_path());
include('Net/SSH2.php');
$ssh = new Net_SSH2($server);
$ssh->enablePTY();

if (!$ssh->login($username, $password)) {
    echo $ssh->isConnected() ? 'Connected but Not Logged In' : 'Disconnected From Server!';
    exit("\nLogin Failed");
}
$ssh->setTimeout(1);
$ssh->write($script."\n");
echo "Initialized Shell Script\n";
$ssh->read('password for $username:');
echo "Password Confirmation\n";
$ssh->write($password.'\n');
echo "Password Login\n";
echo $ssh->read('[prompt]');
?>

Net/SSH2 库支持调试和错误报告。 The documentation for the library 描述如何调试函数和查看错误。

您可以在 exit("\nLogin Failed"); 函数之前调用函数 $ssh->getErrors()。它将 return 错误。