473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PHP - Reading Live Text Log ?

Hi

My mail server outputs live logs like this:

T 20070325 123318 4605007c Connection from 111.111.111.111
T 20070325 123319 4605007c HELO testdomain.com
T 20070325 125817 46050083 EHLO S010600110962b82a.ed.roguedomain.net
T 20070325 125818 46050083 MAIL FROM: <gq*******@roguedomain.net>
E 20070325 125823 46050083 Host 68.150.140.71 blocked by
10-12 .sorbs.net C - Block - message tagged.
T 20070325 125824 46050083 RCPT TO: <di*****@newsite.com>
E 20070325 125824 46050083 RCPT from 68.150.140.71 - user
<di*****@newsite.comnot known.
T 20070325 125824 46050083 Connection closed with 68.150.140.71, 7
sec. elapsed.
T 20070325 123349 4605007c Connection closed with 111.111.111.111, 31
sec. elapsed.
T 20070325 125947 46050084 Connection from 83.28.27.71
T 20070325 125947 46050084 HELO drmac.cl
T 20070325 125947 46050084 MAIL FROM: <bi************@drmac.cl>
E 20070325 125948 46050084 Host 83.28.27.71 blocked by
zen.spamhaus.org sbl/xbl/pbl/zen 2 - message tagged.
T 20070325 125948 46050084 RCPT TO: <a1****************@newsite.com>
E 20070325 125948 46050084 RCPT from 83.28.27.71 - user
<a1****************@newsite.comnot known.
T 20070325 125949 46050084 Connection closed with 83.28.27.71, 2 sec.
elapsed.

I'm looking to try and read this file via PHP and colourise each line
based on the connection ID displaying the final result in a php
page...

eg:

T 20070325 123318 4605007c Connection from 111.111.111.111
The ID is 4605007c This appears on 3 lines. Note that the ID's are
not always grouped together.

Can this be done ?? The file is live and constantly changing...

Thanks

Mar 25 '07 #1
3 3756
You could do this with tail and fread. Here's one example of reading a
file that's continuously being updated:

http://groups.google.com/group/alt.c...b2d87c7d0cf1a8

On Mar 25, 8:07 am, jerryyang_...@yahoo.com wrote:
Hi

My mail server outputs live logs like this:

T 20070325 123318 4605007c Connection from 111.111.111.111
T 20070325 123319 4605007c HELO testdomain.com
T 20070325 125817 46050083 EHLO S010600110962b82a.ed.roguedomain.net
T 20070325 125818 46050083 MAIL FROM: <gqvnwu...@roguedomain.net>
E 20070325 125823 46050083 Host 68.150.140.71 blocked by
10-12 .sorbs.net C - Block - message tagged.
T 20070325 125824 46050083 RCPT TO: <dign...@newsite.com>
E 20070325 125824 46050083 RCPT from 68.150.140.71 - user
<dign...@newsite.comnot known.
T 20070325 125824 46050083 Connection closed with 68.150.140.71, 7
sec. elapsed.
T 20070325 123349 4605007c Connection closed with 111.111.111.111, 31
sec. elapsed.
T 20070325 125947 46050084 Connection from 83.28.27.71
T 20070325 125947 46050084 HELO drmac.cl
T 20070325 125947 46050084 MAIL FROM: <bitransmitt...@drmac.cl>
E 20070325 125948 46050084 Host 83.28.27.71 blocked by
zen.spamhaus.org sbl/xbl/pbl/zen 2 - message tagged.
T 20070325 125948 46050084 RCPT TO: <a1aaa1azzzz1zaa...@newsite.com>
E 20070325 125948 46050084 RCPT from 83.28.27.71 - user
<a1aaa1azzzz1zaa...@newsite.comnot known.
T 20070325 125949 46050084 Connection closed with 83.28.27.71, 2 sec.
elapsed.

I'm looking to try and read this file via PHP and colourise each line
based on the connection ID displaying the final result in a php
page...

eg:

T 20070325 123318 4605007c Connection from 111.111.111.111
The ID is 4605007c This appears on 3 lines. Note that the ID's are
not always grouped together.

Can this be done ?? The file is live and constantly changing...

