以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 如何获取网卡的物理地址 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=172002) |
-- 作者:teng0731 -- 发布时间:2021/9/17 11:01:00 -- 如何获取网卡的物理地址 通过下面的语句可以得到一个字符串,想要从字符串中获取本机电脑的所有网卡的物理地址(也就是mac地址) Dim p As new Process() p.StartInfo.FileName = "cmd.exe" p.StartInfo.UseShellExecute = False \'关闭Shell的使用 p.StartInfo.RedirectStandardInput = True \'重定向标准输入 p.StartInfo.RedirectStandardOutput = True \'重定向标准输出 p.StartInfo.RedirectStandardError = True \'重定向错误输出 p.StartInfo.CreateNoWindow = True \'设置不显示窗口 p.Start() p.StandardInput.WriteLine("ipconfig/all") p.StandardInput.WriteLine("exit") Dim strRst As String = p.StandardOutput.ReadToEnd() output.show(strRst) 上面的strRst会得到很多信息,现在需要将里面的物理地址单独提取出来,用“;”分号来做分隔符号的一个组合字符串 |
-- 作者:有点蓝 -- 发布时间:2021/9/17 11:11:00 -- http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=50305&authorid=0&page=0&star=2 |
-- 作者:teng0731 -- 发布时间:2021/9/17 12:15:00 -- 目前主要卡在正则表达式的编写上面,无法匹配到相应的行上面,比如用这个语句: Dim p As new Process() p.StartInfo.FileName = "cmd.exe" p.StartInfo.UseShellExecute = False \'关闭Shell的使用 p.StartInfo.RedirectStandardInput = True \'重定向标准输入 p.StartInfo.RedirectStandardOutput = True \'重定向标准输出 p.StartInfo.RedirectStandardError = True \'重定向错误输出 p.StartInfo.CreateNoWindow = True \'设置不显示窗口 p.Start() p.StandardInput.WriteLine("ipconfig/all") p.StandardInput.WriteLine("exit") Dim strRst As String = p.StandardOutput.ReadToEnd() output.show(strRst) 可以得到下面的结果: ======================================================== Microsoft Windows [版本 10.0.18363.836] (c) 2019 Microsoft Corporation。保留所有权利。 C:\\foxtable\\Development>ipconfig/all Windows IP 配置 主机名 . . . . . . . . . . . . . : DESKTOP-5CIA55H 主 DNS 后缀 . . . . . . . . . . . : 节点类型 . . . . . . . . . . . . : 混合 IP 路由已启用 . . . . . . . . . . : 否 WINS 代理已启用 . . . . . . . . . : 否 以太网适配器 以太网: 连接特定的 DNS 后缀 . . . . . . . : 描述. . . . . . . . . . . . . . . : Intel(R) Ethernet Connection (7) I219-LM 物理地址. . . . . . . . . . . . . : 00-A8-61-BF-2H-40 DHCP 已启用 . . . . . . . . . . . : 否 自动配置已启用. . . . . . . . . . : 是 本地链接 IPv6 地址. . . . . . . . : fe82::943b:fab6:ba1b:827d%11(首选) IPv4 地址 . . . . . . . . . . . . : 121.159.4.162(首选) 子网掩码 . . . . . . . . . . . . : 255.255.255.128 默认网关. . . . . . . . . . . . . : 121.159.4.254 DHCPv6 IAID . . . . . . . . . . . : 100718689 DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-25-F0-1A-68-00-D8-61-EF-2C-40 DNS 服务器 . . . . . . . . . . . : 121.154.142.28 121.154.142.29 TCPIP 上的 NetBIOS . . . . . . . : 已启用 以太网适配器 以太网 2: 连接特定的 DNS 后缀 . . . . . . . : 描述. . . . . . . . . . . . . . . : Virtual Secure Domain Adapter V2 物理地址. . . . . . . . . . . . . : 00-FN-KK-E7-SD-29 DHCP 已启用 . . . . . . . . . . . : 是 自动配置已启用. . . . . . . . . . : 是 IPv4 地址 . . . . . . . . . . . . : 151.162.36.19(首选) 子网掩码 . . . . . . . . . . . . : 255.255.252.0 获得租约的时间 . . . . . . . . . : 2021年9月17日 7:55:55 租约过期的时间 . . . . . . . . . : 2022年9月17日 7:55:55 默认网关. . . . . . . . . . . . . : DHCP 服务器 . . . . . . . . . . . : 151.162.36.0 TCPIP 上的 NetBIOS . . . . . . . : 已启用 C:\\foxtable\\Development>exit =========对于上面的结果,用正则表达式,如果取到 物理地址. . . . . . . . . . . . . : 00-A8-61-BF-2H-40 物理地址. . . . . . . . . . . . . : 00-FN-KK-E7-SD-29 这两行记录所对应的mac地址,修改为下面的完整语句,还是取不到数据: Dim p As new Process() p.StartInfo.FileName = "cmd.exe" p.StartInfo.UseShellExecute = False \'关闭Shell的使用 p.StartInfo.RedirectStandardInput = True \'重定向标准输入 p.StartInfo.RedirectStandardOutput = True \'重定向标准输出 p.StartInfo.RedirectStandardError = True \'重定向错误输出 p.StartInfo.CreateNoWindow = True \'设置不显示窗口 p.Start() p.StandardInput.WriteLine("ipconfig/all") p.StandardInput.WriteLine("exit") Dim strRst As String = p.StandardOutput.ReadToEnd() Dim mc = System.Text.RegularExpressions.Regex.Matches(strRst, " 物理地址. . . . . . . . . . . . . :[a-z0-9]{2}-[a-z0-9]{2}-[a-z0-9]{2}-[a-z0-9]{2}-[a-z0-9]{2}-[a-z0-9]{2}") For i As Integer = 0 To mc.count -1 Dim v = mc(i).value If v.contains(" 物理地址. . . . . . . . . . . . . :") Then output.show(v.replace(" 物理地址. . . . . . . . . . . . . :","").trim()) End If Next 这一段如何进行修改? |
-- 作者:有点蓝 -- 发布时间:2021/9/17 12:25:00 -- Dim mc = System.Text.RegularExpressions.Regex.Matches(strRst, " 物理地址. . . . . . . . . . . . . : [a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}") |
-- 作者:teng0731 -- 发布时间:2021/9/17 12:32:00 -- Dim p As new Process() p.StartInfo.FileName = "cmd.exe" p.StartInfo.UseShellExecute = False \'关闭Shell的使用 p.StartInfo.RedirectStandardInput = True \'重定向标准输入 p.StartInfo.RedirectStandardOutput = True \'重定向标准输出 p.StartInfo.RedirectStandardError = True \'重定向错误输出 p.StartInfo.CreateNoWindow = True \'设置不显示窗口 p.Start() p.StandardInput.WriteLine("ipconfig/all") p.StandardInput.WriteLine("exit") Dim strRst As String = p.StandardOutput.ReadToEnd() Dim mc = System.Text.RegularExpressions.Regex.Matches(strRst, " 物理地址. . . . . . . . . . . . . : [a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}-[a-zA-Z0-9]{2}") For i As Integer = 0 To mc.count -1 Dim v = mc(i).value If v.contains(" 物理地址. . . . . . . . . . . . . :") Then output.show(v.replace(" 物理地址. . . . . . . . . . . . . :","").trim()) End If Next ============================================================= 上面确实可以了,能够取到数据,还问一下: 正则表达式中的" 物理地址. . . . . . . . . . . . . : 这一段能否简化一下,因为不确定不同版本的windows操作系统会不会不一样或者多一个.,或者少一个.等情况
[此贴子已经被作者于2021/9/17 12:49:16编辑过]
|
-- 作者:有点蓝 -- 发布时间:2021/9/17 13:30:00 -- 简化不了。碰到这种情况再说 |
-- 作者:z769036165 -- 发布时间:2021/9/18 8:09:00 -- Dim nics() As System.Net.NetworkInformation.NetworkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces Output.Show(nics(0).GetPhysicalAddress.Tostring)
|
-- 作者:teng0731 -- 发布时间:2021/9/18 11:49:00 -- 感谢 有点蓝老师和z769036165 ,谢谢! Dim nics() As System.Net.NetworkInformation.NetworkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces For i As Integer = 0 To nics.count -1 Output.Show(nics(i).GetPhysicalAddress.Tostring) Next ==================这种方法更好一点,代码更少 [此贴子已经被作者于2021/9/18 12:07:08编辑过]
|