?
Solved

Need help with a VBS to check if a port is opened on MS Windows Server if not opened then retart the application service

Posted on 2011-05-02
14
Medium Priority
?
1,546 Views
Last Modified: 2012-05-11
Hi Experts,

I was wondering if any of you could help us out to figure this out. We need to get a script that will check if a port on a Windows server is opened, if the port is closed then the script has to restart an application service that runs as a windows service.

The target server is SERVER1 and the service is called SERVICE1.

The port is 9001. Basically a telnet to port 9001 on SERVER1 and if the connection fails then the SERVICE1 on SERVER1 needs to be reestarted.

The goal is that once we have the script we will schedule it to run in our monitoring system.

Could you please help with this one.

Thank you.

0
Comment
Question by:llarava
  • 7
  • 7
14 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35509163
Hi, you can try this VBS to see whether PortQry can tell you.
http://support.microsoft.com/kb/310099

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strPortQry = "C:\Tools\PortQryV2\PortQry.exe"
strServer = "SERVER1"
intPortNo = 9001

Set objExec = objShell.Exec(objFSO.GetFile(strPortQry).ShortPath & " -n " & strServer & " -e " &  intPortNo)
While objExec.Status = 0
	WScript.Sleep 100
Wend
strOutput = objExec.StdOut.ReadAll()
strState = Trim(Mid(strOutput, InStrRev(strOutput, ":") + 1))
If InStr(strState, "Failed to resolve name") > 0 Then
	MsgBox "Unable to resolve " & strServer
ElseIf UCase(strState) = "LISTENING" Then
	MsgBox strState
ElseIf UCase(strState) = "NOT LISTENING" Then
	MsgBox strState
	' add code here to restart the application
ElseIf UCase(strState) = "FILTERED" Then
	MsgBox strState
End If

Open in new window

0
 

Author Comment

by:llarava
ID: 35509423
Hi Rob,

Thank you so much! I am going to with it a go and I will let you know. Only one question, I just wanted to learned why haven't you used the telnet command built-in in windows?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35509489
Telnet cannot be scripted, as far as I know.  It requires commands be manually entered, and I don't like using SendKeys.

Regards,

Rob.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:llarava
ID: 35513077
Hi,

I have tried your code above adding a MsgBox("Restart the service") in case of port NOT LISTENING

The script runs with no errors but I do not get any message. I have test portqry from CLI as follows and it seems to work fine reporting NOT LISTENING.


C:\Tools\PortQryV2>portqry -n server1 -e 9001

Querying target system called:

 server1

Attempting to resolve name to IP address...


Name resolved to 172.20.110.201

querying...

TCP port 9001 (unknown service): NOT LISTENING


Can you please advise?

-----------------------

Also this is the code that I would like to add to the script above so that when the PORT IS NOT LISTENING then the service1 will be rebooted.

set args=wscript.arguments
strcomputer= "server1"
cmd = "cmd /c C:\temp\psservice.exe \\server1 restart service1"
set WshShell = WScript.CreateObject("WScript.Shell")
wshShell.run cmd

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strPortQry = "C:\Tools\PortQryV2\PortQry.exe"
strServer = "server1"
intPortNo = 9001

Set objExec = objShell.Exec(objFSO.GetFile(strPortQry).ShortPath & " -n " & strServer & " -e " &  intPortNo)
While objExec.Status = 0
	WScript.Sleep 100
Wend
strOutput = objExec.StdOut.ReadAll()
strState = Trim(Mid(strOutput, InStrRev(strOutput, ":") + 1))
If InStr(strState, "Failed to resolve name") > 0 Then
	MsgBox "Unable to resolve " & strServer
ElseIf UCase(strState) = "LISTENING" Then
	MsgBox strState
ElseIf UCase(strState) = "NOT LISTENING" Then  
	MsgBox("Restart the service")
ElseIf UCase(strState) = "FILTERED" Then
	MsgBox strState 
End If

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35517794
OK, I've added your bit of code.  If you run this now, it will show you the command, and the resulting output.  You should see at the bottom:
TCP port 9001 (unknown service): NOT LISTENING

with the status after the last colon.  That is the part the script should pick up.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strPortQry = "C:\Tools\PortQryV2\PortQry.exe"
strServer = "server1"
intPortNo = 9001

strCommand = objFSO.GetFile(strPortQry).ShortPath & " -n " & strServer & " -e " &  intPortNo
Set objExec = objShell.Exec(strCommand)
While objExec.Status = 0
	WScript.Sleep 100
Wend
strOutput = objExec.StdOut.ReadAll()
WScript.Echo "Command: " & strCommand & vbCrLf & vbCrLf & "Output: " & vbCrLf & strOutput
strState = Trim(Mid(strOutput, InStrRev(strOutput, ":") + 1))
If InStr(strState, "Failed to resolve name") > 0 Then
	MsgBox "Unable to resolve " & strServer
