php ssh2返回json数据

时间:2017-03-27 17:21:06

标签: php json

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

[
    {
            "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执行相同的命令时,我有这样的响应

[
{
    "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代码

            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

的数据

2 个答案:

答案 0 :(得分:0)

只需使用json_encode()方法从json对象

获取json字符串
    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;
    }

答案 1 :(得分:0)

Amersand in shell意味着在后台运行。您看到所有这些后台命令的输出。

在发送前退出&符号,这将停止