VBScript - 检查端口是否正在侦听 + window 以通知用户
VBScript - Checking if port is listening + window to notify user
我有一个关于 vbs 的问题。
我需要监控端口 5900 上的连接是否建立。
脚本应该 运行 作为一项服务,并且应该经常检查连接是否建立。
建立连接后,它应该打开一个小的 window(不允许用户交互(例如不能关闭,没有 "ok" 按钮等)),其中包含某物。像“已建立连接// [已连接设备的主机名]。
如果连接已经关闭,那么小window也应该关闭。
ATM 我可以弹出消息,如果我 运行 脚本并且已建立连接
到目前为止我的代码:
Dim ObjExec
Dim strFromProc
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c netstat -a | find ""ESTABLISHED"" | find "":5900""" )
Do Until ObjExec.Stdout.atEndOfStream
strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
WScript.Echo "Connection Established"
Loop
此致
尝试类似的方法:使用 .Popup method
Option Explicit
Dim ObjExec,objShell,strFromProc,intButton,Port
Port = "5900"
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c netstat -a | find "& DblQuote("ESTABLISHED") & "| find " & DblQuote(Port) &"")
strFromProc = ObjExec.StdOut.ReadAll
If Instr(strFromProc,"ESTABLISHED") > 0 Then
intButton = objShell.Popup(strFromProc,3,"Connection Established @ Port "& Port &"",vbInformation)
Else
intButton = objShell.Popup("Connection Not Established @ Port "& Port,3,"Connection Not Established @ Port "& Port &"",vbExclamation)
End If
'****************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
如果您想避免显示黑色控制台;试试这个方法:
Option Explicit
Dim ObjExec,strCommand,OutPutData,objShell,strFromProc,intButton,Port
Port = "5900"
Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c netstat -a | find "& DblQuote("ESTABLISHED") & "| find " & DblQuote(Port) &""
OutPutData = Run_Cmd(strCommand)
If Instr(OutPutData,"ESTABLISHED") > 0 Then
intButton = objShell.Popup(OutPutData,3,"Connection Established @ Port "& Port &"",vbInformation)
Else
intButton = objShell.Popup("Connection Not Established @ Port "& Port,3,"Connection Not Established @ Port "& Port &"",vbExclamation)
End If
'****************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
Function Run_Cmd(strCommand)
On Error Resume Next
Const ForReading = 1
Const TemporaryFolder = 2
Const WshHide = 0
Dim wsh, fs, ts
Dim strTempFile,strFile, strData
Set wsh = CreateObject("Wscript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
strTempFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, fs.GetTempName)
strFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, "result.txt")
wsh.Run "cmd.exe /c " & strCommand & " > " & DblQuote(strTempFile) & "2>&1", WshHide, True
wsh.Run "cmd.exe /u /c Type " & DblQuote(strTempFile) & " > " & DblQuote(strFile) & "", WshHide, True
Set ts = fs.OpenTextFile(strFile, ForReading,true,-1)
strData = ts.ReadAll
Run_Cmd = strData
ts.Close
fs.DeleteFile strTempFile
fs.DeleteFile strFile
End Function
'****************************************************************
我有一个关于 vbs 的问题。 我需要监控端口 5900 上的连接是否建立。 脚本应该 运行 作为一项服务,并且应该经常检查连接是否建立。 建立连接后,它应该打开一个小的 window(不允许用户交互(例如不能关闭,没有 "ok" 按钮等)),其中包含某物。像“已建立连接// [已连接设备的主机名]。 如果连接已经关闭,那么小window也应该关闭。
ATM 我可以弹出消息,如果我 运行 脚本并且已建立连接
到目前为止我的代码:
Dim ObjExec
Dim strFromProc
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c netstat -a | find ""ESTABLISHED"" | find "":5900""" )
Do Until ObjExec.Stdout.atEndOfStream
strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
WScript.Echo "Connection Established"
Loop
此致
尝试类似的方法:使用 .Popup method
Option Explicit
Dim ObjExec,objShell,strFromProc,intButton,Port
Port = "5900"
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c netstat -a | find "& DblQuote("ESTABLISHED") & "| find " & DblQuote(Port) &"")
strFromProc = ObjExec.StdOut.ReadAll
If Instr(strFromProc,"ESTABLISHED") > 0 Then
intButton = objShell.Popup(strFromProc,3,"Connection Established @ Port "& Port &"",vbInformation)
Else
intButton = objShell.Popup("Connection Not Established @ Port "& Port,3,"Connection Not Established @ Port "& Port &"",vbExclamation)
End If
'****************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
如果您想避免显示黑色控制台;试试这个方法:
Option Explicit
Dim ObjExec,strCommand,OutPutData,objShell,strFromProc,intButton,Port
Port = "5900"
Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c netstat -a | find "& DblQuote("ESTABLISHED") & "| find " & DblQuote(Port) &""
OutPutData = Run_Cmd(strCommand)
If Instr(OutPutData,"ESTABLISHED") > 0 Then
intButton = objShell.Popup(OutPutData,3,"Connection Established @ Port "& Port &"",vbInformation)
Else
intButton = objShell.Popup("Connection Not Established @ Port "& Port,3,"Connection Not Established @ Port "& Port &"",vbExclamation)
End If
'****************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
Function Run_Cmd(strCommand)
On Error Resume Next
Const ForReading = 1
Const TemporaryFolder = 2
Const WshHide = 0
Dim wsh, fs, ts
Dim strTempFile,strFile, strData
Set wsh = CreateObject("Wscript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
strTempFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, fs.GetTempName)
strFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, "result.txt")
wsh.Run "cmd.exe /c " & strCommand & " > " & DblQuote(strTempFile) & "2>&1", WshHide, True
wsh.Run "cmd.exe /u /c Type " & DblQuote(strTempFile) & " > " & DblQuote(strFile) & "", WshHide, True
Set ts = fs.OpenTextFile(strFile, ForReading,true,-1)
strData = ts.ReadAll
Run_Cmd = strData
ts.Close
fs.DeleteFile strTempFile
fs.DeleteFile strFile
End Function
'****************************************************************