parsing log files not encapsulated with <pre>

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

parsing log files not encapsulated with <pre>

Postby TerraFrost » Wed Jan 18, 2012 7:23 am

Code: Select all
<?php

$log = '    -> NET_SFTP_INIT (0.0009s) 00000000 00:00:00:03 .... <-
    NET_SFTP_VERSION (0.0447s) 00000000 00:00:00:03:00:00:00:18:70:6f:73:69:78:2d:72:65
    ........posix-re 00000010 6e:61:6d:65:40:6f:70:65:6e:73:73:68:2e:63:6f:6d
    name@openssh.com 00000020 00:00:00:01:31:00:00:00:13:73:74:61:74:76:66:73
    ....1....statvfs 00000030 40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:01
    @openssh.com.... 00000040 32:00:00:00:14:66:73:74:61:74:76:66:73:40:6f:70 2
    ....fstatvfs@op 00000050 65:6e:73:73:68:2e:63:6f:6d:00:00:00:01:32 enssh.com....

    2 -> NET_SFTP_REALPATH (0.0006s) 00000000 00:00:00:01:2e ..... <-
    NET_SFTP_NAME (0.0545s) 00000000 00:00:00:01:00:00:00:05:2f:72:6f:6f:74:00:00:00
    ......../root... 00000010 05:2f:72:6f:6f:74:00:00:00:00


    ./root.... -> NET_SFTP_REALPATH (0.0007s) 00000000 00:00:00:0f:2f:72:6f:6f:74:2f:6d:63:2f:77:6f:72
    ..../root/mc/wor 00000010 6c:64:2f ld/

    <- NET_SFTP_NAME (0.0308s) 00000000 00:00:00:01:00:00:00:0e:2f:72:6f:6f:74:2f:6d:63
    ......../root/mc 00000010 2f:77:6f:72:6c:64:00:00:00:0e:2f:72:6f:6f:74:2f
    /world..../root/ 00000020 6d:63:2f:77:6f:72:6c:64:00:00:00:00 mc/world....

    -> NET_SFTP_REMOVE (0.0007s) 00000000 00:00:00:0f:2f:72:6f:6f:74:2f:6d:63:2f:77:6f:72
    ..../root/mc/wor 00000010 6c:64:2f ld/
    <- NET_SFTP_STATUS (0.0285s) 00000000 00:00:00:04:00:00:00:07:46:61:69:6c:75:72:65:00
    ........Failure. 00000010 00:00:00 ...';

$log = preg_replace('#^    #m', '', $log);
$log = str_replace("\r\n", ' ', $log);
$log = preg_split('#((?:<-|->) NET_(?:SSH2|SFTP)_[^ ]+ \([^)]+\) )#', $log, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < count($log); $i+=2)
{
   $log[$i] = preg_split('#(\d{8} )#', $log[$i], -1, PREG_SPLIT_DELIM_CAPTURE);
   for ($j = 0; $j < count($log[$i]); $j+=2)
   {
      $log[$i][$j] = preg_split('# #', $log[$i][$j], -1, PREG_SPLIT_DELIM_CAPTURE);
   }
}

for ($i = 1; $i < count($log); $i++)
{
   echo $log[$i] . "\r\n";
   $i++;
   for ($j = 1; $j < count($log[$i]); $j++)
   {
      echo $log[$i][$j] . ' ';
      $j++;
      echo str_pad($log[$i][$j][0], 65 - 16, ' ');
      echo $log[$i][$j][1];
      echo "\r\n";
   }
   echo "\r\n";
}

It's not perfect but it does the trick 90% of the time. Converts stuff like this:

Code: Select all
1 -> NET_SFTP_INIT (0.0009s) 00000000 00:00:00:03 .... <-
NET_SFTP_VERSION (0.0447s) 00000000 00:00:00:03:00:00:00:18:70:6f:73:69:78:2d:72:65
........posix-re 00000010 6e:61:6d:65:40:6f:70:65:6e:73:73:68:2e:63:6f:6d
name@openssh.com 00000020 00:00:00:01:31:00:00:00:13:73:74:61:74:76:66:73
....1....statvfs 00000030 40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:01
@openssh.com.... 00000040 32:00:00:00:14:66:73:74:61:74:76:66:73:40:6f:70 2
....fstatvfs@op 00000050 65:6e:73:73:68:2e:63:6f:6d:00:00:00:01:32 enssh.com....

