Skip to content

Instantly share code, notes, and snippets.

@danvaly
Last active May 23, 2018 13:27
Show Gist options
  • Save danvaly/74f2d639a6e832e53c44c651e0fc6b5a to your computer and use it in GitHub Desktop.
Save danvaly/74f2d639a6e832e53c44c651e0fc6b5a to your computer and use it in GitHub Desktop.
Search and extract data from server log files
<?php
/**
* Tools to process end extract log data from Proftpd.log files
* Ex: php extract.php -f ftp_in.log -s PUT-o ftp_out.log
*/
$options = getopt("f:s:o:");
if (isset($options['f']) && isset($options['o']) && $options['s'] && strlen($options['s'])>0 ){
if (!file_exists($options['f'])){
die("Eroare: Nu am gasit fisierul pentru citire!");
}
$read = fopen($options['f'],"r");
$write = fopen($options['o'],'a');
if ($read && $write){
while (($line = fgets($read)) !== false) {
// process the line read.
if (isset($options['s']) ){
//detect string in line
if( stristr($line, $options['s']) ) {
fputs($write, $line);
}
}else{
fputs($write, $line);
}
}
fclose($read);
fclose($write);
}else{
die("Fisierele nu pot fi create.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment