Avatar of Lico_w
Lico_w
 asked on

My VBscript goes Crazy, please help!!!!

I created the script below which simply runs a bat file to check an application is listening on a port. When the application is listening it works fine. However if the app isn't then the script goes a little crazy and opens loads of Windows.

It appears to be that the bat file it runs does not close via the sendkeys commands, and then it opens another dos window. This seems to cycle round about 30 times. Can anyone suggest what I could do to resolve this issue please? This is the final piece to a project so I would like to get completed asap.
checkSemaphor()

newLog = 1
for i = 1 to 10
	monitorSS(newLog)
	newLog = newLog + 1
next
checkLogs()
'wscript.echo "Script has completed"

Function checkSemaphor()
	'semFile = "D:\User-Applications\batch_processes\MonitorSorenson\semaphor\semaphor.txt"
	semFile = "C:\Documents and Settings\t104ahe\Desktop\monitor SS\semaphor\semaphor.txt"
	set fsObj = CreateObject("Scripting.FileSystemObject")
	if fsObj.FileExists(semFile) then
		wscript.quit
	end if
End Function

Function monitorSS(logNo)
	sCmd = "monitorSS.bat "
	Set oShell = CreateObject("WSCript.shell") 

	oShell.Run sCmd & logNo, 7
	WScript.Sleep 1000
	oShell.AppActivate "Telnet 10.210.136.218"
	oShell.SendKeys("^]")
	WScript.Sleep 1000
	oShell.AppActivate "Telnet 10.210.136.218"
	oShell.SendKeys("quit{ENTER}")
	WScript.Sleep 1000

	set oShell = nothing
End Function

Function checkLogs()
	'logDir = "D:\User-Applications\batch_processes\MonitorSorenson\logs"
	logDir = "C:\Documents and Settings\t104ahe\Desktop\monitor SS\logs"
	failures = 0
	set fso = CreateObject("Scripting.FileSystemObject")
	Set objFolder = fso.GetFolder(logDir)
	Set objFiles = objFolder.Files
	for each objFile in objFiles
		if objFile.Size < 1 then
			failures = failures + 1
			wscript.echo "File is smaller than 1000 bytes"		
		end if
		if failures > 9 then
			sendEmail()
		end if
	next
	set fso = nothing
	Set objFolder = nothing
	Set objFiles = nothing
End Function

Function sendEmail()
	'email details removed for online posting
	writeSemaphor()
End Function

Function writeSemaphor()
	'semDir = "D:\User-Applications\batch_processes\MonitorSorenson\semaphor\"
	semDir = "C:\Documents and Settings\t104ahe\Desktop\monitor SS\semaphor\"
	set semFso = CreateObject("Scripting.FileSystemObject")
	set semFile = semFso.CreateTextFile(semDir & "semaphor.txt", True)
	semFile.WriteLine("Email alert has been sent to CSI Internet team")
	set semFso = nothing
	set semFile = nothing
End Function

Open in new window

VB ScriptVisual Basic ClassicProgramming

Avatar of undefined
Last Comment
Lico_w

8/22/2022 - Mon
prashanthd

Can you post the bat file?
jorgedeoliveiraborges

Did you implement NOP? (no operation, just listen again).
Lico_w

ASKER
The bat simply does a telnet with the passed param as the logfile no (ip and port removed for security, but you can just use any ip and port which wont be listening to test):

@ECHO OFF
echo %1

telnet -f logs\telnet%1.log <ipaddress> <port>

Not sure what NOP is or how I implement it. If its the application side I.e. the app thats listening on the specified port I am unable to as it is not possible modify how this behaves.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Alan_White

As I see it, the problem is with the windows title.  

At an CMD prompt if I telnet to a known open port, the windows title of the CMD windows will be Telnet <the address>.  
If however I try to telnet to a non listening port, the window title will briefly change to Telnet <the address> but when is quicky fails, will revert back to command prompt.

Therefore when you do and appactivate looking for "telnet <the address>" it wont find the window, therefore your sendkeys to close go blowing in the wind.

My initial thought are to use oShell.Exec instead of .Run as that has a terminate function, or do some telnet scripting to kill the session, if open.  

I'll investigate more now.
Alan_White

Actually, I've just come up with another angle.  You could use PLINK instead of telnet, you have more scripting options with that and your appactivate is more likely to work.

PLINK is from the same people as Putty.
Cimperiali

or,you can use an old ugly trick:
instead of sendkeys, you could make the program who start the bat write a "stopyourself" file.
the bat file could search for that file, if found kill it  and close itself. When  the program who start the bat sees that the "stopyourself" file is disappeared, it knows the bat has ended.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Cimperiali

oppsss, sorry: it is the telnet you cannot stop,  is it?
Cimperiali

the command to send is quit followed by enter key.
Lico_w

ASKER
If you could provide an example of how to do it I'd greatly appreciate it
Your help has saved me hundreds of hours of internet surfing.
fblack61
jorgedeoliveiraborges

>> I created the script below which simply runs a bat file to check an application is listening on a port.  <<
Ok.

>> However if the app isn't then the script goes a little crazy and opens loads of Windows.
There must be another 'application is listening on a port.'
Is there any firewall?
Lico_w

ASKER
No firewall, in fact I'm running the script to monitor the same box, i.e. localhost
ASKER CERTIFIED SOLUTION
Lico_w

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Lico_w

ASKER
Fixed by me, no suitable resolution provided
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.