PHP的原始ssh2_connection不返回值

时间:2018-09-27 19:18:00

标签: php ssh stream

我想做什么

我想与Teampeak服务器建立SSH连接。通过该原始连接,应该可以从控制台放置和获取命令。

我所拥有的

        $socket = @ssh2_connect($this->runtime['host'], $this->runtime['queryport']);

        if($socket === false)
        {
            $this->addDebugLog('Error: connection failed!');
            return $this->generateOutput(false, array('Error: connection failed!'), false);
        }
        else
        {
            if(@ssh2_auth_password($socket, $this->escapeText($username), $this->escapeText($password)))
            {
                if(($shell = @ssh2_shell($socket, "raw")) === false)
                {
                    return $this->generateOutput(false, array('Error: failed to open a secure shell on server!'), false);
                }
                else
                {
                    $this->runtime['ssh'] = $socket;
                    $this->runtime['socket'] = $shell;
                    @stream_set_timeout($this->runtime['socket'], $this->runtime['timeout']);
                    @stream_set_blocking($this->runtime['socket'], true);
                    return $this->generateOutput(true, array(), true);
                }
            }
            else
            {
                return $this->generateOutput(false, array('Error: invalid loginname or password!'), false);
            }
        }

怎么了

由于@stream_set_blocking($this->runtime['socket'], true);的这一行,我的脚本运行超时。另外,我还有一些问题,即fgets总是返回空结果。我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

问题

我在这里超时,导致流一直等待直到将其关闭。

解决方案

如果我在阅读和写入内容后关闭流,那么我会得到答案。

                    @stream_set_timeout($this->stream, intval($this->runtime['timeout']));
                    @stream_set_blocking($this->stream, true);
                    echo fgets($this->stream);
                    echo fgets($this->stream);
                    fwrite($this->stream, 'quit' . "\n");
                    $this->runtime['socket'] = $socket;
                    return $this->generateOutput(true, array(), true);