doufan2541 2018-09-27 19:17
浏览 113
已采纳

php raw ssh2_connection不返回值

What I want do

I want to create a ssh connection to a teamspeak server. With that raw connection it should be possible to put and get commands from the console.

What I´ve

        $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);
            }
        }

What´s wrong

Cause of @stream_set_blocking($this->runtime['socket'], true); this line my script is running into a timeout. Also I´ve got some the problem that fgets always return a empty result. Did I did something wrong?

  • 写回答

1条回答 默认 最新

  • douyinliu8813 2018-10-02 08:46
    关注

    Problem

    I got here a timeout cause the stream waiting until it will be closed.

    Solution

    If I close the stream after my read and write stuff I got my answer.

                        @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' . "
    ");
                        $this->runtime['socket'] = $socket;
                        return $this->generateOutput(true, array(), true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