【发布时间】:2026-01-17 10:55:01
【问题描述】:
我需要将电子表格中的值与包含区域传输输出的文本文件进行比较,以根据电子表格中列出的类型获取特定接口的 IP 地址。
-
我的意见:
- Excel 电子表格: +------------+------------+ | 姓名 | 整数类型 | +------------+------------+ |开关-1 | | |服务器1 |生产 | |服务器2 |离体 | |开关-2 | | |服务器3 |生产 | |服务器4 |其他 | +------------+------------+
- 文本文件: server1-prod.fqdn.com 86400 IN A 192.168.0.10 server1-oob.fqdn.com 600 IN A 192.168.0.11 server2-prod.fqdn.com 600 IN A 192.168.0.12 server2-oob.fqdn.com 600 IN A 192.168.0.13 server3-prod.fqdn.com 300 IN A 192.168.0.14 server3-oob.fqdn.com 600 IN A 192.168.0.15 server4-foo.fqdn.com 86400 IN A 192.168.0.16
-
我想要的输出:
开关名称:Switch-1 接口名称:server1-prod.fqdn.com - IP 地址:192.168.0.10 接口名称:server2-oob.fqdn.com - IP 地址:192.168.0.11 开关名称:Switch-2 接口名称:server3-prod.fqdn.com - IP 地址:192.168.0.12 server4-foo.fqdn.com 未知接口类型:其他。
我当前的代码:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Server-List-by-Switch.xlsx")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("FQDN-and-IP.txt")
strNextLine = objFile.Readline
arrList = Split(objFile.ReadLIne(),vbCrLf)
intRow = 2
objFile.Close
Do Until objExcel.Cells(intRow,1).Value = ""
objHostName = objExcel.Cells(intRow,1).Value
objIntfType = objExcel.Cells(intRow,2).Value
If InStr(1,objExcel.Cells(intRow,1).Value,"Switch-",1) > 0 Then
Wscript.Echo vbCrLF & "Switch Name: " & objHostName
ElseIf InStr(1,objExcel.Cells(intRow, 1).Value,"Switch-",1) = 0 And InStr(1,objExcel.Cells(intRow,2).Value,"Production",1) > 0 then
For Each line In arrList
objFQDN = LCase(objHostName & "-prod.fqdn.com.")
strFQDN = splitRE (strNextLine, "\s+")
Wscript.Echo strFQDN(0)
If InStr(Lcase(line),objFQDN) > 0 Then
strIPAddress = Right(line, Len(line) - InStrRev(line," ") )
Wscript.Echo "Interface name(Production): " & strFQDN & " - IP Address: " & strIPAddress
End If
Next
ElseIf InStr(1,objExcel.Cells(intRow, 1).Value,"Switch-",1) = 0 And InStr(1,objExcel.Cells(intRow,2).Value,"OOB",1) > 0 then
For Each line In arrList
objFQDN = LCase(objHostName & "-oob.fqdn.com.")
strFQDN = splitRE (strNextLine, "\s+")
Wscript.Echo strFQDN(0)
If InStr(Lcase(line),objFQDN) > 0 Then
strIPAddress = Right(line, Len(line) - InStrRev(line," ") )
Wscript.Echo "Interface name(Production): " & strFQDN & " - IP Address: " & strIPAddress
End If
Next
Else
Wscript.Echo objHostName & " has an unknown interface type: " & objIntfType
End If
intRow = intRow + 1
Loop
Function splitRE(strSource, pattern)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern =pattern
splitRE = Split(.Replace(strSource, " "), " ")
End With
End Function
objExcel.Quit
现在唯一的问题是strFQDN变量只填充了文本文件的第一行,重复,所以比较失败:
server1-prod.fqdn.com.
【问题讨论】:
-
能否将您的数据示例上传为图片或format like this?我完全不知道你在说什么数据:(
-
什么比较失败?
strFQDN从来没有与任何东西相比,只是呼应...... -
我没有尝试过你的代码,但我看到的第一个明显错误是
strNextLine = objFile.Readline和arrList = Split(objFile.ReadLIne(),vbCrLf)。您将第一行读入 strNextLine,然后在主循环中重复使用它。您将第二行读入 arrList。您忽略文本文件的其余部分。试试arrList = Split(objFile.ReadAll(),vbCrLf)。 -
第二个错误:
Do Until objExcel.Cells(intRow,1).Value = ""。objExcel是一个 Excel 应用程序。.Cells(intRow,1).Value需要一个工作表。
标签: regex excel vbscript text-files