我需要将电子表格中的值与包含区域传输输出的文本文件进行比较,以根据电子表格中列出的类型获取特定接口的IP地址。
我的输入:
+----------+------------+ | Name | Int type | +----------+------------+ | Switch-1 | | | SERVER1 | Production | | SERVER2 | OOB | | Switch-2 | | | SERVER3 | Production | | SERVER4 | Other | +----------+------------+
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 name: Switch-1 Interface name: server1-prod.fqdn.com - IP Address: 192.168.0.10 Interface name: server2-oob.fqdn.com - IP Address: 192.168.0.11 Switch name: Switch-2 Interface name: server3-prod.fqdn.com - IP Address: 192.168.0.12 server4-foo.fqdn.com Unknown interface type: Other.
我目前的代码:
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。
答案 0 :(得分:1)
您的代码很复杂,没有明显的原因。例如,为什么宏打开工作簿“C:\ Server-List-by-Switch.xlsx”而不是坐在其中?
我不知道Wscript.Echo
做了什么,所以使用Debug.Print输出到立即窗口。我不喜欢在每个变量的开头都有一个三字符类型标识符。但如果使用,它们应该是正确的。所以我认为objFile
是可以的,因为它是一个对象,但是objHostName
是错误的,因为它是一个字符串。
我简化了你的代码但保留了结构。这意味着此代码无法创建输出行server4-foo.fqdn.com Unknown interface type: Other.
,因为您的代码验证Excel表而不是文本文件。代码可以重新排序,但您需要更清楚地了解数据的性质。
我在工作簿中有一个工作表“Server-List-by-Switch”。它包含:
| A | B |
1 | Name | Intf type |
2 | Switch-1 | |
3 | SERVER1 | Production|
4 | SERVER2 | OOB |
5 | Switch-2 | |
6 | SERVER3 | Production|
7 | SERVER4 | Other |
宏的输出是:
Switch Name: Switch-1
Interface name: server1-prod.fqdn.com - IP Address: 192.168.0.10
Interface name: server2-oob.fqdn.com - IP Address: 192.168.0.13
Switch Name: Switch-2
Interface name: server3-prod.fqdn.com - IP Address: 192.168.0.14
SERVER4 has an unknown interface type: Other
尽可能接近,无需重新排序代码。
Option Explicit
Sub GenIPAddressReport()
Dim arrList() As String
Dim FQDN As String
Dim FQDNPart() As String
Dim HostName As String
Dim IntfType As String
Dim intRow As Long
Dim IPAddress As String
Dim Line As Variant
Dim LinePart() As String
Dim objFile As Object
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(ActiveWorkbook.Path & "\FQDN-and-IP.txt")
arrList = Split(objFile.ReadAll(), vbCrLf)
objFile.Close
Set objFile = Nothing
'For Each Line In arrList
' Debug.Print Line
'Next
intRow = 2
With Worksheets("Server-List-by-Switch")
Do Until .Cells(intRow, 1).Value = ""
HostName = .Cells(intRow, 1).Value
IntfType = .Cells(intRow, 2).Value
If InStr(1, HostName, "Switch-") > 0 Then
Debug.Print "Switch Name: " & HostName
ElseIf InStr(1, HostName, "Switch-") = 0 And _
InStr(1, IntfType, "Production") > 0 Then
For Each Line In arrList
FQDN = LCase(HostName & "-prod.fqdn.com")
LinePart = Split(LCase(Line), " ")
If FQDN = LinePart(0) Then
Debug.Print "Interface name: " & LinePart(0) & _
" - IP Address: " & LinePart(UBound(LinePart))
Exit For
End If
Next
ElseIf InStr(1, HostName, "Switch-") = 0 And _
InStr(1, IntfType, "OOB") > 0 Then
For Each Line In arrList
FQDN = LCase(HostName & "-oob.fqdn.com")
LinePart = Split(LCase(Line), " ")
If FQDN = LinePart(0) Then
Debug.Print "Interface name: " & LinePart(0) & _
" - IP Address: " & LinePart(UBound(LinePart))
Exit For
End If
Next
Else
Debug.Print HostName & " has an unknown interface type: " & IntfType
End If
intRow = intRow + 1
Loop
End With
End Sub
答案 1 :(得分:0)
只是猜测 - 替换两者
strFQDN = splitRE (strNextLine, "\s+")
with
strFQDN = splitRE (line, "\s+")
strNextLine
,现在它始终是第一行..