【问题标题】:VB.NET MAC OUI LookupVB.NET MAC OUI 查找
【发布时间】:2018-10-05 17:49:49
【问题描述】:

早安,

我创建了一个 vb.NET 项目,该项目从位于 https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf;hb=HEAD 的 WireShark 的 HTML 文件中提取数据。

到目前为止,我已经能够将页面下载为字符串并搜索字符串以确认是否在字符串中找到“mac”。

我想让它搜索 MAC 地址,并输出 MAC 出现的整行。

例如,如果我搜索 00-00-00-00-00-00,我希望能够提取整行“00:00:00 Xerox Xerox Corporation”

这就是我所拥有的:

Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
    Dim mac As String = txt_MAC.Text.ToUpper
    Dim pattern As Regex = New Regex("^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$")
    Dim match As Match = pattern.Match(mac)

    If match.Success Then
        mac = mac.Replace("-", ":")
        mac = mac.Substring(0, mac.Length - 9)

        Dim wc As New Net.WebClient
        Dim html As String = wc.DownloadString("https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf;hb=HEAD")
        Dim macIndex = html.IndexOf(mac) 'returns line number in string

        MsgBox("Valid MAC: " & mac)

        If html.Contains(mac) Then
            'Display MAC + Vendor. IE.... 0:00:01   Xerox   Xerox Corporation'
            ' Is there a way to read only the specified line number in the string?
        End If

    Else
        MsgBox("You must enter a valid MAC")
    End If
End Sub

非常感谢任何帮助。

【问题讨论】:

  • 最简单的想法:逐行读取文件并针对正则表达式进行测试。如果您的行可能很长,在模式周围使用带有.* 的正则表达式可能会减慢处理速度。
  • 我使用 Dim macIndex = html.IndexOf(mac) 来拉取搜索到的 MAC 所在位置的索引。我得到 2472。有没有办法从字符串中读取该特定行?
  • 你做了太多多余的事情。使用Regex.Match(html, ".*" & mac & ".*").Value

标签: regex vb.net extract mac-address


【解决方案1】:

您可以从上一个链接中获取Mac address lookup list 作为 XML 文件,而不是解析 HTML 文件。

然后你可以使用像这个问题"Searching through XML in VB.NET " 中的 sn-p 来通过 XML 查找

【讨论】:

    【解决方案2】:

    虽然有点草率,但我可以使用以下代码做到这一点:

            Dim wc As New Net.WebClient
            Dim html As String = wc.DownloadString("https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf;hb=HEAD")
            Dim macIndex = html.IndexOf(mac)
    
            If html.Contains(mac) Then
                txt_result.Text = "Searching... "
                txt_result.Text = html.Chars(macIndex) & html.Chars(macIndex + 1) & html.Chars(macIndex + 2) & html.Chars(macIndex + 3) & html.Chars(macIndex + 4) & html.Chars(macIndex + 5) & html.Chars(macIndex + 6) & html.Chars(macIndex + 7) & html.Chars(macIndex + 8) & html.Chars(macIndex + 9) & html.Chars(macIndex + 10)
    

    谢谢!

    【讨论】:

    • 您可以使用 String.Substring(): txt_result.Text = html.Substring(macIndex, 11) 而不是获取和连接每个字符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    相关资源
    最近更新 更多