It takes two parameters: "FileInPath" is the location of the OUI file and "FileOutPath" is the path of the CSVyou want to save to. If no file is there, one will be created automatically.
Private Sub OuiToCsv(ByVal FileInPath As String, ByVal FileOutPath As String)
Const MARKER As String = "(hex)" ' This marks the lines we want to parse.
Dim TextLine As String = ""
If Not File.Exists(FileInPath) Then
Log(" File '" + FileInPath + "' does not exist. Aborting...", COLOR_ERROR)
Exit Sub
End If
Dim objReader As New System.IO.StreamReader(FileInPath)
Dim objWriter As New System.IO.StreamWriter(FileOutPath)
Dim mac As String = ""
Dim company As String = ""
' Since we will be doing a lot on concatenations, a string
' builder is best.
Dim sb As New StringBuilder()
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
If TextLine.Contains(MARKER) Then
sb = New StringBuilder
' This should be relatively flexible if they change the
' delimiting whitespace in the file.
mac = TextLine.Substring(0, TextLine.IndexOf(MARKER)).Replace("-", "").Trim
company = TextLine.Substring(TextLine.IndexOf(MARKER) + MARKER.Length).Trim()
' For some reason the original file contains double spaces
' in some lines. This will remove them from the compaby field.
While company.Contains(" ")
company = company.Replace(" ", " ")
End While
sb.Append(Chr(34)) ' add a quote
sb.Append(mac) ' add the mac address
sb.Append(Chr(34)) ' add a quote
sb.Append(",") ' add the comma
sb.Append(Chr(34)) ' add a quote
sb.Append(company) ' add the company name
sb.Append(Chr(34)) ' add a quote
sb.Append(Chr(10)) ' add a linefeed
Try
objWriter.Write(sb.ToString)
Catch
Log(" Could not write to '" + FileOutPath + "': " + Err.Description, COLOR_ERROR)
objReader.Close()
objReader.Dispose()
objWriter.Flush()
objWriter.Close()
objWriter.Dispose()
Exit Sub
End Try
End If
Loop
' A bit of housekeeping
objWriter.Flush()
objWriter.Close()
objWriter.Dispose()
objReader.Close()
objReader.Dispose()
End Sub
The "Log()" function is a function I use to add make entries in a RichTextBox. Feel free to remove it or replace it with a "Console.Writeline()".
The code above, will not download the OUI. To do that use the following function.
It takes up to four parameters, even though only two are needed in this case: "uri" is the online location of the file we want to download (http://standards.ieee.org/regauth/oui/oui.txt), "destFile" is the location we want the file downloaded to, "uname" and "pass" is the username and password to access the URI if needed.
The function will return nothing if it completed successfully or a description of the error if not.
Public Function DownloadFile(ByVal uri As String, ByVal destFile As String, _
Optional ByVal uname As String = Nothing, Optional ByVal pass As String = Nothing) _
As String
Try
Dim wc As New System.Net.WebClient
If Not uname Is Nothing AndAlso Not pass Is Nothing Then
wc.Credentials = New System.Net.NetworkCredential(uname, pass)
End If
wc.DownloadFile(uri, destFile)
Catch
Return Err.Description
End Try
Return ""
End Function
So to download and parse the file, simply run the two functions one after the other:
DownloadFile("http://standards.ieee.org/regauth/oui/oui.txt", "C:\oui.txt")
OuiToCsv("C:\oui.txt", "C:\oui.csv")
Hey im having some trouble with your code, it seems to consume a lot of memory, and it consumes a full core of my pc. Any ideas?
ReplyDeleteThere is a small bug causing the high CPU etc. It will actually loop at these lines of code:
ReplyDeleteWhile company.Contains(" ")
company = company.Replace(" ", " ")
End While
You need to add a space character on the first and second line like this:
While company.Contains(" ")
company = company.Replace(" ", " ")
End While
Regards Per J
kütahya
ReplyDeletesivas
hatay
ığdır
trabzon
U43XYB