windows - capture command line output in vbscript -
got simple script executes command server - briefly:
//create shell set wshshell=createobject("wscript.shell") wshshell.run "cmd.exe" //send commands wshshell.sendkeys "telnet ip_address" wshshell.sendkeys "dir" server offers feedback want capture. need capture first line variable, , print variable out confirm.
can help? thanks.
do not use windows telnet client automation purposes. telnet client ships windows made interactive use only.
i'd use plink (from putty suite) in batch mode this:
plink.exe -telnet -batch ip_address dir the tool doesn't require installation, can deploy alongside script.
run either in batch file using head/tail, or in vbscript using exec method, can read stdout:
addr = "62.39.x.x" port = 24 timeout = 300 'seconds timedout = false cmdline = "echo ""mute near get"" | plink.exe -telnet -batch " & addr & " -p " & port set sh = createobject("wscript.shell") 'change working directory directory containing script , plink executable set fso = createobject("scripting.filesystemobject") sh.currentdirectory = fso.getparentfoldername(wscript.scriptfullname) 'wait until command completes or timeout expires expiration = dateadd("s", timeout, now) set cmd = sh.exec("%comspec% /c " & cmdline) while cmd.status = 0 , < expiration wscript.sleep 100 loop if cmd.status = 0 cmd.terminate timedout = true end if wscript.echo cmd.stdout.readall if cmd.exitcode <> 0 wscript.echo "command terminated exit code " & cmd.exitcode & "." wscript.echo cmd.stderr.readall wscript.quit 1 elseif timedout wscript.echo "command timed out." wscript.echo cmd.stderr.readall wscript.quit 2 end if
Comments
Post a Comment