LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2010, 02:48 AM   #1
andrapgm03
Member
 
Registered: Nov 2010
Location: Indonesia, jakarta
Distribution: Ubuntu Desktop 10.10
Posts: 32

Rep: Reputation: 0
How to Parse the file correctly with Python


Hi, everyone this is my second thread, I'm currently curios with my python program which the basic goal is to parse the character in mytestfile;

let's see the code

Code:
f=open('/home/andrewraharjo/Desktop/snort.log','r')
j=f.read()
i=0
l=len(j)
l1=l
i=0
m=0

while(l1>=150):
        for k in range(149):
                print j[k+i],
              #I believe I've should modify this line
              #but how??
        l1=l1-150
        i=i+150
        m=m+1

l1=m*150

#print the rest of the character
for k in range(l-l1):
        print j[k+l1],
and the files snortlog:

Code:
TCP/IP message flooding directed to SIP proxy [Priority: 2] 11/05-09:54:30.831216 122.200.1.134:58319 -> 122.200.6.120:22
TCP/IP message flooding directed to SIP proxy [Priority: 2] 11/05-09:55:10.105621 122.200.6.120:60012 -> 91.189.92.172:80
TCP/IP message flooding directed to SIP proxy [Priority: 2] 11/05-09:57:38.982365 122.200.6.120:51490 -> 74.86.51.5:80
ICMP PING CyberKit 2.2 Windows [Priority: 3] 11/05-12:43:36.892309 122.200.54.42 -> 122.200.6.120
the goal is
python read until 160 character then print the 0-160 lines
python read untl >160 charater then print the 160-end lines

but my code printed out something like this
Code:
T C P / I P   m e s s a g e   f l o o d i n g   d i r e c t e d   t o   S I P   p r o x y   [ P r i o r i t y :   2 ]   1 1 / 0 5 - 0 9 : 5 4 : 3 0 . 8 3 1 2 1 6   1 2 2 . 2 0 0 . 1 . 1 3 4 : 5 8 3 1 9   - >   1 2 2 . 2 0 0 . 6 . 1 2 0 : 2 2 
T C P / I P   m e s s a g e   f l o o d i n g   d i r c t e d   t o   S I P   p r o x y   [ P r i o r i t y :   2 ]   1 1 / 0 5 - 0 9 : 5 5 : 1 0 . 1 0 5 6 2 1   1 2 2 . 2 0 0 . 6 . 1 2 0 : 6 0 0 1 2   - >   9 1 . 1 8 9 . 9 2 . 1 7 2 : 8 0  << here's the 160 1st character printed out

here's the 160-end character
T C P / I P   m e s s a g e   f l o o d i n g   d i r e c t e d   t o   S I P   p r o x y   [ P r i o r i t y   2 ]   1 1 / 0 5 - 0 9 : 5 7 : 3 8 . 9 8 2 3 6 5   1 2 2 . 2 0 0 . 6 . 1 2 0 : 5 1 4 9 0   - >   7 4 . 8 6 . 5 1 . 5 : 8 0 
I C M P   P I N G   C y b e r K i t   2 . 2   W i n d o w s   [ P r i o r i t y :   3 ]   1 1 / 0 5 - 1 2 : 4 3 : 3 6 . 8 9 2 3 0 9   1 2 2 . 2 0 0 . 5 4 . 4 2   - >   1 2 . 2 0 0 . 6 . 1 2 0
now my question is;
what's wrong with my code ? help me to figure it out cause I'm currently new with python...

here's my expectation with the output
Code:
TCP/IP message flooding directed to SIP proxy [Priority: 2] 11/05-09:54:30.831216 122.200.1.134:58319 -> 122.200.6.120:22
TCP/IP message flooding directed to SIP proxy [Priority: 2] 11/05-09:55:10.105621 122.200.6.120:60012 -> 91.189.92.172:80 
(160 chars)
print it

(look foward next 160 chars)
print it

TCP/IP message flooding directed to SIP proxy [Priority: 2] 11/05-09:57:38.982365 122.200.6.120:51490 -> 74.86.51.5:80
ICMP PING CyberKit 2.2 Windows [Priority: 3] 11/05-12:43:36.892309 122.200.54.42 -> 122.200.6.120
print the rest of the character file
anyway another question that came to me;
Is there other way to print new snort alert log with python ? So the program will print the last information (log) in the last line in a new file.(I know for using swatch for alert monitoring-but I still confused with using it) Any suggestion with bash programming or python programming are welcome..

Please help me to find out this solution

Cheers

Best Regards,
Andrew
 
Old 11-28-2010, 03:50 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The print statement adds the space (without the comma it will add a new line as well)

Maybe try:
Code:
print '%s' j[k+i],
or
Code:
import sys
sys.stdout.write(j[k+i])
 
Old 11-28-2010, 11:30 AM   #3
andrapgm03
Member
 
Registered: Nov 2010
Location: Indonesia, jakarta
Distribution: Ubuntu Desktop 10.10
Posts: 32

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by graemef View Post
The print statement adds the space (without the comma it will add a new line as well)

Maybe try:
Code:
print '%s' j[k+i],
or
Code:
import sys
sys.stdout.write(j[k+i])
success with code sys.stdout.write(j[k+i])
error with print '%s' j[k+i],

next question ;

how do I print the last line of the text calling another python program to sent it (such as AT command maybe) ?
 
Old 11-28-2010, 08:11 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Sorry but I don't understand your requirement. Maybe you can show what code you have tried and what output you are expecting to see?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse file from remote server to caculate count of string existence in that file saurabhmehan Linux - Newbie 2 08-30-2010 12:30 AM
Python script parse NMEA data from GlobalSat BU-353 buffdigger Linux - Newbie 1 04-07-2010 07:29 PM
why does strptime (c++) seem to not correctly parse date/time string???? lxvor Programming 2 02-08-2010 05:08 PM
Python Parse Code snowman81 Programming 3 01-24-2009 10:50 AM
Parse Python through use of .htaccess Hal Linux - General 1 01-24-2006 12:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration