cr-ispinternet Posted June 23, 2007 Share Posted June 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/ Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/#findComment-280912 Share on other sites More sharing options...
cr-ispinternet Posted June 23, 2007 Author Share Posted June 23, 2007 lol was that in respect to that fact the lines change or something else any one any ideas... alan Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/#findComment-280933 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 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"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/#findComment-280938 Share on other sites More sharing options...
cr-ispinternet Posted June 23, 2007 Author Share Posted June 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/#findComment-280940 Share on other sites More sharing options...
cr-ispinternet Posted June 23, 2007 Author Share Posted June 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/#findComment-280948 Share on other sites More sharing options...
cr-ispinternet Posted June 23, 2007 Author Share Posted June 23, 2007 Solve.... By a stroke of look and 80 minutes a later i have this guy to thank http://www.der-moloch.de/~fab/getphp.php?file=ovpn does everything i need and more :-) Alan Quote Link to comment https://forums.phpfreaks.com/topic/56854-best-way-to-read-a-text-file-and-extract-information/#findComment-280962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.