VBS 使用 RegEx 返回命令行返回的一部分
我正在尝试通过 ssh 执行命令,仅获取返回命令的第一部分,并将其设置为要使用的变量。我正在尝试使用 RegExp,因为我只需要前 4 位数字,所以我使用模式代码“^\d{1,4}”,我可以通过此代码成功获得整个返回;
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
Do Until oExec.StdOut.AtEndOfStream
ID = oExec.StdOut.ReadLine
Loop
WScript.Echo ID
但是现在,当我尝试使用 RegExp 和 echo 来查看是否得到我想要的内容时,我收到“类型不匹配”错误。
Set RegExp = New RegExp
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
RegExp.Pattern = "^\d{1,4}"
Do Until oExec.StdOut.AtEndOfStream
ID = oExec.StdOut.ReadLine
WScript.Echo RegExp.execute(ID)
Loop
如果有人对代码有问题有任何煽动,请告诉我。如果您知道任何替代方案,我将不胜感激。使用 shell 脚本我可以得到我正在寻找的东西,但我需要在 Windows 上运行这个 VBS。这是 Shell 脚本(如果有人可以将其翻译为 VBS)。
.............for i in `command | sed ‘1d’ | awk ‘{print $1}’`..............
提前感谢任何帮助...我在这个问题上绞尽脑汁有一段时间了,然后精疲力竭。
I'm trying to execute a command via ssh, take only the first part of the returned command, and set it as a variable to be used. I'm attempting to use RegExp and because I only need the first 4 digits, I use the pattern code "^\d{1,4}" I can successfully get the entire return via this code;
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
Do Until oExec.StdOut.AtEndOfStream
ID = oExec.StdOut.ReadLine
Loop
WScript.Echo ID
But Now when I try to use RegExp and echo to see if I'm getting what I want I get a "Type Mismatch" error
Set RegExp = New RegExp
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
RegExp.Pattern = "^\d{1,4}"
Do Until oExec.StdOut.AtEndOfStream
ID = oExec.StdOut.ReadLine
WScript.Echo RegExp.execute(ID)
Loop
If anyone has any incite on what's wrong with the code please let me know. If you know of any alternative I'd appreciate it. Using a shell script I can get what I'm looking for but I need to run this VBS with windows. Here's the Shell script if anyone can translate it to VBS.
.............for i in `command | sed ‘1d’ | awk ‘{print $1}’`..............
Appreciate any help in advance... Been racking my brain on this one for a while and getting burnt out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能 .Echo 一个对象,例如从 .Execute 获得的匹配集合;访问其第一项的 .Value:
You can't .Echo an object like the match collection you get from .Execute; access the .Value of its first item: