[Visual Basic] 纯文本查看 复制代码
Option Explicit
' ============================================================
' 企业内部网络路由跟踪工具
' 在"路由跟踪"表 B3 输入目标IP,点击按钮后:
' 1) 后台调用系统 tracert(隐藏窗口,不弹出黑框)
' 2) 解析每一跳的 IP 与响应时间,写入第8行起
' 3) 在"IP数据库"表按 IP 匹配,带出归属信息
' ============================================================
Sub 路由跟踪()
Dim wsMain As Worksheet, wsDB As Worksheet
Dim ipInput As String, tempFile As String, tmpDir As String
Dim wsh As Object, fso As Object, f As Object
Dim lineTxt As String, nRow As Long
On Error GoTo ErrHandler
Set wsMain = ThisWorkbook.Worksheets("路由跟踪")
Set wsDB = ThisWorkbook.Worksheets("IP数据库")
' 1. 读取并校验输入的IP
ipInput = Trim(wsMain.Range("B3").Value)
If ipInput = "" Then
MsgBox "请先在黄色单元格中输入目标IP地址!", vbExclamation, "提示"
Exit Sub
End If
If Not IsValidIP(ipInput) Then
MsgBox "IP地址格式不正确,示例:192.168.1.1", vbExclamation, "提示"
Exit Sub
End If
' 2. 清空上一次结果(保留表头)
wsMain.Range("A8:J60").ClearContents
' 3. 准备临时文件(tracert 结果输出到这里)
tmpDir = Environ("TEMP")
If tmpDir = "" Then tmpDir = "C:\Windows\Temp"
tempFile = tmpDir & "\tracert_" & Format(Now, "yyyymmdd_hhnnss") & ".txt"
' 4. 后台调用 tracert(0=隐藏窗口,True=等待命令结束)
' 参数:-d 不解析域名,-h 30 最多30跳
Set wsh = CreateObject("WScript.Shell")
wsh.Run "cmd.exe /c tracert -d -h 30 " & ipInput & " > """ & tempFile & """", 0, True
' 5. 读取结果文件
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(tempFile) Then
MsgBox "未获取到路由跟踪结果,请检查网络或IP地址。", vbCritical, "错误"
Exit Sub
End If
Set f = fso.OpenTextFile(tempFile, 1, False, 0) ' 1=只读, 0=ASCII
' 6. 逐行解析并写入
nRow = 8
Do While Not f.AtEndOfStream
lineTxt = Trim(f.ReadLine)
Call ParseHop(lineTxt, wsMain, nRow, wsDB)
Loop
f.Close
fso.DeleteFile tempFile, True
wsMain.Range("B3").Select
MsgBox "路由跟踪完成,共解析 " & (nRow - 8) & " 跳。", vbInformation, "完成"
Exit Sub
ErrHandler:
MsgBox "发生错误:" & Err.Description, vbCritical, "错误"
End Sub
' ------------------------------------------------------------
' 解析 tracert 的一行输出,提取 IP 与响应时间,并查询归属
' ------------------------------------------------------------
Sub ParseHop(ByVal lineTxt As String, wsMain As Worksheet, ByRef nRow As Long, wsDB As Worksheet)
Dim re As Object, cm As Object
Dim sIP As String
Dim t1 As String, t2 As String, t3 As String
Dim lastRow As Long, dbRng As Range, fRng As Range
Dim unit As String, dev As String, model As String, iface As String, note As String
' 提取行中的IP地址
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
re.Global = True
If Not re.Test(lineTxt) Then Exit Sub ' 无IP的行(标题/汇总)跳过
Set cm = re.Execute(lineTxt)
sIP = cm(cm.Count - 1).Value ' 取最后一个IP(真实节点IP)
If Not IsValidIP(sIP) Then Exit Sub
' 提取三个响应时间(<1 ms / 数字 ms;无响应为 *)
t1 = ExtractTime(lineTxt, re, 1)
t2 = ExtractTime(lineTxt, re, 2)
t3 = ExtractTime(lineTxt, re, 3)
' 在"IP数据库"表按IP精确查找归属
lastRow = wsDB.Cells(wsDB.Rows.Count, 1).End(xlUp).Row
If lastRow < 2 Then lastRow = 2
Set dbRng = wsDB.Range("A2:A" & lastRow)
Set fRng = dbRng.Find(sIP, LookAt:=xlWhole)
If Not fRng Is Nothing Then
unit = fRng.Offset(0, 1).Value
dev = fRng.Offset(0, 2).Value
model = fRng.Offset(0, 3).Value
iface = fRng.Offset(0, 4).Value
note = fRng.Offset(0, 5).Value
End If
' 写入结果行
With wsMain
.Cells(nRow, 1).Value = nRow - 7 ' 序号
.Cells(nRow, 2).Value = sIP ' IP地址
.Cells(nRow, 3).Value = ToNum(t1)
.Cells(nRow, 4).Value = ToNum(t2)
.Cells(nRow, 5).Value = ToNum(t3)
.Cells(nRow, 6).Value = unit ' 所属单位
.Cells(nRow, 7).Value = dev ' 所属设备
.Cells(nRow, 8).Value = model ' 所属型号
.Cells(nRow, 9).Value = iface ' 接口信息
.Cells(nRow, 10).Value = note ' 备注
End With
nRow = nRow + 1
End Sub
' ------------------------------------------------------------
' 提取第 idx 个响应时间(无响应返回 "*")
' ------------------------------------------------------------
Function ExtractTime(ByVal lineTxt As String, re As Object, ByVal idx As Long) As String
Dim cm As Object
re.Pattern = "(<1|\d+)\s*ms"
re.Global = True
If re.Test(lineTxt) Then
Set cm = re.Execute(lineTxt)
If cm.Count >= idx Then
ExtractTime = cm(idx - 1).SubMatches(0)
Exit Function
End If
End If
ExtractTime = "*"
End Function
' ------------------------------------------------------------
' 校验IP地址是否合法
' ------------------------------------------------------------
Function IsValidIP(ByVal ip As String) As Boolean
Dim parts() As String, i As Long, n As Long
parts = Split(ip, ".")
If UBound(parts) <> 3 Then Exit Function
For i = 0 To 3
If Not IsNumeric(parts(i)) Then Exit Function
n = CLng(parts(i))
If n < 0 Or n > 255 Then Exit Function
Next i
IsValidIP = True
End Function
' ------------------------------------------------------------
' 把响应时间转为数值(无响应/小于1ms 保持原文,不报错)
' ------------------------------------------------------------
Function ToNum(ByVal s As String) As Variant
If IsNumeric(s) Then
ToNum = CLng(s)
Else
ToNum = s
End If
End Function