wagnert
wagnert

Reputation: 13

VBScript - Checking if port is listening + window to notify user

I got a question regarding vbs. I need to monitor if a connection on the port 5900 is established or not. The Script should run as a service, and should check frequently if the connection is established or not. When the connection is established, it should open a small window(that has no user interaction allowed( e.g. cant be closed, has no "ok" button, etc.)), that contains sth. like "Connection established // [Hostname of the device thats connected]. If the connection has been closed, then the small window should also be closed.

ATM i got it to work to pop-up a message, if i run the script and there is an established connection

My code so far:

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

Best Regards

Upvotes: 1

Views: 2037

Answers (1)

Hackoo
Hackoo

Reputation: 18827

Try something like that : using .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
'****************************************************************

And if you like to avoid shwoing the black console; try this method :

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
'****************************************************************

Upvotes: 1

Related Questions