【问题标题】:PHP telnet parsing data output for mysql tablePHP telnet 解析mysql表的数据输出
【发布时间】:2022-11-06 06:27:54
【问题描述】:

大家好,我正在开发一个小项目并通过它学习 php 编码.. 但我被困在下面的一个新问题上.. 我正在使用 PHPtelnet 发送 cmds 并从设备获取数据回复..

所以我发送了一个 cmd 来检查我正在使用下面的示例代码测试的设备在特定端口上连接的设备总数。 代码发送 cmd,并解析回复以使用特定项目更新 mysql 表 代码上描述的。

问题是在具有多达 10 个设备的特定硬件上,我可以解析每个设备的每个细节并正确更新到 mysql,但是在具有超过 10 个设备的端口上,例如下面发布的示例.. 超过 30 个设备.. 它吐出一些错误不确定它的缓冲区大小还是什么..但是在连接到设备的远程网络上使用相同的 cmd 我得到了正确的输出..

在代码下方

   /*Displays all the devices on the specific PON*/
   /*PON1*/
   $show_pon[0] = "show ont info 1 all";
   $telnet->DoCommand($show_pon, $data_response);
  // Data is a raw string
  $sections = preg_split('/-{50,}\s*\r?\n/s', $data_response);
  $rows = preg_split('/\r?\n/', trim($sections[2]));
  $query_chunks = [];
  $query_data = [];

  foreach( $rows as $row )
  {
  $query_chunks[] = "(?,?,?,?,?,?,?,?,?)";
  $query_data = array_merge($query_data, preg_split("/\s+/", trim($row)));

  }

  $query = "INSERT INTO ont_onu (frame_slot, pon, ont_id, mac_address, control_flag, run_state, config_state, match_state, description) VALUES "  . implode( ', ', $query_chunks ) ;

  $pdo_dbh = new PDO('mysql:host=localhost;dbname=MYDBNAME', 'MYDBUSER', 'MYDBPASSWD');
  $sth = $pdo_dbh->prepare( $query );
  $sth->execute( $query_data );
   
  die(print_r($query_data, true));   

下面我在屏幕上打印出以下输出

