1
Hello. I have a .txt (Pastebin) which contains the log of the connected clients on a server openvpn and the format of each of the lines is the below:
CLIENT_LIST orion-01889596000195 177.43.212.110:28763 172.16.191.145 872199 860412 Wed May 25 07:22:52 2016 1464171772 UNDEF
I’m using the function preg_match()
to store each result in a position of an array. I made the code in two ways.
First form
$log = "log.txt";
$handle = fopen($log, "r");
$inclients = false;
$cdata = array();
while (!feof($handle))
{
$line = fgets($handle, 4096);
if (substr($line, 0, 11) == "CLIENT_LIST")
{
$inclients = true;
}
if ($inclients)
{
preg_match("/\w+-\d{14}/", $line, $cdata);
preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\:[0-9]+/", $line, $cdata[1]);
preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/", $line, $cdata[2]);
preg_match("/\s+[0-9]+/", $line, $cdata[3]);
return var_dump($cdata[0], $cdata[1], $cdata[2], $cdata[3]);
}
}
Second form
if ($inclients)
{
preg_match("/\w+-\d{14}\s+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\:[0-9]+\s[0-9]+\. [0-9]+\.[0-9]+\.[0-9]+\s+[0-9]+\s+[0-9]+/", $line, $cdata);
var_dump($cdata);
}
Problems I found in the first form: the name (Orion-01889596000195) and the first ip (177.43.212.110:28763) managed to store in $cdata[0]
and $cdata[1]
but when storing the 2nd ip (172.16.191.145) in $cdata[3]
it returns to take the 1st ip but without the numbering after ":". And the other data I need to store is: 872199, 860412 and 1464171772 I couldn’t get to them.
Problems I found in the second form: I believe it was stored in $cdata[0]
the following information: array(1) { [0]=> string(70) "orion-01889596000195 177.43.212.110:28763 172.16.191.145 872199 860412" }
and so I don’t know how to get each information separately. Although I managed to capture the result 860412 I couldn’t move between Wed May 25 07:22:52 2016 until you get into 1464171772.
I appreciate anyone who can help me!
I think it would be better to separate by lines and then apply rules line by line. I’ll test it later and put an answer in if no one has solved it yet. What worries me is that you have a few lines with IP after the UNDEF, it breaks the pattern...
– Sergio
Those IP lines after the UNDEF would be like this?
CLIENT_LIST UNDEF 201.14.8.164:1134 42 54 Wed May 25 15:05:07 2016 1464199507 UNDEF
– Cesar Augusto
If yes, this UNDEF at the beginning is a bug of the Open VPN server itself when it generates the log... it cannot identify the key (for example:
orion-01889596000195
) then he registered as UNDEF– Cesar Augusto
Yes, exact. Ok, that means that all UNDEF must be empty strings for example?
– Sergio
Yes. My intention is to add this information to a database and then display it on a web page. As an example, log lines 3 and 4: ;
CLIENT_LIST databits-13031005000123 187.17.235.203:50515 172.16.136.217 459833 409771 Wed May 25 06:09:01 2016 1464167341 UNDEF
CLIENT_LIST UNDEF 201.14.8.164:1134 42 54 Wed May 25 15:05:07 2016 1464199507 UNDEF
In the third line would appeardatabits-13031005000123 etc etc etc
E in the fourth linestring-vazia etc etc etc
– Cesar Augusto
Okay, try this then: https://ideone.com/T9IIJy
– Sergio
@Sergio worked perfectly! I changed your first code and also managed to make it work perfectly. I appreciate immensely dear!
– Cesar Augusto
Great, I’ll add answer then to be complete and useful to others.
– Sergio