|
Posted by Lawrence Krubner on July 26, 2008, 7:49 pm
Please log in for more thread options
I'm suppose to track FTP uploads to the server. I've got a cron script,
written in PHP, that runs every 5 minutes and which does the command
lsof | grep vsftpd
This gives me a big file that has data like this in it:
vsftpd 12316 lawrence cwd DIR 9,2 4096
20938763 /data/ftp/lawrence_krubner
vsftpd 12316 lawrence rtd DIR 9,2 4096
20938763 /data/ftp/lawrence_krubner
Then I needed to this data broken up and stored in the database, each
field. I looped through this stuff, taking on line at a time, and each
line I would hit with explode, like this:
$arrayOfFields = explode(" ", $stringOfOneLine);
Unfortunetly, this gives me an array that has lots of blank rows because
every white space becomes a row in the array and, as you can see, there
is a lot of white space.
So I loop through the array and take out all the empty rows, but damn,
this is tedious and clumsy. Is there a better way?
I also struggle with the opposite problem:
Bee/878037001068/878037001068_01
The fields run together. Can anyone think of a way to separate them?
|
|
Posted by Jerry Stuckle on July 26, 2008, 10:10 pm
Please log in for more thread options
Lawrence Krubner wrote:
>
> I'm suppose to track FTP uploads to the server. I've got a cron script,
> written in PHP, that runs every 5 minutes and which does the command
>
> lsof | grep vsftpd
>
> This gives me a big file that has data like this in it:
>
>
> vsftpd 12316 lawrence cwd DIR 9,2 4096
> 20938763 /data/ftp/lawrence_krubner
>
> vsftpd 12316 lawrence rtd DIR 9,2 4096
> 20938763 /data/ftp/lawrence_krubner
>
>
> Then I needed to this data broken up and stored in the database, each
> field. I looped through this stuff, taking on line at a time, and each
> line I would hit with explode, like this:
>
> $arrayOfFields = explode(" ", $stringOfOneLine);
>
>
> Unfortunetly, this gives me an array that has lots of blank rows because
> every white space becomes a row in the array and, as you can see, there
> is a lot of white space.
>
> So I loop through the array and take out all the empty rows, but damn,
> this is tedious and clumsy. Is there a better way?
>
> I also struggle with the opposite problem:
>
> Bee/878037001068/878037001068_01
>
> The fields run together. Can anyone think of a way to separate them?
>
>
>
Sounds like you're approaching things the wrong way. If you need to
track ftp uploads, you should be hooking into the ftpd daemon. You
won't be able to do it in PHP, though.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
|
|
Posted by Dale on July 27, 2008, 12:38 am
Please log in for more thread options
>
> I'm suppose to track FTP uploads to the server. I've got a cron script,
> written in PHP, that runs every 5 minutes and which does the command
>
> lsof | grep vsftpd
>
> This gives me a big file that has data like this in it:
>
>
> vsftpd 12316 lawrence cwd DIR 9,2 4096
> 20938763 /data/ftp/lawrence_krubner
>
> vsftpd 12316 lawrence rtd DIR 9,2 4096
> 20938763 /data/ftp/lawrence_krubner
i'm going to stick to answering your question rather than telling you it
can't be done in php...like some other morons here. :)
> I also struggle with the opposite problem:
>
> Bee/878037001068/878037001068_01
not sure what you means by that one.
anyway, i couldn't tell if the output you pasted here is a single line entry
starting with 'vsftpd' and ending with 'lawrence_krubner' (case 'A') or, if
there were two related rows where a new row of data started after '4096' and
'lawrence_krubner' (case 'B'). there was a soft-return (a \n) after 4096
when i copied it into my test script...so, i assume the latter. if not, let
me know. either way, this should work...
=============
<?
$contents = "vsftpd 12316 lawrence rtd DIR 9,2
4096
20938763 /data/ftp/lawrence_krubner"; // case 'B'
$patterns = array(
'/\r?\n/' , // force unix \n
'/^/' , // beginning of line
'/$/' , // end of line
'/ +/' // spaces
);
$replacements = array(
"\"\n\"" , // unix nl w/ encapsulation
'"' , // start line with quote
'"' , // end line with quote
'", "' // encapsulate fields
);
$contents = preg_replace($patterns, $replacements, $contents);
?>
=============
this will turn your output file contents into a csv format. if case 'A', you
can use php's built-in cvs parsing functions to help you out or look at the
example below. if case 'B', you'll have to keep track of what row (even or
odd) you are processing to know what fields to expect from the row and what
positions the fields will be in.
i digress. at this point, you've gotten a format you can work with and
forced a unix \n to separate your rows. manually break it out into rows and
from there, into fields:
=============
<?
$contents = explode("\n", $contents); // file into array of rows
foreach ($contents as $row)
{
$row = substr($row, 1); // strip initial quote
$row = substr($row, 0, -1); // strip ending guote
$row = explode('", "', $row); // row into array of fields
foreach ($row as $index => $column)
{
echo '<pre style="font:10px;">COLUMN ' . $index . ' = ' . $column .
'</pre>';
}
}
?>
=============
if case 'A', this sample will output:
COLUMN 0 = vsftpd
COLUMN 1 = 12316
COLUMN 2 = lawrence
COLUMN 3 = rtd
COLUMN 4 = DIR
COLUMN 5 = 9,2
COLUMN 6 = 4096
COLUMN 7 = 20938763
COLUMN 8 = /data/ftp/lawrence_krubner
if case 'B', this sample will output:
COLUMN 0 = vsftpd
COLUMN 1 = 12316
COLUMN 2 = lawrence
COLUMN 3 = rtd
COLUMN 4 = DIR
COLUMN 5 = 9,2
COLUMN 6 = 4096
COLUMN 0 = 20938763
COLUMN 1 = /data/ftp/lawrence_krubner
anyway, like i said, i'm not making egotistical assumptions about what
you're trying to do. i'm merely answering the question you asked. i hope it
helps.
cheers
|
|
Posted by Michael Fesser on July 27, 2008, 7:05 am
Please log in for more thread options .oO(Lawrence Krubner)
>I'm suppose to track FTP uploads to the server. I've got a cron script,
>written in PHP, that runs every 5 minutes and which does the command
>
>lsof | grep vsftpd
>
>This gives me a big file that has data like this in it:
>
>
>vsftpd 12316 lawrence cwd DIR 9,2 4096
>20938763 /data/ftp/lawrence_krubner
>
>vsftpd 12316 lawrence rtd DIR 9,2 4096
>20938763 /data/ftp/lawrence_krubner
Just curious: What does this tell you about FTP uploads? Additionally
lsof might output some more stuff than you actually want, so it might
make sense to pass some explicit command line options to it.
>Then I needed to this data broken up and stored in the database, each
>field. I looped through this stuff, taking on line at a time, and each
>line I would hit with explode, like this:
>
>$arrayOfFields = explode(" ", $stringOfOneLine);
>
>
>Unfortunetly, this gives me an array that has lots of blank rows because
>every white space becomes a row in the array and, as you can see, there
>is a lot of white space.
One option to remove empty array entries is array_filter(), but in this
case preg_split() would be better:
$arrayOfFields = preg_split('/ +/', $stringOfOneLine);
>So I loop through the array and take out all the empty rows, but damn,
>this is tedious and clumsy. Is there a better way?
>
>I also struggle with the opposite problem:
>
>Bee/878037001068/878037001068_01
>
>The fields run together. Can anyone think of a way to separate them?
Where does this come from?
Micha
|
|
Posted by C. (http://symcbean.blogspot.c on July 27, 2008, 7:18 am
Please log in for more thread options > I'm suppose to track FTP uploads to the server. I've got a cron script,
> written in PHP, that runs every 5 minutes and which does the command
>
> lsof | grep vsftpd
>
What do you think you're measuring?
(because its not file uploads)
C.
|
| Similar Threads | Posted | | how can white space enter a text file? | June 5, 2008, 6:02 pm |
| white space in select option | December 27, 2005, 4:57 pm |
| preserving white space with DOMDocument / loadHTML | May 16, 2008, 8:46 pm |
| How do I hide part of an image with white space? | June 13, 2008, 4:40 pm |
| Help array is slicing up my sting from a DB at the white spaces between the word | January 27, 2005, 10:05 am |
| regular expression space and 2 byte space | August 25, 2006, 5:25 pm |
| How to have line breaks in this text | November 29, 2006, 7:42 pm |
| read line from text file | December 28, 2004, 7:50 pm |
| Processing data from a text area, one line at a time | September 13, 2007, 5:02 pm |
| read a value written in the first tab of the last line of a text file where values are separated by "\t"? | November 19, 2004, 4:40 pm |
|