Link to home
Start Free TrialLog in
Avatar of Lico_w
Lico_w

asked on

How do I close a WSCript.shell object in VBScript?

I have the following code running to monitor some software:

Set oShell = CreateObject("WSCript.shell")
hidden = 0
minimised = 7
logF = 1
sCmd = "monitorSS.bat "

testPort(logF)

function testPort(logFile)
newLog = logFile + 1
'sCmd = "monitorSS.bat test abc"
'newLog = "test8910"
'Run the BAT file.
oShell.Run sCmd & newLog, 0, false 'find out what false does
oShell.quit
if newLog < 10 then _
      testPort(newLog)

end function

This calls a bat file which telnets to a port and dumps out the response to a logfile. However the connection is not closing hence the 2nd 3rd 4th etc attempts are unable to telnet on that port.

Any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SterlingMcClung
SterlingMcClung

Can we see the batch file?  I suspect that it will be best to determine why the telnet does not close and fix that first.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or maybe I'm just plainly talking rubbish about the quit method after checking MSDN, doh. Still, the rest of it may or may not help...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, if you *do* use the Exec method (objShell.Exec) you can use the Terminate method if that helps.

Set objExec = objShell.Exec("monitorSS.bat")
' ......
objExec.Terminate

Rob.
Avatar of Lico_w

ASKER

RobS using your way does terminate it, however the bat doesn't seem to run properly i.e. no response from the port and no logging???

I've attached the simple bat file for those who requested below:

@ECHO OFF
echo %1
telnet -f telnet%1.log <ip address> <port>


The issue is that once the BAT file connects via telnet it isn't quitting. I cant get it to close the connection regardless. I've tried typing quit, close, exit etc. nothing seems to break the connection other than closing the DOS window. This is what I need to resolve
I don't think that it is possible to send commands to telnet after it is started in a script like this.  Are you actually connecting to a telnet server, or a different service.  If you are not connecting to a telnet server, you could use netcat quite easily.  Something like the following:
 
nc <ip address> <port> < netcatcommands.txt > nc.log

Open in new window

You can get a windows version of netcat here:

http://joncraton.org/blog/46

I'm going to explore a slightly different approach, will report back if it yields anything useful...

~bp
Avatar of Lico_w

ASKER

Thanks bp.

Sterling my internet policy won't allow me to download this!

One way I was thinking was to open the telnet session minimised, then close the window via a script. Not sure how that could be done though....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lico_w

ASKER

Thanks for your suggestions, but I'd rather not download additional software especially as it seems it can be done through the telnet comand set. I have just found that the telnet session can be terminated manually with a CTRL + ]. However I'm struggling to do this through the script. My basic code is below, can anyone suggest what I'm doing wrong?


Set oShell = CreateObject("WSCript.shell") 

newLog = "1"
sCmd = "monitorSS.bat "

oShell.Run sCmd & newLog
oShell.SendKeys("^]")
oShell.SendKeys("quit")

Open in new window

Avatar of Lico_w

ASKER

Never mind I sussed it out. I just needed a sleep delay in there. Thanks for all imputs I'll try to be fair when awarding points
Just curious, there are a few free telnet controls that you could use directly from VBS to automate the telnet connection to your server, and then process the output right in VBS.  Before I go too far down that path, would you be able to install such a control on the server that will be running the script, or is that not allowed?

~bp