代码之家  ›  专栏  ›  技术社区  ›  Lukas Gund

php原始ssh2\u连接不返回值

  •  0
  • Lukas Gund  · 技术社区  · 7 年前

    我想创建到teamspeak服务器的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 回复  |  直到 7 年前
        1
  •  0
  •   Lukas Gund    7 年前

    问题

    我在这里得到了一个超时原因流等待,直到它将被关闭。

    解决方案

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