Jump to content

Need help on writing a script (extract text from file)


sedvis

Recommended Posts

Hello. I have one programming problem. I have this log, from witch i have to read specific area of text:

webtopay.log

 

OK 123.456.7.89 [2012-03-15 09:09:59 -0400] v1.5: MIKRO to:"1398", from:"865458961", id:"13525948", sms:"MCLADM thing"

 

So i need the script to extract word "thing" from that log. Also that script has to check if there is new entries in the log, and extract text from the last one.

(Explaining in other words, that script should extract word AFTER MCLADM. Every time its a different word)

 

p.s. I need that script to be integrated here (this has to send command to server "/manuadd (text from log)" :

<?php
try{
     $HOST = "178.16.35.196"; //the ip of the bukkit server
     $password = "MCLietuva";
     //Can't touch this:
     $sock = socket_create(AF_INET, SOCK_STREAM, 0)
     or die("error: could not create socket\n");
     $succ = socket_connect($sock, $HOST, 4445) 
     or die("error: could not connect to host\n");
     //Authentification
     socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1)
     or die("error: failed to write to socket\n");
     //Begin custom code here.
     socket_write($sock, $command = "/Command/ExecuteConsoleCommandAndReturn-SimpleBroadCast:broadcast lol;", strlen($command) + 1) //Writing text/command we want to send to the server
     or die("error: failed to write to socket\n");
     sleep(2); // This is example code and here has to be that script i want to make.
     //while(($returnedString = socket_read($sock,50000))!= ""){
        $returnedString = socket_read($sock,50000,PHP_NORMAL_READ);
        print($returnedString)
     //}
     print("End of script");
     socket_close($sock);
}catch(Exception $e){
    echo $e->getMessage();
}
?>

 

 

I hope i made things clear and you will help me :)

Thanks :)

 

Link to comment
Share on other sites

I am willing to "help". This forum is for people wanting "help" with code they have written. If YOU have written the code above and someone provides a solution, YOU should be able to integrate it into that code. In any event, you need to be VERY explicit in these types of request.

 

Your first request looks very simple, but you don't state whether there are multiple lines that would contain "MCLADM thing" or only one. If there are multiple, what should be the behavior? I'm also not following these two comments:

 

Also that script has to check if there is new entries in the log, and extract text from the last one.

OK, so you only want the word for the LAST entry and only if that entry is "new"? What criteria determines if an entry is new?

 

Explaining in other words, that script should extract word AFTER MCLADM. Every time its a different word

The last statement said to only retrieve the last entry (if it is new), but this one states to get the value "every time it is different". Every time it is different from what? The previous entry in the log, from the last time the log was processed, or what?

Link to comment
Share on other sites

OK, I will explain the whole thing. I have minecraft server and I use WebToPay service. When someone sends sms he types in "MCLADM (and his name here in my minecraft server). Then that message is send to my website, and is logged there in webtopay.log . The message gets logged like this:

 

OK 178.16.35.196 [2012-03-15 09:09:59 -0400] v1.5: MIKRO to:"1398", from:"865473201", id:"13525948", sms:"MCLADM sedvis"

 

when someone else sends message the log will look like:

 

 

OK 178.16.35.196 [2012-03-15 09:09:59 -0400] v1.5: MIKRO to:"1398", from:"865473201", id:"13525948", sms:"MCLADM sedvis"

OK 178.16.35.196 [2012-03-16 10:23:43 -0400] v1.5: MIKRO to:"1398", from:"865445896", id:"13526983", sms:"MCLADM petras"

 

and so on.

 

So this script has to check if any new messages came and if new sms came, it has to send "/manuadd (username of the sms sender (petras))" to my minecraft server, and somehow it has to be integrated with this...:

<?php
try{
     $HOST = "178.16.35.196"; //the ip of the bukkit server
     $password = "MCLietuva";
     //Can't touch this:
     $sock = socket_create(AF_INET, SOCK_STREAM, 0)
     or die("error: could not create socket\n");
     $succ = socket_connect($sock, $HOST, 4445) 
     or die("error: could not connect to host\n");
     //Authentification
     socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1)
     or die("error: failed to write to socket\n");
     //Begin custom code here.
     socket_write($sock, $command = "/Command/ExecuteConsoleCommandAndReturn-SimpleBroadCast:broadcast lol;", strlen($command) + 1) //Writing text/command we want to send to the server
     or die("error: failed to write to socket\n");
     sleep(2); // This is example code and here has to be that script i want to make.
     //while(($returnedString = socket_read($sock,50000))!= ""){
        $returnedString = socket_read($sock,50000,PHP_NORMAL_READ);
        print($returnedString)
     //}
     print("End of script");
     socket_close($sock);
}catch(Exception $e){
    echo $e->getMessage();
}
?>

 

 

and sorry for my english, its not my native language :)

 

Link to comment
Share on other sites

OK, so let me rephrase what I understand. You want all entries in the log with the "MCLADM XXXX" to be processed. But, you only want the ones that were not processed previously.

 

OK, but how is the script supposed to determine WHICH entries are new? You might be able to use the timestamp in the log, but I assume that timestamp is determined by the Minecraft server and not the Web server. If they are not exactly synced you could end up either missing some entries or duplicating others. I would also think that emptying the log is not an option. If the PHP script was trying to save the emptied copy while the game server is trying to make a new entry there would be problems. I think the best option in this situation would be to process the log file and then store the timestamp of the last record processed.

 

Also, are the record in chronological or reverse chronological order? I.e. are the new records at the top or the bottom. I would assume they are at the bottom and that creates another problem. As the log file grows, the time to process the log file will increase.

 

Lastly, if there are two entries in the log file for the same user (both new since the last time the file was processed) do you want them both processed or only one. Plus, what if a log entry for a user was processed in a previous script run and a new one was added? If you don't want to process new log entries for users that were processed previously, you will need to store a record of the ones you have previously processed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.