Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

How can i parse this string

i have a string of

10/19/2004 12:08:03 AM - (   757) 9fe2 LOGIN "jsangel@dragonsworkshop.com" "XXXX"

id like to parse it so i get the values of

check for LOGIN in multible lines of a text file

when line is found return values of

Login_Date = "10/19/2004"
Login_Time = "12:08:03 AM "
Login_Email = "jsangel@dragonsworkshop.com"
Login_PW = "XXXX"

i also have a line before all this that is
10/19/2004 12:08:02 AM - Requested IMAP connection from 24.166.229.22 [24.166.229.22], ID=757

id like the
Login_Protocal = "IMAP"
Login_IP = "24.166.229.22 "
Login_ID = "757"

one thing that i think may help is the id is in both lines so it can be assoicated with it maybe..

i do not understand how to parse this for the info to be in strings..
my other problem is there is alot of them with diffrent users.

id like it to break down the logons by time user loged on with what ip..sorted by users email

i assume i need to do arrays but i ahve never done this before so i have no clue how to do this

i could of done this with the mid fuction that vb6 has (was unsure if vb.net has it or not) but i figured vb.net has a better way..maybe faster too


please post code examples

thx
Johnny
aka Pern



Avatar of Mikal613
Mikal613
Flag of United States of America image

id split it split(yourstring," ")

and then
(1)                   (2)     (3)                         (7)                 (8)                                  (9)
10/19/2004 12:08:03 AM - (   757) 9fe2 LOGIN "jsangel@dragonsworkshop.com" "XXXX"
Avatar of Johnny

ASKER

ok that does not answer my full question tho..

problems are

ID=757
how would i return just
Login_ID = "757"

and how would i assign an array to it for multible users and logons

i tryed split  with a space as chop.. but i got lost in tracking it some...
i liked that way the best outa all the things ive tried.. but again i got lost how to group them
so thats why i asked my question i couldnt figure out how to parse all the info corectly
split that with a "="

or replace ("ID=","")
Avatar of Johnny

ASKER

sample logfile:

10/19/2004 12:08:02 AM - Requested IMAP connection from 24.166.229.22 [24.166.229.22], ID=757
10/19/2004 12:08:03 AM - (   757) 9fe2 LOGIN "jsangel@dragonsworkshop.com" "xxx"
10/19/2004 12:08:03 AM - (   757) 9fe2 OK LOGIN successful
10/19/2004 12:08:05 AM - IMAP connection with 24.166.229.22 [24.166.229.22] ended. ID=757

10/19/2004 12:08:05 AM - Requested IMAP connection from 24.166.229.22 [24.166.229.22], ID=758
10/19/2004 12:08:05 AM - (   758) jpsy LOGIN "jsangel@dragonsworkshop.com" "xxx"
10/19/2004 12:08:05 AM - (   758) jpsy OK LOGIN successful
10/19/2004 12:08:09 AM - IMAP connection with 24.166.229.22 [24.166.229.22] ended. ID=758

code i have so far
        Dim LogFile As Long
        LogFile = FreeFile()
        Dim str As String
        'Dim strng2 As Collection
        Dim txtSearchString As String
        Dim x As Integer

        txtSearchString = "LOGIN "
        FileOpen(LogFile, "../../logs/2004-10-18.txt", OpenMode.Input, OpenAccess.Read)
        While Not EOF(LogFile)
            Str = LineInput(LogFile)
            'strng2 = Mid(str, 8, 80)
            'strng2 = Split(str, " ")
            x = InStr(1, str, txtSearchString, CompareMethod.Text)
            If x > 0 Then
                ListBox1.Items.Add(str)
            End If
        End While
        FileClose()

--------------------------
its finding the lines
10/19/2004 12:08:03 AM - (   757) 9fe2 LOGIN "jsangel@dragonsworkshop.com" "xxx"
10/19/2004 12:08:03 AM - (   757) 9fe2 OK LOGIN successful
i only want line
10/19/2004 12:08:03 AM - (   757) 9fe2 LOGIN "jsangel@dragonsworkshop.com" "xxx"

i noticed that the id of 757 and 758 are in patterns is there a way to know if the line is id 757 and then look for

find and asign
Login_Protocal = "IMAP"
Login_IP = "24.166.229.22 "
Login_ID = "757"

