NetStumbler.org Forums

Go Back   NetStumbler.org Forums > NetStumbler Community > Scripts
Register Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old 06-27-2006   #1 (permalink)
mbolgian
 
Posts: n/a
Script To Parse the OUI.TXT file with Visual Basic (in Microsoft Access)

I threw this together to help with one of my projects. It may help you also. I'm storing my APs in an MS Access database. I needed a table to relate the MAC IDs to the BSSID in the FoundAP table. Creating it the first time is easy enough, but when the table needs to be updated, this Sub can be run and it will do it for you. Be warned, it runs very slow, taking about five minutes to parse all 9,374 records. Strings are slow as hell, at least in VB. Under ASP/VBScript this would run much faster, I believe.

Modify as needed. Enjoy!

Code:
Sub UpdateOUI()
' This will parse the OUI.txt file located at http://standards.ieee.org/regauth/oui/oui.txt
' This version does not grab the file automatically.  You'll have to download it and place it
' somewhere that's accessible by this code.  I have made a separate version that uses Microsoft's
' XML parser to download the file automatically AND parse it (useful for automated scripts run daily).
' This version is for the few people that may not have the XML parser installed on their system.

' Create a FSO.
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file.  Change the path to where you have your file saved.
Set ts = fso.OpenTextFile("C:\OUI\OUI.txt")

' The first six lines contain nothing useful to us, so we skip them.
For i = 1 To 6
ts.SkipLine
Next

' Any line that contains (hex) in it has the info we need.
Do While Not ts.AtEndOfStream

tvar = ts.ReadLine
If Mid(tvar, 12, 5) = "(hex)" Then
    Dim MMACID As String
    Dim Company As String
    MMACID = Replace(Left(tvar, 8), "-", "")
    Company = Replace(Mid(tvar, 19), "'", "''")
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT MACID FROM OUI WHERE MACID = '" & MMACID & "'")
    If rs.EOF Then
        CurrentDb.Execute "INSERT INTO OUI (MACID, Company) VALUES ('" & MMACID & "', '" & Company & "')"
    End If
End If
Loop

'Tidy up after ourselves.
ts.Close
Set ts = Nothing
rs.Close
Set rs = Nothing
End Sub
  Reply With Quote
Old 06-27-2006   #2 (permalink)
beakmyn
root\.workspace\.garbage.
 
Join Date: Aug 2003
Posts: 4,789
Quote:
Originally Posted by mbolgian
I threw this together to help with one of my projects. It may help you also. I'm storing my APs in an MS Access database. I needed a table to relate the MAC IDs to the BSSID in the FoundAP table. Creating it the first time is easy enough, but when the table needs to be updated, this Sub can be run and it will do it for you. Be warned, it runs very slow, taking about five minutes to parse all 9,374 records. Strings are slow as hell, at least in VB. Under ASP/VBScript this would run much faster, I believe.

Modify as needed. Enjoy!

Code:
Sub UpdateOUI()
' This will parse the OUI.txt file located at http://standards.ieee.org/regauth/oui/oui.txt
' This version does not grab the file automatically.  You'll have to download it and place it
' somewhere that's accessible by this code.  I have made a separate version that uses Microsoft's
' XML parser to download the file automatically AND parse it (useful for automated scripts run daily).
' This version is for the few people that may not have the XML parser installed on their system.

' Create a FSO.
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file.  Change the path to where you have your file saved.
Set ts = fso.OpenTextFile("C:\OUI\OUI.txt")

' The first six lines contain nothing useful to us, so we skip them.
For i = 1 To 6
ts.SkipLine
Next

' Any line that contains (hex) in it has the info we need.
Do While Not ts.AtEndOfStream

tvar = ts.ReadLine
If Mid(tvar, 12, 5) = "(hex)" Then
    Dim MMACID As String
    Dim Company As String
    MMACID = Replace(Left(tvar, 8), "-", "")
    Company = Replace(Mid(tvar, 19), "'", "''")
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT MACID FROM OUI WHERE MACID = '" & MMACID & "'")
    If rs.EOF Then
        CurrentDb.Execute "INSERT INTO OUI (MACID, Company) VALUES ('" & MMACID & "', '" & Company & "')"
    End If
End If
Loop

'Tidy up after ourselves.
ts.Close
Set ts = Nothing
rs.Close
Set rs = Nothing
End Sub

move your DIM outside of the loop so you declare them once and not everytime you go through the loop.
The real memory hog and slowness is creating a hook into your database every time you want to add a record.

Instead of using opensrecordset and insert consider adding a WHERE clause to your insert statment.
__________________
It's not Intelligent Design, it's peer pressure.

┌──────────────────────────────┐
NS Icons Explained|et hoc genus omne
└──────────────────────────────┘
beakmyn is offline   Reply With Quote
Old 06-27-2006   #3 (permalink)
Scruge
Nyuk nyuk!
 
Scruge's Avatar
 
Join Date: Jan 2005
Location: TX
Posts: 1,378
5 minutes is a long time..