ElseIf UCase(strState) = "LISTENING" Then
	MsgBox strState
ElseIf UCase(strState) = "NOT LISTENING" Then  
	MsgBox "Restart the service"
	cmd = "cmd /c C:\temp\psservice.exe \\" & strServer & " restart service1"
	objShell.Run cmd, 1, True
ElseIf UCase(strState) = "FILTERED" Then
	MsgBox strState 
End If

Open in new window

0
 

Author Comment

by:llarava
ID: 35546840
Yes, this time I am getting a message box with the result of portqry which seems to be fine.

When I get the NOT LISTENING result the service doesn't restart. I have checked the code but it seems to be fine.

I have attached the lastest code that I have used.

Do you have any suggestions?

Thank you!
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strPortQry = "C:\Tools\PortQryV2\PortQry.exe"
strServer = "is-2h6j411"
intPortNo = 9001

strCommand = objFSO.GetFile(strPortQry).ShortPath & " -n " & strServer & " -e " &  intPortNo
Set objExec = objShell.Exec(strCommand)
While objExec.Status = 0
	WScript.Sleep 100
Wend
strOutput = objExec.StdOut.ReadAll()
WScript.Echo "Command: " & strCommand & vbCrLf & vbCrLf & "Output: " & vbCrLf & strOutput
strState = Trim(Mid(strOutput, InStrRev(strOutput, ":") + 1))
If InStr(strState, "Failed to resolve name") > 0 Then
	MsgBox "Unable to resolve " & strServer
ElseIf UCase(strState) = "LISTENING" Then
	MsgBox strState
ElseIf UCase(strState) = "NOT LISTENING" Then  
	MsgBox "Restart the service"
	cmd = "cmd /c C:\temp\psservice.exe \\" & strServer & " restart service1"
	objShell.Run cmd, 1, True
ElseIf UCase(strState) = "FILTERED" Then
	MsgBox strState 
End If

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35548152
Do you get the "Restart the service" message?

Have you confirmed this is correct:
cmd = "cmd /c C:\temp\psservice.exe \\" & strServer & " restart service1"

If you change the cmd /c to cmd /k does it give you an error?

Rob.
0
 

Author Comment

by:llarava
ID: 35551646
No I don't get the Do you get the "Restart the service" message just the NOT LISTENING IP resolving, etc...

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35552281
OK, after this line:
strState = Trim(Mid(strOutput, InStrRev(strOutput, ":") + 1))

can you put
MsgBox "Identified state is: " & strState

and see what you get?

Rob.
0
 

Author Comment

by:llarava
ID: 35557037
I get -- Identified STATE: NOT LISTENING

What have you proved so far with the last MsgBox?

No luck with the restart of the service... :(


 
0
 

Author Comment

by:llarava
ID: 35561424
What have you proved so far with the last MsgBox? Sorry I just see it... it is strange that the result is NOT LISTENING but it doesn't seem to do:

ElseIf UCase(strState) = "NOT LISTENING" Then  
      MsgBox "Restart the service"
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35563345
OK, the last MsgBox proved that it's at least obtaining the correct part of the output.  What I have just found out though, is that there's a line break at the end of that line.

This now removes that line break, and therefore correctly matches the status string.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strPortQry = "C:\Tools\PortQryV2\PortQry.exe"
strServer = "is-2h6j411"
intPortNo = 9001

strCommand = objFSO.GetFile(strPortQry).ShortPath & " -n " & strServer & " -e " &  intPortNo
Set objExec = objShell.Exec(strCommand)
While objExec.Status = 0
	WScript.Sleep 100
Wend
strOutput = objExec.StdOut.ReadAll()
'WScript.Echo "Command: " & strCommand & vbCrLf & vbCrLf & "Output: " & vbCrLf & strOutput
strState = Replace(Trim(Mid(strOutput, InStrRev(strOutput, ":") + 1)), vbCrLf, "")

If InStr(strState, "Failed to resolve name") > 0 Then
	MsgBox "Unable to resolve " & strServer
ElseIf UCase(strState) = "LISTENING" Then
	MsgBox strState
ElseIf UCase(strState) = "NOT LISTENING" Then  
	MsgBox "Restart the service"
	cmd = "cmd /c C:\temp\psservice.exe \\" & strServer & " restart service1"
	objShell.Run cmd, 1, True
ElseIf UCase(strState) = "FILTERED" Then
	MsgBox strState 
End If

Open in new window

0
 

Author Closing Comment

by:llarava
ID: 35690439
Dude, you are the reason why we are still keeping our subscription... -thank you-
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35694294
No problem. Thanks for the grade.  Sorry about the issues with the code.

Regards,

Rob.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question