doufan2541 2018-09-27 11:17
浏览 114
已采纳

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 00: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 Mysql 一张表同时多人查询和插入怎么防止死锁
  • ¥20 centos6.7 安装libevent库.总是报错,如何解决?
  • ¥15 电脑买回,学校的有线网络总掉。
  • ¥20 关于普洛菲斯触摸屏与AB连接地址问题
  • ¥15 vue但是页面显示的数据为空为什么呀,明明在钩子函数中已经成功赋值(相关搜索:输出数据)
  • ¥15 syri可视化不显示插入缺失
  • ¥30 运行软件卡死查看系统日志分析不出来
  • ¥15 C语言代码改正特征选择算法设计,贝叶斯决策,,设计分类器,远程操作代码修正一下
  • ¥15 String 类valuve指向的问题
  • ¥15 在ros2的iron版本进行编译时遇到如下问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部