以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [讨论]如何读取局域网内某台电脑的网卡MAC地址及计算机名字?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=131566)

--  作者:huangfanzi
--  发布时间:2019/3/1 13:04:00
--  [讨论]如何读取局域网内某台电脑的网卡MAC地址及计算机名字?
刚开发的一个系统给客户用,遇到一个问题,系统用的是云服务器,客户要求登录系统的客户端电脑必须位于公司内,不可以在公司外登录,我为了便于维护及后续开发又不想把数据库装到这个公司的局域网内的服务器上,
我想了半天,想到一个变通的办法,就是这个公司本身有台24小时开机的服务器,假定IP是 192.168.1.188  我想在登录系统时,读取这个IP的网卡地址或计算机名什么的,
然后比对下,PING通这台电脑并且能得到这台电脑的名字与网卡MAC地址,如果比对一致,继续执行后续代码,不一致,就退出
请问老师,如何读取局域网内指定IP电脑的网卡MAC地址与计算机名,代码如何写?
或有没有别的更好的办法实现我想要的功能,谢谢老师

--  作者:有点甜
--  发布时间:2019/3/1 14:51:00
--  

无法根据ip地址获取计算机名。

 

获取局域网ip对应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("arp -a")
p.StandardInput.WriteLine("exit")
Dim strRst As String = p.StandardOutput.ReadToEnd()

Dim mc = System.Text.RegularExpressions.Regex.Matches(strRst, "192+\\.168\\.[0-9]+\\.[0-9]+.+?[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
    msgbox(mc(i).value)
Next


--  作者:huangfanzi
--  发布时间:2019/3/1 14:56:00
--  
老师,我就想获得 192.168.1.188的MAC 上面代码应该如何写?
--  作者:有点甜
--  发布时间:2019/3/1 15:02: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("arp -a")
p.StandardInput.WriteLine("exit")
Dim strRst As String = p.StandardOutput.ReadToEnd()

Dim mc = System.Text.RegularExpressions.Regex.Matches(strRst, "192+\\.168\\.[0-9]+\\.[0-9]+.+?[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("192.168.0.104") Then
        msgbox(v.replace("192.168.0.104","").trim())
    End If
Next


--  作者:huangfanzi
--  发布时间:2019/3/1 15:18:00
--  
上面代码有些小问题,我改了下,正常了

For i As Integer = 0 To mc.count -1
    Dim s As String = mc(i).value
    If s.Contains("192.168.1.253") Then
        Output.Show(s.Replace("192.168.1.253","").Trim())
    End If
Next

但还是有个小问题
如果我想比对的IP地址是 192.168.1.2 ,程序还会返回例如 192.168.1.201  192.168.1.202 等,问题显然是出在Contains上,如何解决呢?
[此贴子已经被作者于2019/3/1 15:33:10编辑过]

--  作者:有点甜
--  发布时间:2019/3/1 15:55: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("arp -a")
p.StandardInput.WriteLine("exit")
Dim strRst As String = p.StandardOutput.ReadToEnd()

Dim mc = System.Text.RegularExpressions.Regex.Matches(strRst, "192+\\.168\\.[0-9]+\\.[0-9]+.+?[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
    Dim v1 = v.split(" ")(0)
    If v1 = "192.168.0.104" Then
        msgbox(v.replace(v1,"").trim())
    End If
Next