Array 
 ( 
 [0] => 0/0 [1] => 1 [2] => 1 [3] => 80:14:A8:A6:B8:E2 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-TA. 
 [9] => 0/0 [10] => 1 [11] => 2 [12] => 80:14:A8:AA:45:80 [13] => active [14] => online [15] => success [16] => match [17] => ONU-EXT-NA. 
 [18] => 0/0 [19] => 1 [20] => 3 [21] => 80:14:A8:98:12:F0 [22] => active [23] => online [24] => success [25] => match [26] => EURIZAM-MO. 
 [27] => 0/0 [28] => 1 [29] => 4 [30] => E0:67:B3:A6:56:73 [31] => active [32] => online [33] => success [34] => match [35] => DOUGLAS-CA. 
 [36] => 0/0 [37] => 1 [38] => 5 [39] => C4:70:0B:26:B1:58 [40] => active [41] => online [42] => success [43] => match [44] => AFONSO-LEM. 
 [45] => 0/0 [46] => 1 [47] => 6 [48] => 80:14:A8:32:0B:60 [49] => active [50] => online [51] => failed [52] => match [53] => ALINE-FERR. 
 [54] => 0/0 [55] => 1 [56] => 7 [57] => 80:14:A8:A6:BB:13 [58] => active [59] => online [60] => success [61] => match [62] => ONU-EXT-R.. 
 [63] => 0/0 [64] => 1 [65] => 8 [66] => E0:E8:E6:4E:74:C0 [67] => active [68] => online [69] => success [70] => match [71] => MARCIA-CAR. 
 [72] => 0/0 [73] => 1 [74] => 9 [75] => 80:14:A8:A6:54:1F [76] => active [77] => online [78] => success [79] => match [80] => ONU-EXT-JE. 
 [81] => 0/0 [82] => 1 [83] => 10 [84] => E0:67:B3:7E:03:CA [85] => active [86] => online [87] => success [88] => match [89] => MARIA-AGUI 
 [90] => --More [91] => ( [92] => Press [93] => 'Q' [94] => to [95] => quit [96] => )-- [97] =>  [98] => . 
  [99] => 0/0 [100] => 1 [101] => 11 [102] => C4:70:0B:26:B2:E0 [103] => active [104] => online [105] => success [106] => match [107] => CRISTINO-A. 
 [108] => 0/0 [109] => 1 [110] => 12 [111] => 44:55:B1:07:7E:11 [112] => active [113] => online [114] => success [115] => match [116] => IRACEMA-SI. 
  [117] => 0/0 [118] => 1 [119] => 13 [120] => 80:14:A8:AA:6D:F0 [121] => active [122] => online [123] => success [124] => match [125] => ONU-EXT 
  [126] => 0/0 [127] => 1 [128] => 14 [129] => E0:E8:E6:4E:55:10 [130] => active [131] => online [132] => success [133] => match [134] => LENILDE-OL. 
  [135] => 0/0 [136] => 1 [137] => 15 [138] => E0:67:B3:B2:FC:8F [139] => active [140] => online [141] => success [142] => match [143] => VINICIUS-N. 
  [144] => 0/0 [145] => 1 [146] => 16 [147] => 80:14:A8:A6:BB:17 [148] => active [149] => online [150] => success [151] => match [152] => ONU-EXT-RJ. 
  [153] => 0/0 [154] => 1 [155] => 17 [156] => 80:14:A8:A6:C0:B8 [157] => active [158] => online [159] => success [160] => match [161] => ONU-EXT-MA. 
  [162] => 0/0 [163] => 1 [164] => 18 [165] => 80:14:A8:A6:BB:05 [166] => active [167] => online [168] => success [169] => match [170] => ONU-EXT-DI. 
  [171] => 0/0 [172] => 1 [173] => 19 [174] => 80:14:A8:A6:BB:18 [175] => active [176] => online [177] => success [178] => match [179] => ONU-FRANCI. 
  [180] => 0/0 [181] => 1 [182] => 20 [183] => E0:67:B3:88:5C:4A [184] => active [185] => online [186] => success [187] => match [188] => EDILEUZA-A. 
  [189] => 0/0 [190] => 1 [191] => 21 [192] => E0:67:B3:7E:0A:B1 [193] => active [194] => online [195] => success [196] => match [197] => ANDRESA-RO. 
  [198] => 0/0 [199] => 1 [200] => 22 [201] => C4:70:0B:25:F1:60 [202] => active [203] => online [204] => success [205] => match [206] => MARCUS-SOU. 
 [207] => --More [208] => ( [209] => Press [210] => 'Q' [211] => to [212] => quit [213] => )-- 

)

