Results 1 to 11 of 11

Thread: VBA: CustomerID for IP-Addresses in external logfile.txt

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    VBA: CustomerID for IP-Addresses in external logfile.txt

    Hi everybody,

    I have a logfile.txt which looks like this:
    62.68.174.12 - - [01/Jan/2005:00:07:58 +0100] "GET /pubid-848078832/index/index_pdf.html HTTP/1.0" 200 2862 "-" "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" 0
    62.68.174.12 - - [01/Jan/2005:00:08:01 +0100] "GET /pubid-848078832/show_pdf.html?/pdf/pubid-848078832_1043.pdf?page=9 HTTP/1.0" 200 130220 "-" "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" 2
    ...

    Now I want to insert Customer-IDs to each IP-Address in that logfile. The CustomerID could be seperated from the IP-Address by a ";" or as you like.

    I have an Excel-table which provides the CustomerIDs for the IP-Addresses, BUT those IP-Addresses are ranges...

    That table looks like this (I have it as an external TXT also):
    IP-Address(Range) -- CustomerID
    203.75.24.6 -- 290
    203.75.24.5 -- 290
    203.75.24.250 -- 295
    203.75.23.20 -- 295
    199.212.31.* -- 293
    198.96.61.* -- 293
    192.82.131.* -- 293
    192.82.130.* -- 296
    192.82.129.* -- 296
    192.82.128.* -- 296
    192.75.254.* -- 296
    192.75.177.* -- 296
    192.139.207.* -- 296
    192.139.206.* -- 298
    192.12.183.* -- 298
    142.151.*.* -- 298


    I found this so far, but I couldn't apply it:
    http://forums.microsoft.com/MSDN/Sho...22180&SiteID=1

    The logfile has about 100,000 lines, so I cannot import it into Excel anymore which I was able to do until recently. Now I heard it was possible to alter an external file - but I don't know how...

    Is there a way, that a MAKRO will check into which range of IP-addresses the IP-address of each entry of the logfile falls, and which will then produce the according CustomerID and put it into the logfile???

    Thanks so MUCH!!!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Integer, pos As Integer, mytest As String, pos1 As Integer
    3. Dim f1 As Integer, f2 As Integer, f3 As Integer
    4. Dim li As Integer, strfi As String, arrfi() As String, pard As Boolean
    5. Dim tlen As Integer, myind As String
    6. Dim clist As String, logln As String, iparr() As String
    7.  
    8.  
    9. f1 = FreeFile
    10. Open "logfile.txt" For Input As f1
    11.  
    12. f2 = FreeFile
    13. Open "custlist.txt" For Input As f2
    14. f3 = FreeFile
    15. Open "newlist.txt" For Output As f3
    16.  
    17. clist = Input(LOF(f2), #f2)
    18.  
    19. Do While Not EOF(f1)
    20.     Line Input #f1, logln
    21.     logln = Left(logln, InStr(logln, "-") - 1)
    22.     iparr = Split(logln, ".")
    23.     pos = InStr(clist, iparr(0) & ".*.*.*")
    24.     If pos = 0 Then pos = InStr(clist, iparr(0) & "." & iparr(1) & ".*.*")
    25.     If pos = 0 Then pos = InStr(clist, iparr(0) & "." & iparr(1) & "." & iparr(2) & ".*")
    26.     If pos = 0 Then pos = InStr(clist, logln)
    27.     If pos = 0 Then GoTo noip 'this ip not found
    28.    
    29.     pos = InStr(pos + 1, clist, "-- ")
    30.     pos1 = InStr(pos + 1, clist, vbNewLine)
    31.     If pos1 = 0 Then pos1 = LOF(f2) + 1
    32.     Print #f3, logln & Mid(clist, pos, pos1 - pos)
    33.  
    34.  
    35. noip:
    36. Loop
    37.  
    38.  
    39. Close
    40. Shell "notepad.exe newlist.txt", vbNormalFocus
    41. Stop
    42. Exit Sub

    try this, i just pasted the lines you posted into files, might take a while to do 100,000 lines though

    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Thumbs up Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    GREAT THANKS SO MUCH!!!
    It works great!!! Especially with the ranges.

    One slight thing: so far the output newlist.txt contains only IP-Adress and corresponding CustomerID. But it should also have the rest of the original logfile.
    How do I have to alter this code line:
    VB Code:
    1. Print #f3, logln & Mid(clist, pos, pos1 - pos)



    Do you know whether there are limitation, lets say the logfile might eventually have 200,000 lines or 500,000 lines. Do you think it will still work?

    THANKS AGAIN!!!!

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    where do you want the customers id??

    after the ip, or at the end

    VB Code:
    1. Do While Not EOF(f1)
    2.     Line Input #f1, logln
    3.  
    4.     restofline = right(logln, len(logln) - instr(logln, " -") - 1 ) 'add here
    5.     logln = Left(logln, InStr(logln, " -") - 1) 'edit
    6.  
    7.  
    8.     iparr = Split(logln, ".")
    9.     pos = InStr(clist, iparr(0) & ".*.*.*")
    10.     If pos = 0 Then pos = InStr(clist, iparr(0) & "." & iparr(1) & ".*.*")
    11.     If pos = 0 Then pos = InStr(clist, iparr(0) & "." & iparr(1) & "." & iparr(2) & ".*")
    12.     If pos = 0 Then pos = InStr(clist, logln)
    13.     If pos = 0 Then GoTo noip 'this ip not found
    14.         pos = InStr(pos + 1, clist, "-- ")
    15.     pos1 = InStr(pos + 1, clist, vbNewLine)
    16.     If pos1 = 0 Then pos1 = LOF(f2) + 1
    17.  
    18.  
    19.     Print #f3, logln & Mid(clist, pos, pos1 - pos) & restofline 'edit
    20.  
    21.  
    22. noip:
    23. Loop

    this will put the rest of the line from the log after the customer id
    i haven't tested the changes

    i don't think it will matte how big you logfile gets, but it might need to be changed if your customer list (file) gets too big, as i keep the whole of that file in memory, you didn't indicate how big it might be.
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Resolved Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    Hey, this is great!!! THANKS!!!
    It is really quick...!

    The customerID file might get up to a thousand entries, but not much more than that, so I guess it should be fine...

    THANKS SO MUCH again!!

    JVLennox

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Question Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    Hi, suddenly I have another problem:

    I just put the code on a different computer and now I get this error message:
    Run-time error '62': Input past end of file
    ...after this line:
    VB Code:
    1. clist = Input(LOF(f2), #f2)

    No idea why! I put the same files into the same location on the harddrive, etc...

    Can you help me out with that? Yesterday it ran on my computer...
    Maybe it is because on this computer I use the full CustID-file which has around 1000 entries and the memory can only handle 256 items??? ???

    THANKS again
    Last edited by JVLennox; Jul 10th, 2006 at 12:45 PM.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    how big is the cust-id file?? Mb

    how much memory both computers?

    try copy it onto your computer to see what happens

    try to run it on that computer with the cust id file you used

    pete

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    i tested the code with a customer file i generated with 100,000 customers, it was able to load the file with no problem on my laptop.

    if this is now working, please mark the thread resolved

    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Unhappy Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    Hi Pete!

    I tried it again and again, and even got some more memory....
    DIDn't help.

    I always get the error "Overflow" - so I thought it would be the memory.
    The customer file is not really big, has a couple thousand lines.
    (I ATTACHED it.)

    The code looks like that:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Integer, pos As Integer, mytest As String, pos1 As Integer
    3. Dim f1 As Integer, f2 As Integer, f3 As Integer
    4. Dim li As Integer, strfi As String, arrfi() As String, pard As Boolean
    5. Dim tlen As Integer, myind As String
    6. Dim clist As String, logln As String, iparr() As String
    7.  
    8.  
    9. f1 = FreeFile
    10. Open "C:\logfile.txt" For Input As f1
    11.  
    12. f2 = FreeFile
    13. Open "C:\customers.txt" For Input As f2
    14. f3 = FreeFile
    15. Open "C:\newlist.txt" For Output As f3
    16.  
    17. clist = Input(LOF(f2), #f2)
    18.  
    19. Do While Not EOF(f1)
    20.     Line Input #f1, logln
    21.  
    22.     restofline = right(logln, len(logln) - instr(logln, " ") - 1 ) 'add here
    23.     logln = Left(logln, InStr(logln, " -") - 1) 'edit
    24.  
    25.  
    26.     iparr = Split(logln, ".")
    27.     pos = InStr(clist, iparr(0) & ".*.*.*")
    28.     If pos = 0 Then pos = InStr(clist, iparr(0) & "." & iparr(1) & ".*.*")
    29.     If pos = 0 Then pos = InStr(clist, iparr(0) & "." & iparr(1) & "." & iparr(2) & ".*")
    30.     If pos = 0 Then pos = InStr(clist, logln)
    31.     If pos = 0 Then GoTo noip 'this ip not found
    32.         pos = InStr(pos + 1, clist, "-- ")
    33.     pos1 = InStr(pos + 1, clist, vbNewLine)
    34.     If pos1 = 0 Then pos1 = LOF(f2) + 1
    35.  
    36.  
    37.     Print #f3, logln & Mid(clist, pos, pos1 - pos) & restofline 'edit
    38.  
    39.  
    40. noip:
    41. Loop
    42.  
    43. Close
    44. Shell "notepad.exe newlist.txt", vbNormalFocus
    45. Stop
    46. Exit Sub


    Why is it running at your computer, but not with mine? Or is there a slight typing error in the code???

    THANKS so much!
    Attached Files Attached Files

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    i doubt if memory was the problem, i had previously tested with customer file with 100,000 entries

    i am not totally certain on this, but the error you are getting is to do with something in the txt file, probably an end of file marker. try a different text file.

    your file worked fine on my machine, but it is a copy that has been downloaded, so it has been changed

    i had a text file here that was giving the same error, i opened it in notepad, selected all, copied it to a new empty notepad and saved it, the new file then worked ok, but it may depend how the customers.txt file is created as to whether this will help you.
    the alternative is to use the line input method to read all lines into a single variable, which is really no harder.
    replace clist = Input(LOF(f2), #f2) with

    VB Code:
    1. Do While Not EOF(f2)
    2.     Line Input #f2, mytest
    3.     clist = clist & mytest
    4.     Loop

    theres a couple of options, something should work

    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: VBA: CustomerID for IP-Addresses in external logfile.txt

    Thanks So Much For Your Help!!!!

    Really!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width