【问题标题】:Create interactive ssh2 terminal window创建交互式 ssh2 终端窗口
【发布时间】:2014-12-18 22:16:50
【问题描述】:

下面的代码可以正常工作,它完美地返回了我连接到 SSH 服务器的用户名。是否可以创建某种交互式 shell,我可以自己发送命令。就像 Mac 上的黑色终端窗口一样。我看过一些关于 ssh2_shell 函数的文档,您可以在其中指定终端窗口的宽度和高度,但无法使其正常工作。

$server['ip'] = "127.0.0.1";
$server['sshport'] = 22;
$server['user'] = "myUsername";
$server['pw'] = "myPassword!";

$command = "whoami";

if($ssh = ssh2_connect($server['ip'], $server['sshport'])) {
    if(ssh2_auth_password($ssh, $server['user'], $server['pw'])) {
        $stream = ssh2_exec($ssh, $command);
        stream_set_blocking($stream, true);
        $data = "";
        while($buffer = fread($stream, 4096)) {
            $data .= $buffer;   
        }
        fclose($stream);
        print $data;
    } else {
        echo "User or password is incorrect!";
    }
} else {
    echo "Could not connect to the ssh server!";    
}

【问题讨论】:

    标签: php ssh libssh2


    【解决方案1】:

    遗憾的是,我认为 libssh2 不可能。我知道在 PHP 中这样做的唯一方法是使用 phpseclib:

    http://phpseclib.sourceforge.net/ssh/examples.html#top

    例子:

    <?php
    include('Net/SSH2.php');
    include('File/ANSI.php');
    
    $ssh = new Net_SSH2('www.domain.tld');
    if (!$ssh->login('username', 'password')) {
        exit('Login Failed');
    }
    
    $ansi = new File_ANSI();
    
    $ansi->appendString($ssh->read('username@username:~$'));
    $ssh->write("top\n");
    $ssh->setTimeout(5);
    $ansi->appendString($ssh->read());
    echo $ansi->getScreen(); // outputs HTML
    ?>
    

    【讨论】:

    • 我也不这么认为,如果它会的话,对吧?我去看看phpseclib,谢谢!
    • 是否有可能使这个动态?喜欢按 Enter 键执行 ajax 请求?
    猜你喜欢
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多