但 telnet cmd 上的正确输出是

   OLT(config-interface-epon-0/0)# show ont info 1 all
 -----------------------------------------------------------------------------
   F/S  P  ONT MAC               Control   Run      Config   Match     Desc
           ID                    flag      state    state    state
  ----------------------------------------------------------------------------
   0/0  1  1   80:14:A8:A6:B8:E2 active    online   success  match     ONU-EXT-TA.
   0/0  1  2   80:14:A8:AA:45:80 active    online   success  match     ONU-EXT-NA.
   0/0  1  3   80:14:A8:98:12:F0 active    online   success  match     EURIZAM-MO.
   0/0  1  4   E0:67:B3:A6:56:73 active    online   success  match     DOUGLAS-CA.
   0/0  1  5   C4:70:0B:26:B1:58 active    online   success  match     AFONSO-LEM.
   0/0  1  6   80:14:A8:32:0B:60 active    online   failed   match     ALINE-FERR.
   0/0  1  7   80:14:A8:A6:BB:13 active    online   success  match     ONU-EXT-R..
   0/0  1  8   E0:E8:E6:4E:74:C0 active    online   success  match     MARCIA-CAR.
   0/0  1  9   80:14:A8:A6:54:1F active    online   success  match     ONU-EXT-JE.
   0/0  1  10  E0:67:B3:7E:03:CA active    online   success  match     MARIA-AGUI.
   0/0  1  11  C4:70:0B:26:B2:E0 active    online   success  match     CRISTINO-A.
   0/0  1  12  44:55:B1:07:7E:11 active    online   success  match     IRACEMA-SI.
   0/0  1  13  80:14:A8:AA:6D:F0 active    online   success  match     ONU-EXT
   0/0  1  14  E0:E8:E6:4E:55:10 active    online   success  match     LENILDE-OL.
   0/0  1  15  E0:67:B3:B2:FC:8F active    online   success  match     VINICIUS-N.
   0/0  1  16  80:14:A8:A6:BB:17 active    online   success  match     ONU-EXT-RJ.
   0/0  1  17  80:14:A8:A6:C0:B8 active    online   success  match     ONU-EXT-MA.
   0/0  1  18  80:14:A8:A6:BB:05 active    online   success  match     ONU-EXT-DI.
   0/0  1  19  80:14:A8:A6:BB:18 active    online   success  match     ONU-FRANCI.
   0/0  1  20  E0:67:B3:88:5C:4A active    online   success  match     EDILEUZA-A.
   0/0  1  21  E0:67:B3:7E:0A:B1 active    online   success  match     ANDRESA-RO.
   0/0  1  22  C4:70:0B:25:F1:60 active    online   success  match     MARCUS-SOU.
   0/0  1  23  C4:70:0B:26:C2:78 active    online   success  match     RICARDO-PA.
   0/0  1  24  E0:67:B3:A6:56:65 active    online   success  match     PHATRICIA-.
   0/0  1  25  00:6D:61:59:ED:B0 active    online   success  match     CARINA-BRI.
   0/0  1  26  80:14:A8:A6:BB:11 active    online   success  match     ONU-JONATA.
   0/0  1  27  C4:70:0B:26:07:C8 active    online   success  match     CHRISTIANO.
   0/0  1  28  80:14:A8:7F:8D:48 active    online   success  match     CARLA-SOUZ.
   0/0  1  29  E0:67:B3:88:30:13 active    online   success  match     VALERIA-FA.
  -----------------------------------------------------------------------------
   Total: 29, online 29

正如我们所看到的,输出不完整,因为它似乎在完成输出之前崩溃了..或者可能读取了输出限制?

【问题讨论】:

  • 您能否提供您正在使用的 PHPTelnet 库的链接。还有var_dump($sections)

标签: php mysql


【解决方案1】:

在处理 cmd 的其余部分之前,以下输出会打印在 php 日志中

 --More ( Press 'Q' to quit )--  . 

显然它崩溃了,因为它没有按预期将正确的数据转发到 mysql pdo 语句。

这可能是 php 未处理所有完整 cmd 回复的 cmd 回复的缓冲区大小问题吗?

