无法在 VBScript 中使用正则表达式
Unable to use regex in VBScript
我有一个 PuTTY 日志文件,其中捕获了所有 SSH session 的输出。我想阅读该日志文件并以所需的形式分解其内容。
在日志文件中,我所做的每个 ping 命令都有所需的模式。
我想在包含日期、时间、IP 和状态的 .CSV 文件中输出 headers 以及来自日志文件的数据。日志文件内容如下:
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2018.11.19 07:47:18 =~=~=~=~=~=~=~=~=~=~=~=
Using username "admin".
PING 172.27.1.4 (172.27.1.4) 56(84) bytes of data.
64 bytes from 172.27.1.4: icmp_req=1 ttl=64 time=1.22 ms
64 bytes from 172.27.1.4: icmp_req=2 ttl=64 time=1.05 ms
--- 172.27.1.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 1.058/1.139/1.221/0.088 ms
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2018.11.19 07:47:22 =~=~=~=~=~=~=~=~=~=~=~=
Using username "admin".
PING 172.27.1.5 (172.27.1.5) 56(84) bytes of data.
64 bytes from 172.27.1.5: icmp_req=1 ttl=64 time=1.08 ms
64 bytes from 172.27.1.5: icmp_req=2 ttl=64 time=1.04 ms
--- 172.27.1.5 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 1.041/1.061/1.081/0.020 ms
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2018.11.19 07:47:26 =~=~=~=~=~=~=~=~=~=~=~=
Using username "admin".
PING 172.27.1.6 (172.27.1.6) 56(84) bytes of data.
From 172.27.1.6 icmp_seq=1 Destination Host Unreachable
From 172.27.1.6 icmp_seq=2 Destination Host Unreachable
--- 172.27.1.6 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1004ms pipe 2
每个 IP 的完整数据将在 =~=~=~
和 ---
中找到。
如果它找到 TTL,则状态将是健康的,"Destination Host Unreachable" 或 "request timed out" 则状态将是不健康的。
已编写示例 VBScript,但它仅列出日志文件的第一个值。
Set fso = CreateObject("Scripting.FileSystemObject")
Set f=fso.opentextfile("log.txt",1)
a = f.ReadAll
Set r = New RegExp
r.Global = True
r.Multiline = True
r.IgnoreCase = True
r.Pattern = "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ((.|\n)*?)---"
Set Matches = r.Execute(a)
If Matches.Count > 0 Then Data = Matches(0).SubMatches(0)
MsgBox Data
WriteFileText "Test.txt", Data
f.Close
Function WriteFileText(sFile, Data)
Dim objFSO, oTS, sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = objFSO.CreateTextFile(sFile, 2)
oTS.WriteLine Data
oTS.Close
Set oTS = Nothing
Set objFSO = Nothing
End Function
您只得到第一个匹配项,因为您只处理第一个匹配项:
Set Matches = r.Execute(a)
If Matches.Count > 0 Then Data = <b>Matches(0)</b>.SubMatches(0)
MsgBox Data
WriteFileText "Test.txt", Data
将该代码更改为一个循环,它应该会执行您想要的操作:
Set f = fso.CreateTextFile(sFile, 2)
For Each m In r.Execute(a)
f.WriteLine m.SubMatches(0)
Next
f.Close
为片段内的 "unhealthy" 子字符串添加嵌套检查将使您能够输出状态 "healthy" 或 "not healthy".
Set re2 = New RegExp
re2.Pattern = "destination host Unreachable|request timed out"
re2.IgnoreCase = True
If re2.Test(m.SubMatches(0)) Then
WScript.Echo "Unhealthy"
Else
WScript.Echo "Healthy"
End If
或者检查 ttl=\d+
并在找到匹配时报告 "healthy",否则 "unhealthy"。
我有一个 PuTTY 日志文件,其中捕获了所有 SSH session 的输出。我想阅读该日志文件并以所需的形式分解其内容。 在日志文件中,我所做的每个 ping 命令都有所需的模式。
我想在包含日期、时间、IP 和状态的 .CSV 文件中输出 headers 以及来自日志文件的数据。日志文件内容如下:
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2018.11.19 07:47:18 =~=~=~=~=~=~=~=~=~=~=~= Using username "admin". PING 172.27.1.4 (172.27.1.4) 56(84) bytes of data. 64 bytes from 172.27.1.4: icmp_req=1 ttl=64 time=1.22 ms 64 bytes from 172.27.1.4: icmp_req=2 ttl=64 time=1.05 ms --- 172.27.1.4 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 1.058/1.139/1.221/0.088 ms =~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2018.11.19 07:47:22 =~=~=~=~=~=~=~=~=~=~=~= Using username "admin". PING 172.27.1.5 (172.27.1.5) 56(84) bytes of data. 64 bytes from 172.27.1.5: icmp_req=1 ttl=64 time=1.08 ms 64 bytes from 172.27.1.5: icmp_req=2 ttl=64 time=1.04 ms --- 172.27.1.5 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 1.041/1.061/1.081/0.020 ms =~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2018.11.19 07:47:26 =~=~=~=~=~=~=~=~=~=~=~= Using username "admin". PING 172.27.1.6 (172.27.1.6) 56(84) bytes of data. From 172.27.1.6 icmp_seq=1 Destination Host Unreachable From 172.27.1.6 icmp_seq=2 Destination Host Unreachable --- 172.27.1.6 ping statistics --- 2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1004ms pipe 2
每个 IP 的完整数据将在 =~=~=~
和 ---
中找到。
如果它找到 TTL,则状态将是健康的,"Destination Host Unreachable" 或 "request timed out" 则状态将是不健康的。
已编写示例 VBScript,但它仅列出日志文件的第一个值。
Set fso = CreateObject("Scripting.FileSystemObject")
Set f=fso.opentextfile("log.txt",1)
a = f.ReadAll
Set r = New RegExp
r.Global = True
r.Multiline = True
r.IgnoreCase = True
r.Pattern = "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ((.|\n)*?)---"
Set Matches = r.Execute(a)
If Matches.Count > 0 Then Data = Matches(0).SubMatches(0)
MsgBox Data
WriteFileText "Test.txt", Data
f.Close
Function WriteFileText(sFile, Data)
Dim objFSO, oTS, sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = objFSO.CreateTextFile(sFile, 2)
oTS.WriteLine Data
oTS.Close
Set oTS = Nothing
Set objFSO = Nothing
End Function
您只得到第一个匹配项,因为您只处理第一个匹配项:
Set Matches = r.Execute(a)
If Matches.Count > 0 Then Data = <b>Matches(0)</b>.SubMatches(0)
MsgBox Data
WriteFileText "Test.txt", Data
将该代码更改为一个循环,它应该会执行您想要的操作:
Set f = fso.CreateTextFile(sFile, 2)
For Each m In r.Execute(a)
f.WriteLine m.SubMatches(0)
Next
f.Close
为片段内的 "unhealthy" 子字符串添加嵌套检查将使您能够输出状态 "healthy" 或 "not healthy".
Set re2 = New RegExp
re2.Pattern = "destination host Unreachable|request timed out"
re2.IgnoreCase = True
If re2.Test(m.SubMatches(0)) Then
WScript.Echo "Unhealthy"
Else
WScript.Echo "Healthy"
End If
或者检查 ttl=\d+
并在找到匹配时报告 "healthy",否则 "unhealthy"。