Jump to content

Best way to read a text file and extract information


cr-ispinternet

Recommended Posts

Hi All,

 

i have a log file of which i need to extract certain bits of information

the problem is the file never stays the same a sin line numbers and is always updated

every 1 minute with new data depensing on connections on the VPN system...

 

Yes its openvpn...

 

OpenVPN CLIENT LIST
Updated,Sat Jun 23 05:52:54 2007
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
192.168.10.10,milton,83.217.191.198:3236,Sat Jun 23 05:07:13 2007
GLOBAL STATS
Max bcast/mcast queue length,0
END

 

the log file looks like that and basically the part between Common Name and routing table will increase

it could be 2 lines one minute and 56 lines the next...

 

Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
ROUTING TABLE

 

i could have 200 entries in there at any one time what i need is a piece fo code

whcih will look 4 lines down and stop when it reaches ROUTING TABLE i just need it to prcoess everything

in between. i can then split the results returned and insert them in to a database for ease querying

when a clinet was last connected.

 

can any one point me in the right direction been toying using preg_match or something

but im not good with that string yet???

 

can any one help! i would so greatfull if some one could shed some light on it

 

Alan

<?php

$lines = file("file.txt");

foreach($lines as $linenumber => $line) {
if($linenumber != 0) {
  list($name, $address, $received, $sent, $date) = explode(",", $line);
  // ^^^ Build query using these variables ^^^
}
}

?>

 

Seems the easiest way to me :)

 

EDIT: Ooops.. didn't read the last part of your post :(

Try this (should only let you do something with the variables if the line they read from was valid):

 

<?php

$lines = file("file.txt");

foreach($lines as $linenumber => $line) {
if($linenumber != 0) {
  while(list($name, $address, $received, $sent, $date) = explode(",", $line)) {
   // ^^^ Build query using these variables ^^^
   echo "$name<br />$address<br />$received<br />$sent<br />$date<br /><br />---<br /><br />\n\n";
  }
}
}

?>

some errors here.....

 

[root@cougar openvpn]# ./test.php
./test.php: line 1: ?php: No such file or directory
./test.php: line 3: syntax error near unexpected token `('
./test.php: line 3: `$lines = file("/etc/openvpn/openvpn-status.log");'
[root@cougar openvpn]# 

 

any ideas tried looking for the obvious?

 

ok that was my fault for not adding

 

#! /usr/bin/php

 

however there are problems it didnt even finish just shed loads of this

 

PHP Notice:  Undefined offset:  4 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  4 in /etc/openvpn/test.php on line 9
PHP Notice:  Undefined offset:  3 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  3 in /etc/openvpn/test.php on line 9
PHP Notice:  Undefined offset:  2 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  2 in /etc/openvpn/test.php on line 9
Updated<br />Sat Jun 23 13:14:33 2007
<br /><br /><br /><br /><br />---<br /><br />

PHP Notice:  Undefined offset:  4 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  4 in /etc/openvpn/test.php on line 9
PHP Notice:  Undefined offset:  3 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  3 in /etc/openvpn/test.php on line 9
PHP Notice:  Undefined offset:  2 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  2 in /etc/openvpn/test.php on line 9
Updated<br />Sat Jun 23 13:14:33 2007
<br /><br /><br /><br /><br />---<br /><br />

PHP Notice:  Undefined offset:  4 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  4 in /etc/openvpn/test.php on line 9
PHP Notice:  Undefined offset:  3 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  3 in /etc/openvpn/test.php on line 9
PHP Notice:  Undefined offset:  2 in /etc/openvpn/test.php on line 9

Notice: Undefined offset:  2 in /etc/openvpn/test.php on line 9
Updated<br />Sat Jun 23 13:14:33 2007
<br /><br /><br /><br /><br />---<br /><br />

PHP Notice:  Undefined offset:  4 in /etc/openvpn/test.php on line 9

 

Alan

Alan

i think the problem i have here is that there is text in there before the parts that i need

i need to only split this line and nothing else from it...

 

OpenVPN CLIENT LIST
Updated,Sat Jun 23 05:52:54 2007
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
192.168.10.10,milton,83.217.191.198:3236,Sat Jun 23 05:07:13 2007
GLOBAL STATS
Max bcast/mcast queue length,0
END

 

all i need from that part is this..

 

milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007

 

but like i said it can be that one minute and then look like this....

 

milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007
milton,83.217.191.198:3236,18351,18630,Sat Jun 23 05:07:13 2007

 

im sure theres got to be some php code that matches patters

like preg_match or something

 

Alan

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.