then look in line that contain 757 for
10/19/2004 12:08:03 AM - (   757) 9fe2 LOGIN "jsangel@dragonsworkshop.com" "xxx"
breaking it apart to
Login_Date = "10/19/2004"
Login_Time = "12:08:03 AM "
Login_Email = "jsangel@dragonsworkshop.com"
Login_PW = "XXXX"

telling me i can use split to do this does not help me ive been plugging away at this for a bit now and i keep gaving problems saying there has to be an easer way

i want to display in a listbox
**---------------------------------------------------**
Login_ID = "757"
Login_Protocal = "IMAP"
Login_IP = "24.166.229.22 "
Login_Date_time = "10/19/2004 - 12:08:03 AM "
Login_Email = "jsangel@dragonsworkshop.com"
Login_PW = "XXXX"
LogOff_Date_Time = "10/19/2004 - 12:08:05 AM "
**---------------------------------------------------**

then repeats for 758's info etc till end of logfile

in looking for this type of thing
right now im having a problem with keeping track of how to do this and im getting false things with lines like the two login's

im not looking for a single line like split..im looking for the logic and the code to acomplish this..its driving me nuts
im still learning vb.net from vb6 (i most likely can do this in vb6 but vb.net does things totaly diffrent

thanks for your help in advance
Johnny
aka Pern
the file has to be consisted
try an access db or an excel spreadsheet 4 better results
Avatar of arif_eqbal
arif_eqbal

See you have a clear Pattern here
There are Four lines For each Request

Line 1: It defines the Request It contains the protocol, IP, Login ID and date & Time of request, This line has a Key Word REQUEST

Line 2: It defines the Login parameters like Mail ID, Password etc.

Line 3: Contains whether Request was Successful (Keyword SUCCESSFUL)

Now you can Look for these three lines only and get your values

Split the First Line with Space, So you get an Array of Date, Time, Protocol, IP, and ID
All these parameters will be directly available as array members but ID will be ID=757 So use
Your_ID_Variable=Arr(i).Substring(Arr(i).IndexOf("=") - 1)

Splitting second line with Space will give all values as array Members

The last Array member after Split of the Thhird line will give you whether connection was successful or not.

If you want I can try it out and give you a sample code

Avatar of Johnny

ASKER

arif_eqbal,

can you wait till friday ill give you a actual log file to use..as it has more then the sample code above... to see if we can parse out the info corectly.

i have to work today and will not be back till late..

that sounds about right tho.. i would love a code example...

thx
Johnny
aka Pern
Avatar of Johnny

ASKER

ok heres an actual log snip...

http://dragonsworkshop.com/email_log/sample_log.txt

im trying to pull the above info from it...

thanks
Johnny
aka Pern
I have downloaded your file
I'll develop a full working code for you
But sorry to say that I am a bit today
Mind if I get back to you tomorrow!!!!!

Avatar of Johnny

ASKER

im in no rush!!
Avatar of Johnny

ASKER

but i would like to know the solution...i asume you where trying to say u are sick(if so hope you fell better)

awaiting for the answer....

thx
ASKER CERTIFIED SOLUTION
Avatar of arif_eqbal
arif_eqbal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

reviewing code...will advise...
Avatar of Johnny

ASKER

how do i populate a listbox for now with the info..

the below code seams not to work..i have errors...and if i redim the vars wont that erase them??


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ParseFile()
        ListBox1.Items.Add("Login_ID: " & Login_ID) 'Login_ID
        ListBox1.Items.Add("Login_Email: " & Login_Email) 'Login_Email
        ListBox1.Items.Add("Login_Protocal: " & Login_Protocal) 'Login_Protocal
        ListBox1.Items.Add("Login_IP: " & Login_IP) 'Login_IP
        ListBox1.Items.Add("Login_Date_Time: " & Login_Date_Time) 'Login_Date_Time
        ListBox1.Items.Add("Logoff_Date_Time: " & Logoff_Date_Time) 'Logoff_Date_Time


    End Sub

and thx..
Avatar of Johnny

ASKER

i got it see code below THIS IS PERFECT!!!

it does parse the smtp line too...
im going to test on a full log and see what happens but i can now manipulate the output with the populated info..thats what i wanted...

im trying to make a stats of each user and send them emails each week on there useage... (just playing around, see if i can get it all to work)


thanks alot this is great
Johnny
aka Pern
Avatar of Johnny

ASKER

i found that pop3 and smtp do not have logoff details and pop3 does nto have email...you mentioned somethiong about only imap works..is this what u ment?? im trying to figure out how to get pop3 and smtp to finish the info...if finds them but does nto complete info... also i would need password as it does change (i xxx um so other could not hack it as its a live server)

i would be happy to email u a full log to see all this... ill post my email so u dont get spamed

perndrgn@yahoo.com (please put a few * in subject cuz its my spam (dont care) email addy)

i gota find the rfc for email protocal so i can parse this log right ..as i said im making a stats thing to send the users there stats...ive had a few ask for if they could get info like this...

this is great and thx for the extra help...
Avatar of Johnny

ASKER

ive noticed u parsed the string of
10/24/2004 12:11:21 PM - (   238) ZZZZ OK LOGOUT completed


and a btter one would have been
10/24/2004 12:11:21 PM - {   238} END IMAP
or
10/24/2004 12:11:34 PM - {   240} END POP3
or
10/24/2004 11:34:06 AM - {   221} END SMTP

this way we get the end time on all areas

i also noticed the log grabs the doamin name for ip
10/24/2004 11:47:37 AM - (   223) EHLO mx1.gamblersbank.com
is there a way to grab this info too..

if you can show me how to do this i should be able to get other info myself...
Hopefully

thx again for helping me out
Johnny
aka Pern

ps i tryed to get the end string to parse but im lost(botched it up)





Avatar of Johnny

ASKER

smtp is the only one to use ehlo ... for domain name

and heres a sample pop3 snip

10/24/2004 10:02:20 AM - Requested POP3 connection from 62.127.12.98 [62.127.12.98], ID=200
10/24/2004 10:02:20 AM - (   200) +OK ArGoSoft Mail Server Pro for WinNT/2000/XP, Version 1.8 (1.8.6.5)
10/24/2004 10:02:20 AM - (   200) CAPA
10/24/2004 10:02:20 AM - (   200) -ERR Unknown command
10/24/2004 10:02:20 AM - (   200) USER postmaster@laymansworld.com
10/24/2004 10:02:20 AM - (   200) +OK Password required for postmaster@laymansworld.com
10/24/2004 10:02:21 AM - (   200) PASS XXXX
10/24/2004 10:02:21 AM - (   200) +OK Mailbox locked and ready
10/24/2004 10:02:21 AM - (   200) CAPA
10/24/2004 10:02:21 AM - (   200) -ERR Unknown command
10/24/2004 10:02:21 AM - (   200) UIDL 1
10/24/2004 10:02:21 AM - (   200) -ERR Message number 1 does not exist
10/24/2004 10:02:21 AM - (   200) LIST
10/24/2004 10:02:21 AM - (   200) +OK
10/24/2004 10:02:22 AM - (   200) .
10/24/2004 10:02:22 AM - (   200) QUIT
10/24/2004 10:02:22 AM - (   200) +OK Aba he
10/24/2004 10:02:22 AM - POP3 connection with 62.127.12.98 [62.127.12.98] ended. ID=200
10/24/2004 10:02:22 AM - {   200} END POP3


thx again..sorry for asking all this
Avatar of Johnny

ASKER

i never posted the code i used to view the info

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ParseFile()
        Dim i As Integer
        For i = 0 To LoginDetail.Length - 1
            ListBox1.Items.Add("Login_ID: " & LoginDetail(i).Login_ID) 'Login_ID
            ListBox1.Items.Add("Login_Email: " & LoginDetail(i).Login_Email) 'Login_Email
            ListBox1.Items.Add("Login_Protocal: " & LoginDetail(i).Login_Protocal) 'Login_Protocal
            ListBox1.Items.Add("Login_IP: " & LoginDetail(i).Login_IP) 'Login_IP
            ListBox1.Items.Add("Login_Date_Time: " & LoginDetail(i).Login_Date_Time) 'Login_Date_Time
            ListBox1.Items.Add("Logoff_Date_Time: " & LoginDetail(i).Logoff_Date_Time) 'Logoff_Date_Time
            ListBox1.Items.Add("#------------------------------------------#") 'Sep Line

        Next i


    End Sub

sorry
I hope you got a start now
Yes in SMTP there's no Email and logout time, that's what I mentioned if it is that way in the log we can not do anything, but if it was missing in the log that you sent me but actually it is being logged then we can pick that also

As for POP3 you must be getting the Login date time but no logout and mail id

And yes you are right if we pick END instead of LOGOUT we might get logout date time for all protocols so Replace the Last EndIF i.e.

ElseIf Str.IndexOf(" LOGOUT ") >= 0 AndAlso Str.IndexOf("completed") >= 0 Then
..... This block

with this code

 ElseIf Str.IndexOf("  END ") >= 0  Then
                TmpArr = Str.Split("-")
                'Get ID
                TmpStr = Str.Substring(Str.IndexOf("{") + 1, Str.IndexOf("}") - Str.IndexOf("{") - 1).Trim '---> The brackets have changed ( to {
                'First Element Forms Date & Time of Logout
                UpdateLogout(TmpStr, TmpArr(0))


Thanks

Avatar of Johnny

ASKER

im still trying to figure all this out...but it works great...thx

hopefully ill be able to parse the rest of the info too

Avatar of Johnny

ASKER

im trying to parse the start line...so i get delivery,smtp,pop3, and imap corectly as the protocal line we have been parseing is not right and i cant get it...im banging my head agenst the wall...

any help???

i dont understand how this all works...ive tryed looking up the tokens..and what not but im still confused...

nor do i understand how its being read...(populated into the checking)

im really lost..
thx for any help arif_eqbal
and sorry for asking so much...!
OK Lets look at the first Line Parsing i.e. the first If  block

'Open the File in Read Mode
 FileOpen(FNo, Fl, OpenMode.Input, OpenAccess.Read)
        'Loop through all the lines
        While Not EOF(FNo)
            'Read One Line at a Time
            Str = LineInput(FNo)
       'Now whenever a Request comes Our Log file logs it as "Requested IMAP connection"
'or "Requested SMTP connection" So all we need to do is Check if the Line currently Read
'contains both the words "Requested" and "Connection" if yes get inside the If block
            If Str.IndexOf(" Requested ") >= 0 AndAlso Str.IndexOf(" connection ") >= 0 Then
                'Now get the Index for the array where we will keep the values
                'If array LoginDetail is so far nothing then start from 0 Index otherwise write
                'on the last index i.e. Length of array
                If LoginDetail Is Nothing Then
                    Index = 0
                Else
                    Index = LoginDetail.Length
                End If
                'Redim the array to hold the new element
                ReDim Preserve LoginDetail(Index)

             'Now we have the storage place and we know this is the Connection request line
             'So Now we will try and extract values from this line
             'On the left of Dash "-" we have the Date Time value so first thing split it on dash

                TmpArr = Str.Split("-")
                'Now Since the Line has only one dash TmpArr will have two elements one
               ' containing value on the left of dash and one containing value on the Right
               
                'First Element will be Date & Time so just store it
                LoginDetail(Index).Login_Date_Time = TmpArr(0)
               
             'Now Second Element of Array will contain the User ID at the very end of the line
             'After the keyword ID= eg ID=769
             'So what we do, we get the Index of keyword ID= in the line and
       'move 3 places ahead so that I D and = are skipped then we copy the text till the end
                LoginDetail(Index).Login_ID = TmpArr(1).Substring(TmpArr(1).IndexOf("ID=") + 3) '+3 is for skipping ID=
               
      'Also the Protocol lies between keywords "Requested" And "Connection" So go to the
      'position where we have Requested keyword then move 10 places ahead as keyword
         'Requested is 9 chars long and one for space, then extract the value till index of
         'keyword Connection is reached

                TmpStr = Str.Substring(Str.IndexOf("Requested ") + 10, Str.IndexOf("connection ") - Str.IndexOf("Requested ") - 10).Trim
                LoginDetail(Index).Login_Protocal = TmpStr

         'Now for IP it is between [ and ] so apply the same logic as above
         'Go to [ move one place ahead and extract values till ] is reached

                TmpStr = Str.Substring(Str.IndexOf("[") + 1, Str.IndexOf("]") - Str.IndexOf("[") - 1)
                LoginDetail(Index).Login_IP = TmpStr


Hope you get this one now
there's similar Logic throughout so probably you can figure them out
If any problems feel free to ask