Thanks

Mar 25 '07 #2
Thanks

But what about colourising the matching lines ?

Mar 25 '07 #3
On Mar 25, 10:34 am, jerryyang_...@yahoo.com wrote:
Thanks

But what about colourising the matching lines ?
Here's one (ugly) way to do it:

<?

error_reporting(E_ALL);

function printLog($text)
{
$lines = explode("\n", $text);
$buf = '';
$head = null;
$extra = array();

for ($i = 0; $i < count($lines); $i++) {
$parts = explode(' ', $lines[$i], 5);
if (count($parts) >= 4 && ($parts[0] == 'T' || $parts[1] == 'E'))
{
if (!is_null($head)) {
$buf .= printEntry($head, $extra);
}
$head = $parts;
$extra = array();
if ($i == count($lines) - 1) {
$buf .= printEntry($head, $extra);
}
} else {
$extra[] = $lines[$i];
}
}

echo $buf;
flush();
}

function printEntry($head, $extra)
{
static $rnd = null;
if (is_null($rnd)) {
$rnd = rand();
}

$hex = md5($head[3] . $rnd);
$fg = substr($hex, 0, 6);
$bg = sprintf("%02x%02x%02x",
((('0x' . substr($hex, 0, 2)) + 0x7f) % 0xff),
((('0x' . substr($hex, 2, 2)) + 0x7f) % 0xff),
((('0x' . substr($hex, 4, 2)) + 0x7f) % 0xff));

$msg = join(' ', $head) . "\n" . join("\n", $extra);
$str = "<span style=\"width: 100%; color: #{$fg}; background: #{$bg};
\">" .
htmlentities($msg, ENT_QUOTES) . "</span>";
$str = nl2br($str);

return $str;
}

$text = "T 20070325 123318 4605007c Connection from 111.111.111.111
T 20070325 123319 4605007c HELO testdomain.com
T 20070325 125817 46050083 EHLO S010600110962b82a.ed.roguedomain.net
T 20070325 125818 46050083 MAIL FROM: <gqvnwu...@roguedomain.net>
E 20070325 125823 46050083 Host 68.150.140.71 blocked by
10-12 .sorbs.net C - Block - message tagged.
T 20070325 125824 46050083 RCPT TO: <dign...@newsite.com>
E 20070325 125824 46050083 RCPT from 68.150.140.71 - user
<dign...@newsite.comnot known.
T 20070325 125824 46050083 Connection closed with 68.150.140.71, 7
sec. elapsed.
T 20070325 123349 4605007c Connection closed with 111.111.111.111, 31
sec. elapsed.
T 20070325 125947 46050084 Connection from 83.28.27.71
T 20070325 125947 46050084 HELO drmac.cl
T 20070325 125947 46050084 MAIL FROM: <bitransmitt...@drmac.cl>
E 20070325 125948 46050084 Host 83.28.27.71 blocked by
zen.spamhaus.org sbl/xbl/pbl/zen 2 - message tagged.
T 20070325 125948 46050084 RCPT TO: <a1aaa1azzzz1zaa...@newsite.com>
E 20070325 125948 46050084 RCPT from 83.28.27.71 - user
<a1aaa1azzzz1zaa...@newsite.comnot known.
T 20070325 125949 46050084 Connection closed with 83.28.27.71, 2 sec.
elapsed.
";

printLog($text);

?>

Mar 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
1
by: rodchar | last post by:
hey all, is there a quick way to read.all the contents of a text file, search for the text, and if it finds that text to read the entire line? thanks, rodchar
1
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
4
by: Vikas Kumar | last post by:
propertyDescription += "<br>" + lblpropertyDescription.Text; //here i am reading some text from text area i test wrting "p" in my text area it wrks fine but when i write <pin my text...
1
by: engggirl3000 | last post by:
Another question I have, what is the difference between reading a text file to a program and opening a text file in the program? A sample of one of the text files is formatted like this: 3 ...
2
by: thanawala27 | last post by:
Hi, I'm facign a strange problem in reading a text file. The contents of my text file is: A1;B1;C1;D1 A2;B2;C2;D2 A3;B3;C3;D3
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.