首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php ssh2返回json数据

php ssh2返回json数据
EN

Stack Overflow用户
提问于 2017-03-28 01:21:06
回答 2查看 274关注 0票数 0

我在ssh2中遇到了返回数据的问题,当我在终端ssh中执行命令时,结果返回如下

代码语言:javascript
复制
[
    {
            "mac": "xx:xx:xx:xx:xx",
            "name": "test",
            "distance": 630,
            "txpower": 13,
            "noisefloor": -96,
            "airmax": {
                    "priority": 3,
                    "quality": 0,
                    "beam": 255,
                    "signal": -96,
                    "capacity": 0
            },
            "stats": {
                    "rx_data": 5973340,
                    "rx_bytes": 8021746863,
                    "rx_pps": 36,
                    "tx_data": 3666039,
                    "tx_bytes": 690527643,
                    "tx_pps": 26
            },
            "rates": [ "MCS0", "MCS1", "MCS2", "MCS3", "MCS4", "MCS5", "MCS6", "MCS7", "MCS8", "MCS9", "MCS10", "MCS11", "MCS12", "MCS13", "MCS14", "MCS15" ],
            "signals": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -67, -66, -65, 0 ],
            "remote": {
                    "uptime": 3451971,
                    "hostname": "test",
                    "platform": "Rocket",
                    "version": "1",
                    "distance": 1050
            }
    }

]

当我在php中使用ssh2执行相同的命令时,我得到的响应如下

代码语言:javascript
复制
[
{
    "mac": "xx:xx:xx:xx:xx:xx"
[1] => 
    "name": "test"
[2] => 
    "lastip": "1.1.1.1"
[3] => 
    "associd": 6
[4] => 
    "aprepeater": 0
[5] => 
    "tx": 108.0
[6] => 
    "rx": 240.0
[7] => 
    "signal": -62
[8] => 
    "rssi": 34
[9] => 
    "chainrssi": [ 33 
[10] =>  29 
[11] =>  0 ]
[12] => 
    "rx_chainmask": 3
[13] => 
    "ccq": 94
[14] => 
    "idle": 0
[15] => 
    "tx_latency": 1
[16] => 
    "uptime": 80463
[17] => 
    "ack": 22
[18] => 
    "distance": 0
[19] => 
    "txpower": 13
[20] => 
    "noisefloor": -96
[21] => 
    "airmax": {
        "priority": 3
[22] => 
        "quality": 0
[23] => 
        "beam": 255
[24] => 
        "signal": -96
[25] => 
        "capacity": 0
    }
[26] => 
    "stats": {
        "rx_data": 10962443
[27] => 
        "rx_bytes": 15130635403
[28] => 
        "rx_pps": 2
[29] => 
        "tx_data": 7018200
[30] => 
        "tx_bytes": 1135210790
[31] => 
        "tx_pps": 4
    }
[32] => 
    "rates": [ "MCS0"
[33] =>  "MCS1"
[34] =>  "MCS2"
[35] =>  "MCS3"
[36] =>  "MCS4"
[37] =>  "MCS5"
[38] =>  "MCS6"
[39] =>  "MCS7"
[40] =>  "MCS8"
[41] =>  "MCS9"
[42] =>  "MCS10"
[43] =>  "MCS11"
[44] =>  "MCS12"
[45] =>  "MCS13"
[46] =>  "MCS14"
[47] =>  "MCS15" ]
[48] => 
    "signals": [ 0
[49] =>  0
[50] =>  0
[51] =>  0
[52] =>  0
[53] =>  0
[54] =>  0
[55] =>  0
[56] =>  0
[57] =>  0
[58] =>  0
[59] =>  -63
[60] =>  -64
[61] =>  -66
[62] =>  -66
[63] =>  -66 ]
[64] => 
    "remote": {
        "uptime": 3469376
[65] => 
        "hostname": "Test"
[66] => 
        "platform": "Rocket"
[67] => 
        "version": "1"
[68] => 
        "signal": -69
[69] => 
        "tx_power": 27
[70] => 
        "rssi": 27
[71] => 
        "chainrssi": [ 19 
[72] =>  21 
[73] =>  0 ]
[74] => 
        "rx_chainmask": 3
[75] => 
        "tx_latency": 1
[76] => 
        "noisefloor": -89
[77] => 
        "distance": 1050
    }
}

]

我使用的php代码

代码语言:javascript
复制
            if(($stream=ssh2_exec($conn,$command))) 
            {
                stream_set_blocking($stream, true);
                $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
                return stream_get_contents($stream_out);
            }

我需要使用php ssh2执行命令并返回类似终端ssh的数据。

EN

回答 2

Stack Overflow用户

发布于 2017-10-31 19:49:06

只需使用json_encode()方法从json对象中获取json字符串

代码语言:javascript
复制
    if(($stream=ssh2_exec($conn,$command)))
    {
        stream_set_blocking($stream, true);
        $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
        $streamString = stream_get_contents($stream_out);
        $Result = json_encode($streamString);
        return $Result;
    }
票数 0
EN

Stack Overflow用户

发布于 2017-10-31 19:51:08

Amersand in shell的意思是在后台运行。您可以看到所有这些后台命令的输出。

在发送之前对与号进行转义,这将停止

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43052726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档