You should be able to get your time down to less than 1 second using low level read and write. I convert the OUI data into .dbf and tablized .txt in about 1 sec.
__________________
KNSGEM
A wifi boundary plotter for Google Earth
Click Me
Scruge is offline   Reply With Quote
Old 06-27-2006   #4 (permalink)
mbolgian
 
Posts: n/a
Thanks for the tips. I moved the Dim statements outside of the loop (not sure why I put them inside, other than I threw this together in 5 min) but it still runs dog slow due to the recordset lookup. I wasn't sure what you meant by using a WHERE clause in the INSERT statement. How would I do that?
  Reply With Quote
Old 06-27-2006   #5 (permalink)
streaker69
Psychic Amish Stumbler
 
streaker69's Avatar
 
Join Date: Jul 2004
Location: Virginville, BlueBall, Bird In Hand, Intercourse, Paradise, PA
Posts: 11,790
Quote:
Originally Posted by beakmyn
move your DIM outside of the loop so you declare them once and not everytime you go through the loop.
The real memory hog and slowness is creating a hook into your database every time you want to add a record.

Instead of using opensrecordset and insert consider adding a WHERE clause to your insert statment.
I may be wrong, but how does one do a WHERE on a Record that doesn't exist?

If you're inserting it, then you're creating the record, you can do a WHERE in the UPDATE, but I've never see one on a INSERT.

I could be wrong, never claimed to know everything about SQL.
__________________
"One of these days, I'm going to cut you to pieces."

If you're offended by this post, please feel free to report it to one of the many helpful moderators of this forum.

Thank you.
streaker69 is offline   Reply With Quote
Old 06-27-2006   #6 (permalink)
beakmyn
root\.workspace\.garbage.
 
Join Date: Aug 2003
Posts: 4,789
Quote:
Originally Posted by streaker69
I may be wrong, but how does one do a WHERE on a Record that doesn't exist?

If you're inserting it, then you're creating the record, you can do a WHERE in the UPDATE, but I've never see one on a INSERT.

I could be wrong, never claimed to know everything about SQL.
I was thinking something else, basically a insert/select all in one using not Exist


Normally you'd open a connection to the table then use a select statement to return a true/false then perform an update/insert based on the conditions. There are more elequeont ways of doing this rather then they method you're using.
__________________
It's not Intelligent Design, it's peer pressure.

┌──────────────────────────────┐
NS Icons Explained|et hoc genus omne
└──────────────────────────────┘
beakmyn is offline   Reply With Quote
Old 06-28-2006   #7 (permalink)
goldfndr
Knight Tooth Puller
 
goldfndr's Avatar
 
Join Date: Aug 2004
Location: Bellevue, WA, USA
Posts: 182
Quote:
Originally Posted by mbolgian
Code:
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT MACID FROM OUI WHERE MACID = '" & MMACID & "'")
    If rs.EOF Then
        CurrentDb.Execute "INSERT INTO OUI (MACID, Company) VALUES ('" & MMACID & "', '" & Company & "')"
    End If
My thought is, rather than a huge number of OpenRecordsets (with all of their associated overhead), simply use Scripting.Dictionary[1] to keep track of Unique entries[2], then insert the whole lot of Dictionary entries to CurrentDb without error checking. That'll allow you to just use CurrentDb.Execute statements, no need for CurrentDb.OpenRecordset evaluations.

If you're unfamiliar with Scripting.Dictionary, look at the Master Script's ns04speech.vbs: adding at OnScanResultSpeech, regurgitating at OnScanCompleteSpeech. ns04database.vbs also has some potentially useful code. Obviously MSDN et al have more info.

[1]Assuming the language you're using exposes Scripting.Dictionary. You're using Dim with types, so my early assumption of vbscript was incorrect.
[2]Is an OUI listing with duplicates valid?

Last edited by goldfndr : 06-28-2006 at 05:43 AM.
goldfndr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Google
 
Web NetStumbler.org

All times are GMT -7. The time now is 03:00 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 ©2007, Crawlability, Inc.


All messages express the views of the author and are for entertainment purposes only. Netstumbler.org cannot be held responsible for the authenticity of the content or the actions of its members. By using this site and its services, you warrant that you will not post any messages that are discriminating, obscene, hateful, threatening, or otherwise violates any laws and you release Netstumbler.org from any future claims of any kind whatsoever including, but not limited to, addiction and loss of productivity. All forum messages, private messages and any other content are properties of Netstumbler.org. Even if publicly available, personal or copyrighted information are not to be posted without the consent of the owner. Distribution of licensed and copyrighted materials in any way not endorsed by the copyright owner is strictly prohibited. You may not use this site and its resources to spam other sites or individuals or perform any action that violates any law. Items sold or bought in the For Sale forum are sold as is and no warranty or insurance of any kind is provided. Netstumbler.org cannot be held responsible for the outcome of any transactions and no warranty of any kind is provided, either express or implied. Vulgar words are not allowed in the subject lines ; they may be used in the message body in any forum. The Administrator, Super Moderators and Moderators of Netstumbler.org have the right to remove, edit, move or close any thread for any reason and to reveal your identity and other known information in the event of a complaint or legal action arising from any message posted by you.