【讨论】:

    【解决方案2】:

    你能输出你得到的确切错误吗?

    标准的 MYSQL/PHP 可以轻松地一次进行 100k+ 次插入,而不会出现太大问题。

    $query_data = array_merge($query_data, preg_split("/s+/", trim($row))); 发生了很多事情,并且在逻辑上没有强制执行 $query_chunks 的要求。

    $show_pon[0] = "show ont info 1 all";
       $telnet->DoCommand($show_pon, $data_response);
      // Data is a raw string
      $sections = preg_split('/-{50,}s*
    ?
    /s', $data_response);
      $rows = preg_split('/
    ?
    /', trim($sections[2]));
      $query_chunks = [];
      $query_data = [];
    
      foreach( $rows as $row )
      {
        $data =preg_split("/s+/", trim($row));
        if (count($data) == 9){
            $query_data[] = $data;
            $query_chunks[] = "(?,?,?,?,?,?,?,?,?)";
        }
      }
    
      $query = "INSERT INTO ont_onu (frame_slot, pon, ont_id, mac_address, control_flag, run_state, config_state, match_state, description) VALUES "  . implode( ', ', $query_chunks ) ;
    
      $pdo_dbh = new PDO('mysql:host=localhost;dbname=MYDBNAME', 'MYDBUSER', 'MYDBPASSWD');
      $sth = $pdo_dbh->prepare( $query );
      $sth->execute( $query_data );
       
      die(print_r($query_data, true));  
    

    【讨论】:

    • 嗨 Marc .. 确保在代码错误下方附加了警告:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in ... 这指向代码行我的 pdo $sth->execute( $query_data );从第一篇文章中发布的代码。
    • 基本上我需要拆分所有细节,以便我可以通过 pdo mysql 将它们发送到我的数据库表,其中包含以下项目.. frame_slot、pon、ont_id、mac_address、control_flag、run_state、config_state、match_state、描述。要输入的值,所以我可以在稍后阶段读取它们。
    • 添加了一些代码,我无法测试它
    • 嗨.. 将对其进行测试并报告线程。
    • 嗨,Marc,我已经用您的代码对其进行了测试,但我仍然收到以下错误通知:第 227 行 /data.php 中的数组到字符串转换警告:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number绑定变量的数量与第 227 行 /data.php 中的标记数量不匹配
    【解决方案3】:

    下面附上完整的日志,有些东西正在中断读取..从阵列 20 向前。

    警告:PDOStatement::execute(): SQLSTATE[HY093]: 无效参数编号:绑定变量的数量与第 227 行 /data.php 中的标记数量不匹配

     Array 
     ( 
     [0] => Array ( [0] => 0/0 [1] => 1 [2] => 1 [3] => 80:14:A8:A6:B8:E2 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-TA. ) 
     [1] => Array ( [0] => 0/0 [1] => 1 [2] => 2 [3] => 80:14:A8:AA:45:80 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-NA. ) 
     [2] => Array ( [0] => 0/0 [1] => 1 [2] => 3 [3] => 80:14:A8:98:12:F0 [4] => active [5] => online [6] => success [7] => match [8] => EURIZAM-MO. ) 
     [3] => Array ( [0] => 0/0 [1] => 1 [2] => 4 [3] => E0:67:B3:A6:56:73 [4] => active [5] => online [6] => success [7] => match [8] => DOUGLAS-CA. ) 
     [4] => Array ( [0] => 0/0 [1] => 1 [2] => 5 [3] => C4:70:0B:26:B1:58 [4] => active [5] => online [6] => success [7] => match [8] => AFONSO-LEM. ) 
     [5] => Array ( [0] => 0/0 [1] => 1 [2] => 6 [3] => 80:14:A8:32:0B:60 [4] => active [5] => online [6] => failed [7] => match [8] => ALINE-FERR. ) 
     [6] => Array ( [0] => 0/0 [1] => 1 [2] => 7 [3] => 80:14:A8:A6:BB:13 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-R.. ) 
     [7] => Array ( [0] => 0/0 [1] => 1 [2] => 8 [3] => E0:E8:E6:4E:74:C0 [4] => active [5] => online [6] => success [7] => match [8] => MARCIA-CAR. ) 
     [8] => Array ( [0] => 0/0 [1] => 1 [2] => 9 [3] => 80:14:A8:A6:54:1F [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-JE. ) 
     [9] => Array ( [0] => 0/0 [1] => 1 [2] => 11 [3] => C4:70:0B:26:B2:E0 [4] => active [5] => online [6] => success [7] => match [8] => CRISTINO-A. ) 
     [10] => Array ( [0] => 0/0 [1] => 1 [2] => 12 [3] => 44:55:B1:07:7E:11 [4] => active [5] => online [6] => success [7] => match [8] => IRACEMA-SI. ) 
     [11] => Array ( [0] => 0/0 [1] => 1 [2] => 13 [3] => 80:14:A8:AA:6D:F0 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT ) 
     [12] => Array ( [0] => 0/0 [1] => 1 [2] => 14 [3] => E0:E8:E6:4E:55:10 [4] => active [5] => online [6] => success [7] => match [8] => LENILDE-OL. ) 
     [13] => Array ( [0] => 0/0 [1] => 1 [2] => 15 [3] => E0:67:B3:B2:FC:8F [4] => active [5] => online [6] => success [7] => match [8] => VINICIUS-N. ) 
     [14] => Array ( [0] => 0/0 [1] => 1 [2] => 16 [3] => 80:14:A8:A6:BB:17 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-RJ. ) 
     [15] => Array ( [0] => 0/0 [1] => 1 [2] => 17 [3] => 80:14:A8:A6:C0:B8 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-MA. ) 
     [16] => Array ( [0] => 0/0 [1] => 1 [2] => 18 [3] => 80:14:A8:A6:BB:05 [4] => active [5] => online [6] => success [7] => match [8] => ONU-EXT-DI. ) 
     [17] => Array ( [0] => 0/0 [1] => 1 [2] => 19 [3] => 80:14:A8:A6:BB:18 [4] => active [5] => online [6] => success [7] => match [8] => ONU-FRANCI. ) 
     [18] => Array ( [0] => 0/0 [1] => 1 [2] => 20 [3] => E0:67:B3:88:5C:4A [4] => active [5] => online [6] => success [7] => match [8] => EDILEUZA-A. ) 
     [19] => Array ( [0] => 0/0 [1] => 1 [2] => 21 [3] => E0:67:B3:7E:0A:B1 [4] => active [5] => online [6] => success [7] => match [8] => ANDRESA-RO. ) 
     [20] => Array ( [0] => 0/0 [1] => 1 [2] => 22 [3] => C4:70:0B:25:F1:60 [4] => active [5] => online [6] => success [7] => match [8] => MARCUS-SOU. ) 
    

    )

    下面是完整的 cmd telnet 回复,我已经通过 telnet 远程连接到同一设备,并且我执行了相同的命令,它打印出连接到特定 PON 端口的所有设备.. 而通过 PHP telnet 的相同命令没有输出在先前日志中看到的完整 cmd 回复..

     OLT(config-interface-epon-0/0)# show ont info 1 all
         -----------------------------------------------------------------------------
        F/S  P  ONT MAC               Control   Run      Config   Match     Desc
                ID                    flag      state    state    state
       ----------------------------------------------------------------------------
        0/0  1  1   80:14:A8:A6:B8:E2 active    online   success  match     ONU-EXT-TA.
        0/0  1  2   80:14:A8:AA:45:80 active    online   success  match     ONU-EXT-NA.
        0/0  1  3   80:14:A8:98:12:F0 active    online   success  match     EURIZAM-MO.
        0/0  1  4   E0:67:B3:A6:56:73 active    online   success  match     DOUGLAS-CA.
        0/0  1  5   C4:70:0B:26:B1:58 active    online   success  match     AFONSO-LEM.
        0/0  1  6   80:14:A8:32:0B:60 active    online   failed   match     ALINE-FERR.
        0/0  1  7   80:14:A8:A6:BB:13 active    online   success  match     ONU-EXT-R..
        0/0  1  8   E0:E8:E6:4E:74:C0 active    online   success  match     MARCIA-CAR.
        0/0  1  9   80:14:A8:A6:54:1F active    online   success  match     ONU-EXT-JE. 
        0/0  1  10  E0:67:B3:7E:03:CA active    online   success  match     MARIA-AGUI.
        0/0  1  11  C4:70:0B:26:B2:E0 active    online   success  match     CRISTINO-A.
        0/0  1  12  44:55:B1:07:7E:11 active    online   success  match     IRACEMA-SI.
        0/0  1  13  80:14:A8:AA:6D:F0 active    online   success  match     ONU-EXT
        0/0  1  14  E0:E8:E6:4E:55:10 active    online   success  match     LENILDE-OL.
        0/0  1  15  E0:67:B3:B2:FC:8F active    online   success  match     VINICIUS-N.
        0/0  1  16  80:14:A8:A6:BB:17 active    online   success  match     ONU-EXT-RJ.
        0/0  1  17  80:14:A8:A6:C0:B8 active    online   success  match     ONU-EXT-MA.
        0/0  1  18  80:14:A8:A6:BB:05 active    online   success  match     ONU-EXT-DI.
        0/0  1  19  80:14:A8:A6:BB:18 active    online   success  match     ONU-FRANCI.
        0/0  1  20  E0:67:B3:88:5C:4A active    online   success  match     EDILEUZA-A.
        0/0  1  21  E0:67:B3:7E:0A:B1 active    online   success  match     ANDRESA-RO.
        0/0  1  22  C4:70:0B:25:F1:60 active    online   success  match     MARCUS-SOU.
        0/0  1  23  C4:70:0B:26:C2:78 active    online   success  match     RICARDO-PA. 
        0/0  1  24  E0:67:B3:A6:56:65 active    online   success  match     PHATRICIA-.
        0/0  1  25  00:6D:61:59:ED:B0 active    online   success  match     CARINA-BRI.
        0/0  1  26  80:14:A8:A6:BB:11 active    online   success  match     ONU-JONATA.
        0/0  1  27  C4:70:0B:26:07:C8 active    online   success  match     CHRISTIANO.
        0/0  1  28  80:14:A8:7F:8D:48 active    online   success  match     CARLA-SOUZ.
        0/0  1  29  E0:67:B3:88:30:13 active    online   success  match     VALERIA-FA.
       -----------------------------------------------------------------------------
        Total: 29, online 29
    

    【讨论】:

      【解决方案4】:

      这是设备发出的 $data_response 的完整输出

       show ont info 1 all 
       ----------------------------------------------------------------------------- 
       F/S P ONT MAC Control Run Config Match Desc ID flag state state state 
       ---------------------------------------------------------------------------- 
       0/0 1 1 80:14:A8:A6:B8:E2 active online success match ONU-EXT-TA. 
       0/0 1 2 80:14:A8:AA:45:80 active online success match ONU-EXT-NA. 
       0/0 1 3 80:14:A8:98:12:F0 active online success match EURIZAM-MO. 
       0/0 1 4 E0:67:B3:A6:56:73 active online success match DOUGLAS-CA. 
       0/0 1 5 C4:70:0B:26:B1:58 active online success match AFONSO-LEM.  
       0/0 1 6 80:14:A8:32:0B:60 active online failed match ALINE-FERR. 
       0/0 1 7 80:14:A8:A6:BB:13 active online success match ONU-EXT-R.. 
       0/0 1 8 E0:E8:E6:4E:74:C0 active online success match MARCIA-CAR. 
       0/0 1 9 80:14:A8:A6:54:1F active online success match ONU-EXT-JE. 
       0/0 1 10 E0:67:B3:7E:03:CA active online success match MARIA-AGUI 
       --More ( Press 'Q' to quit )--  . 
       0/0 1 11 C4:70:0B:26:B2:E0 active online success match CRISTINO-A. 
       0/0 1 12 44:55:B1:07:7E:11 active online success match IRACEMA-SI. 
       0/0 1 13 80:14:A8:AA:6D:F0 active online success match ONU-EXT 
       0/0 1 14 E0:E8:E6:4E:55:10 active online success match LENILDE-OL. 
       0/0 1 15 E0:67:B3:B2:FC:8F active online success match VINICIUS-N. 
       0/0 1 16 80:14:A8:A6:BB:17 active online success match ONU-EXT-RJ. 
       0/0 1 17 80:14:A8:A6:C0:B8 active online success match ONU-EXT-MA. 
       0/0 1 18 80:14:A8:A6:BB:05 active online success match ONU-EXT-DI. 
       0/0 1 19 80:14:A8:A6:BB:18 active online success match ONU-FRANCI. 
       0/0 1 20 E0:67:B3:88:5C:4A active online success match EDILEUZA-A. 
       0/0 1 21 E0:67:B3:7E:0A:B1 active online success match ANDRESA-RO. 
       0/0 1 22 C4:70:0B:25:F1:60 active online success match MARCUS-SOU. 
        --More ( Press 'Q' to quit )--
      

      这是输出日志上发生的事情..这就是为什么它似乎正在崩溃并且没有读出 PHP 上的完整输出 telnet cmd 响应.. 似乎 cmd 回复.. 它位于输入 ENTER 按钮或Q 退出 .. 并且因为 php 什么都不做.. 它只是崩溃并且不显示完整的输出 cmd 回复。

      【讨论】:

        【解决方案5】:

        Você conseguiu 解析器?? estou passando pelo mesmo 问题...

        【讨论】:

        猜你喜欢
        • 2014-10-19
        • 2021-04-20
        • 2022-06-15
        • 2019-03-23
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        相关资源
        最近更新 更多