2 -> NET_SFTP_REALPATH (0.0006s) 00000000 00:00:00:01:2e ..... <-
NET_SFTP_NAME (0.0545s) 00000000 00:00:00:01:00:00:00:05:2f:72:6f:6f:74:00:00:00
......../root... 00000010 05:2f:72:6f:6f:74:00:00:00:00


./root.... -> NET_SFTP_REALPATH (0.0007s) 00000000 00:00:00:0f:2f:72:6f:6f:74:2f:6d:63:2f:77:6f:72
..../root/mc/wor 00000010 6c:64:2f ld/

<- NET_SFTP_NAME (0.0308s) 00000000 00:00:00:01:00:00:00:0e:2f:72:6f:6f:74:2f:6d:63
......../root/mc 00000010 2f:77:6f:72:6c:64:00:00:00:0e:2f:72:6f:6f:74:2f
/world..../root/ 00000020 6d:63:2f:77:6f:72:6c:64:00:00:00:00 mc/world....

-> NET_SFTP_REMOVE (0.0007s) 00000000 00:00:00:0f:2f:72:6f:6f:74:2f:6d:63:2f:77:6f:72
..../root/mc/wor 00000010 6c:64:2f ld/
<- NET_SFTP_STATUS (0.0285s) 00000000 00:00:00:04:00:00:00:07:46:61:69:6c:75:72:65:00
........Failure. 00000010 00:00:00 ...

...to this...

Code: Select all
-> NET_SFTP_INIT (0.0009s)
00000000  00:00:00:03                                      ....

<- NET_SFTP_VERSION (0.0447s)
00000000  00:00:00:03:00:00:00:18:70:6f:73:69:78:2d:72:65  ........posix-re
00000010  6e:61:6d:65:40:6f:70:65:6e:73:73:68:2e:63:6f:6d  name@openssh.com
00000020  00:00:00:01:31:00:00:00:13:73:74:61:74:76:66:73  ....1....statvfs
00000030  40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:01  @openssh.com....
00000040  32:00:00:00:14:66:73:74:61:74:76:66:73:40:6f:70  2
00000050  65:6e:73:73:68:2e:63:6f:6d:00:00:00:01:32        enssh.com....

-> NET_SFTP_REALPATH (0.0006s)
00000000  00:00:00:01:2e                                   .....

<- NET_SFTP_NAME (0.0545s)
00000000  00:00:00:01:00:00:00:05:2f:72:6f:6f:74:00:00:00  ......../root...
00000010  05:2f:72:6f:6f:74:00:00:00:00

-> NET_SFTP_REALPATH (0.0007s)
00000000  00:00:00:0f:2f:72:6f:6f:74:2f:6d:63:2f:77:6f:72  ..../root/mc/wor
00000010  6c:64:2f                                         ld/

<- NET_SFTP_NAME (0.0308s)
00000000  00:00:00:01:00:00:00:0e:2f:72:6f:6f:74:2f:6d:63  ......../root/mc
00000010  2f:77:6f:72:6c:64:00:00:00:0e:2f:72:6f:6f:74:2f  /world..../root/
00000020  6d:63:2f:77:6f:72:6c:64:00:00:00:00              mc/world....

-> NET_SFTP_REMOVE (0.0007s)
00000000  00:00:00:0f:2f:72:6f:6f:74:2f:6d:63:2f:77:6f:72  ..../root/mc/wor
00000010  6c:64:2f                                         ld/

<- NET_SFTP_STATUS (0.0285s)
00000000  00:00:00:04:00:00:00:07:46:61:69:6c:75:72:65:00  ........Failure.
00000010  00:00:00                                         ...
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 1 guest