Keep Windows Script in same Command Window
I have the following script which does the following on a Virtuozzo hardware node. : Get Running VEs : Prompt for VEID : Get Established Connections on Port 3389 for Specified VEID : Prompt for IP to Block : Create IP Security Policy to Block Specified IP : Reset Terminal 开发者_如何转开发Sessions
So everything works the way I want except I can't figure out how to have everything occur in a single cmd prompt rather than spawning new command prompts. If I remove the cmd /K from the exec commands then nothing is displayed. Am I missing something simple?
Set oShell = CreateObject ("Wscript.Shell")
getrunningvelistcmd = "cmd /K echo 'List Running VEs' & vzlist -a | find ""running"""
getrunningvelist = oShell.run (getrunningvelistcmd,1,false)
strVEID = InputBox("Enter VEID","Enter VEID")
whosethebrutecmd = "cmd /K echo 'Whose the Brute' & vzctl exec "&strVEID & " netstat -ano | find "":3389"""
whosethebrute = oShell.run (whosethebrutecmd,1,false)
strIP = InputBox("Enter IP Address to Block","Enter IP Address to Block")
blockcmd = "cmd /K echo 'Create Policy' & vzctl exec "&strVEID & " netsh ipsec static add policy description=""Block Rule"" name=""Blocked Traffic"" && echo. && echo 'Create Filter List' && vzctl exec "&strVEID & " netsh ipsec static add filterlist name=""IP Block List"" && echo. && echo 'Create Filter' && vzctl exec "&strVEID & " netsh ipsec static add filter filterlist=""IP Block List"" srcaddr="&strIP & " dstaddr=any description=""Hacker IP"" && echo. && echo 'Define Filter Action' && vzctl exec "&strVEID & " netsh ipsec static add filteraction name=""Block"" action=block && echo. && echo 'Add Rule to Filter Action' && vzctl exec "&strVEID & " netsh ipsec static add rule name=""Block Rule"" policy=""Blocked Traffic"" filterlist=""IP Block List"" filteraction=""Block"" activate=yes && echo. && echo 'Assign Policy' && vzctl exec "&strVEID & " netsh ipsec static set policy name=""Blocked Traffic"" assign=yes && echo. && echo. && echo 'Reset Sessions PRESS ENTER' && vzctl exec "&strVEID & " rwinsta rdp-tcp && echo. && echo 'You Terminated the Brute!'"
blockthebrute = oShell.run (blockcmd,1,false)
set oShell = Nothing
WScript.Quit(0)
The 'programmatically correct way' to solve your problem is to write a .hta GUI. But if you are willing to use cscript to run your script, you may get an acceptable UI without to much work. As I don't have your *ware, this POC script:
' bstep00.vbs - keep-windows-script-in-same-command-window
Option Explicit
Dim oWSH : Set oWSH = CreateObject( "WScript.Shell" )
Dim sCmd, oEx, sFN, iRet
sCmd = "fkdir.bat"
Set oEx = oWSH.Exec( sCmd )
If Not oEx.StdOut.AtEndOfStream Then
WScript.Echo oEx.StdOut.ReadAll()
WScript.StdOut.Write "file to type: "
sFN = WScript.StdIn.ReadLine()
sCmd = "fktype.bat " & sFN
Set oEx = oWSH.Exec( sCmd )
If Not oEx.StdOut.AtEndOfStream Then
WScript.Echo oEx.StdOut.ReadAll()
Else
WScript.Echo "(1) oEx.StdOut.AtEndOfStream"
End If
Else
WScript.Echo "(0) oEx.StdOut.AtEndOfStream"
End If
WScript.Echo "Done."
uses dir
(get/show info) and type
(use user's input based on info) to demonstrate the components: .Exec and .bat files (like
fktype.bat:
@echo off
type %1
). Using .bat (or .cmd) files solves 2 problems: "complex command lines" and "%comspec% starting", so don't be tempted to avoid them by putting more text into sCmd.
Output of sample session:
cscript bstep00.vbs
07.10.2011 13:48 29 fkdir.bat
07.10.2011 14:01 20 fktype.bat
file to type: fkdir.bat
@echo off
dir | find "bat"
Done.
精彩评论