I have written script to connect from my local machine to Jump server using secure crt. i was able to execute the script and capture the output into Msgbox, But unable to print it to console.
Below is the code which i have written.
#$language = "VBScript"
#$interface = "1.0"
Set objTab = crt.GetScriptTab
Set g_shell = CreateObject("WScript.Shell")
Public szData
crt.Sleep 6000
objTab.Screen.IgnoreEscape = True
objTab.Screen.Synchronous = True
Dim szCommand, szPrompt, nRow, szLogFileName, nIndex
Do
bCursorMoved = objTab.Screen.WaitForCursor(1)
Loop until bCursorMoved = False
nRow = objTab.Screen.CurrentRow
szPrompt = objTab.screen.Get(nRow, _
0, _
nRow, _
objTab.Screen.CurrentColumn - 1)
szPrompt = Trim(szPrompt)
crt.Screen.Synchronous = True
Dim pran
Sub Main
strVal = crt.Arguments(0)
crt.Screen.Send "pwd" & chr(13)
crt.Screen.WaitForString "[xyz#xlpv0002 ~]$"
crt.Screen.Send "sh test.sh" & chr(9) & chr(13)
**szData = CaptureOutputOfCommand("sh test.sh", "[xyz#xlpv0002 ~]") & vbCr**
'MsgBox(szData)
pran = szData
crt.Clipboard.Format = "CF_TEXT"
crt.Clipboard.Text = pran
crt.Dialog.MessageBox("Text is now in the clipboard: \n\n" + crt.Clipboard.Text)
'MessageBox.Show(szData)
If Not SendExpect("exit", "[xyz#xlpv0002 ~]") then exit sub
g_shell.Run "%comspec% /c taskkill /IM SecureCRT.exe /F"
End Sub
'=======================================================================
Function CaptureOutputOfCommand(szCommand, szPrompt)
if crt.Session.Connected <> True then
CaptureOutputOfCommand = "[ERROR: Not Connected.]"
exit function
end if
objTab.Screen.Send szCommand & vbcr
objTab.Screen.WaitForString vbcr
CaptureOutputOfCommand = objTab.Screen.ReadString(szPrompt)
End Function
'======================================================================
Function SendExpect(szSend, szExpect)
if objTab.Session.Connected <> True then exit function
objTab.Screen.Send szSend & vbcr
objTab.Screen.WaitForString szExpect
SendExpect = True
End Function
'========================================
I am capturing the script output into szData variable. Is there a way to print the same in the console?
You are already using the command
crt.Screen.Send
You just need to pass a valid command that will output to the console, if doing this on a Windows Server you should be able to use the Command Line program ECHO to output to the console.
crt.Screen.Send "echo Display in console!!!"
Output (untested, but should produce)
Display in console!!!
So you should just be able to do
crt.Screen.Send "echo " & szData
Useful Links
Scripting Essentials:
A Guide to Using
VBScript in SecureCRT (official documentation)
Related
I am writing a program in VBScript to automate the process of file encryption, and am struggling with a problem.
I want to test which code the script will execute based on whether a file comparison returns an errorlevel of 0 or 1. (For simplicity, I cut out that code from this post.) Google searches have pointed me to the following to start the process of modifying one of the comparison files for this purpose.
Set testFile = fso.OpenTextFile(testDestFile, 8, False, 0)
However, VBScript always throws a "File not found" error for that line unless I put
WScript.Echo "testDestFile is '" & testDestFile & "'..."
right before it.
I don't want that, because the script's actions should be invisible to the user unless necessary. When I run this script, I can see in Windows Explorer that it creates the file represented by testDestFile. What am I doing wrong?
Option Explicit
Dim baseDirLen, compareOpts, decryptOpts, destDataPath, destFolder, _
destFolderPath, encDestFile, encryptorPath, encryptOpts, file, fileName, _
folder, folderEnd, fso, keyPath, oShell, srcDataPath, srcDirEndLen, _
srcFolder, strErrorCode, testDestFile, testDiff, testFile, t
Set oShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
srcDataPath = "e:\EZcrypt\TargetData"
keyPath = "e:\EZcrypt\Key\Demo010719.key.bin"
destDataPath = "E:\EZcrypt\EncryptedData"
encryptorPath = "E:\OpenSSL-Win32\bin\openssl"
Set srcFolder = fso.GetFolder(srcDataPath)
baseDirLen = Len(srcDataPath)
recurseFolders(srcFolder)
Sub recurseFolders(srcFolder)
For Each folder In srcFolder.subfolders
srcDirEndLen = (Len(folder) - baseDirLen - 1)
folderEnd = Right(folder, srcDirEndLen)
destFolderPath = destDataPath & "\" & folderEnd & "\"
If Not fso.FolderExists(destFolderPath) Then
fso.CreateFolder(destFolderPath)
End If
For Each file In folder.Files
fileName = fso.GetFileName(file)
testDestFile = destFolderPath & "test." & fileName
encDestFile = destFolderPath & fileName & ".enc"
If Not fso.FileExists(encDestFile) Then
strErrorCode = ""
encryptOpts = encryptorPath & " enc -aes-256-cbc -salt -in """ & _
file & """ -out """ & encDestFile & _
""" -pass file:""" & keyPath & """ -pbkdf2"
oShell.Run (encryptOpts)
decryptOpts = encryptorPath & " enc -d -aes-256-cbc -in """ & _
encDestFile & """ -out """ & testDestFile & _
""" -pass file:""" & keyPath & """ -pbkdf2"
oShell.Run (decryptOpts)
WEcript.Echo "testDestFile is '" & testDestFile & "'..."
Set testFile = fso.OpenTextFile(testDestFile, 8, False, 0)
Else
WScript.Echo "'" & encDestFile & "' exists. Skipping..."
End If
Next
recurseFolders(folder)
Next
End Sub
The most likely reason for the behavior you observed is that the openssl commands you run right before trying to open that file (specifically the encryption command, which appears to be creating the file) haven't finished yet. You don't tell the Run method to wait for the commands to return, so they're running asynchronously in the background. Presumably the WScript.Echo adds just enough delay for the encryption to finish before the code proceeds to opening the file. Using WScript.Sleep instead of echoing something would probably have had the same effect.
To fix the issue, wait for the external commands to return.
Replace these lines:
encryptOpts = encryptorPath & ...
oShell.Run (encryptOpts)
decryptOpts = encryptorPath & ...
oShell.Run (decryptOpts)
with this:
encryptOpts = encryptorPath & ...
oShell.Run encryptOpts, 0, True
decryptOpts = encryptorPath & ...
oShell.Run decryptOpts, 0, True
It's also good practice to check the exit status of external commands, so you can see if something went wrong:
rc = oShell.Run(encryptOpts, 0, True)
If rc <> 0 Then
'an error occurred
End If
I am preparing vbscript to run bat files where bat file name contains version number, Script have to search for bat file with matching version name . Once Ran,The result Text file will be generated with same version number. I have to search for Text file with same version number and read the txt file for error or msg, if error found i want to display it.
I know my requirement is little too much.
But I am finding little difficulty in debugging the code.I am stuck.
Can any one help me to resolve the issue.
Thank You
here is my script,
I am passing values for sql query from excel sheet.
Dim Ver, Version_Number
Dim con
Dim rs
Set con=createobject("adodb.connection")
Set rs=Createobject("adodb.recordset")
Set PinXL = CreateObject("Excel.application")
Set PinWB = PinXL.Workbooks.Open("C:\maspects\Trial.xls") '// Login and Enable Debug Window in application
Set PinWS = PinWB.Worksheets("Sheet1")
varr = Cstr(PinWS.Cells(2,1).Value)
varr1 = trim(varr)
usename = Cstr(PinWS.Cells(2,2).Value)
UN = trim(usename)
password = Cstr(PinWS.Cells(2,3).Value)
PWD = trim(password)
IrisDB = Cstr(PinWS.Cells(2,4).Value)
DB = trim(IrisDB)
Site_Name = Cstr(PinWS.Cells(2,5).Value)
Site = trim(Site_Name)
con.open"provider=sqloledb.1;server=" & varr1 & ";uid=" & UN & ";pwd=" & PWD & ";database=" & DB &""
Wscript.sleep 1000*3
rs.Open "select * from tblSettingsUnique where [Setting Name] like '%Revision%'" ,con
Wscript.sleep 1000*2
Version_Number = rs.Fields("Setting Value")
Ver = Version_Number
msgbox Ver
Call Execute
PinWB.Save
PinWB.Close
PinXL.Quit
Wscript.Quit
Function Execute
If Ver < PinWS.Cells(2,6).Value = "True" Then ' if Ver is less than Cell(2,6) value then application should come out of loop
For i = Ver to PinWS.Cells(2,6).Value step 1
msgbox i
Set WShell = CreateObject("WScript.Shell")
For each f in Wshell.Getfolder("C:\maspects\DB_script").Files
BatFile = instr(f.File , "4_0_"&i )
WShell.Run ("CMD /K C:\maspects\DB_script\"&batFile &".bat" & Varr1 &" "& UN &" "& password )
Call msg
Next
Next
End If
End Function
Function msg
Set TxtObject = CreateObject("scripting.FileSystemObject")
For each ResFile in TxtObject.GetFolder("C:\maspects\DB_script").Files
TargetFile = InStr(ResFile.File , "4_0_"&i )
Set TxtFile = TxtObject.openTextFile("C:\maspects\DB_script\"&TargetFile, 1 ,true)
Do until TxtFile.AtEndOfStream
For each F in TxtFile.Readline
if InStr (F,"msg" ) = "True" and InStr (F,"msg 207" )= "False" Then
msgbox "Error in:"&TargetFile
React =Cint(Inputbox("Go through the Result file:"&TargetFile &"Enter '1' to Continue '0' to Quit"))
If React = "0" Then
TextObject.Close
Wscript.Quit
End if
End If
Next
Loop
TextObject.Close
Next
End Function
I have a function that pings computers from an excel list and gets the ping value of them.
While the script was running, the excel was completely unresponsive. I could fix this with DoEvents, this made it a bit more responsive.
However, the problem starts when the function gets to an offline computer. While it waits for the response of the offline PC, Excel freezes again and the script does not jump to the next PC until it gets the "timeout" from the actual one.
As the default ping timeout value is 4000ms, if I have 100 computers in my list, and 50 of them are turned off, that means I have to wait an extra 3,3 minutes for the script to finish, and also blocks the entire Excel, making it unusable for the duration.
My question is, if is there any way to make this faster or more responsive or smarter?
The actual code:
Function:
Function sPing(sHost) As String
Dim oPing As Object, oRetStatus As Object
Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & sHost & "'")
DoEvents
For Each oRetStatus In oPing
DoEvents
If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
sPing = "timeout" 'oRetStatus.StatusCode <- error code
Else
sPing = sPing & vbTab & oRetStatus.ResponseTime
End If
Next
End Function
Main:
Sub pingall_Click()
Dim c As Range
Dim p As String
Dim actives As String
actives = ActiveSheet.Name
StopCode = False
Application.EnableCancelKey = xlErrorHandler
On Error GoTo ErrH:
DoEvents
For Each c In Sheets(actives).UsedRange.Cells
If StopCode = True Then
Exit For
End If
DoEvents
If Left(c, 7) = "172.21." Then
p = sPing(c)
[...]
End If
Next c
End Sub
As already noted in the comments, to prevent this from blocking after each call, you need to invoke your pings asynchronously from your function. The way I would approach this would be to delegate your sPing(sHost) function to a VBScript that you create on the fly in a temp folder. The script would look something like this, and it takes the IP address as a command line argument and outputs the result to a file:
Dim args, ping, status
Set ping = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & Wscript.Arguments(0) & "'")
Dim result
For Each status In ping
If IsNull(status.StatusCode) Or status.StatusCode <> 0 Then
result = "timeout"
Else
result = result & vbTab & status.ResponseTime
End If
Next
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile(Wscript.Arguments(0), True)
file.Write result
file.Close
You can create a Sub to write this to a path something like this:
Private Sub WriteScript(path As String)
Dim handle As Integer
handle = FreeFile
Open path & ScriptName For Output As #handle
Print #handle, _
"Dim args, ping, status" & vbCrLf & _
"Set ping = GetObject(""winmgmts:{impersonationLevel=impersonate}"").ExecQuery _" & vbCrLf & _
" (""select * from Win32_PingStatus where address = '"" & Wscript.Arguments(0) & ""'"")" & vbCrLf & _
"Dim result" & vbCrLf & _
"For Each status In ping" & vbCrLf & _
" If IsNull(status.StatusCode) Or status.StatusCode <> 0 Then" & vbCrLf & _
" result = ""timeout""" & vbCrLf & _
" Else" & vbCrLf & _
" result = result & vbTab & status.ResponseTime" & vbCrLf & _
" End If" & vbCrLf & _
"Next" & vbCrLf & _
"Dim fso, file" & vbCrLf & _
"Set fso = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf & _
"Set file = fso.CreateTextFile(Wscript.Arguments(0), True)" & vbCrLf & _
"file.Write result" & vbCrLf & _
"file.Close"
Close #handle
End Sub
After that, it's pretty straightforward - create a new directory in the user's temp directory, plop the script in there, and then use the Shell command to run each ping in its own process. Wait for the length of your timeout, then read the results from the files:
Private Const TempDir = "\PingResults\"
Private Const ScriptName As String = "ping.vbs"
'Important - set this to the time in seconds of your ping timeout.
Private Const Timeout = 4
Sub pingall_Click()
Dim sheet As Worksheet
Set sheet = ActiveSheet
Dim path As String
'Create a temp folder to use.
path = Environ("Temp") & TempDir
MkDir path
'Write your script to the temp folder.
WriteScript path
Dim results As Dictionary
Set results = New Dictionary
Dim index As Long
Dim ip As Variant
Dim command As String
For index = 1 To sheet.UsedRange.Rows.Count
ip = sheet.Cells(index, 1)
If Len(ip) >= 7 Then
If Left$(ip, 1) = "172.21." Then
'Cache the row it was in.
results.Add ip, index
'Shell the script.
command = "wscript " & path & "ping.vbs " & ip
Shell command, vbNormalFocus
End If
End If
Next index
Dim completed As Double
completed = Timer + Timeout
'Wait for the timeout.
Do While Timer < completed
DoEvents
Loop
Dim handle As String, ping As String, result As String
'Loop through the resulting files and update the sheet.
For Each ip In results.Keys
result = Dir$(path & ip)
If Len(result) <> 0 Then
handle = FreeFile
Open path & ip For Input As #handle
ping = Input$(LOF(handle), handle)
Close #handle
Kill path & ip
Else
ping = "timeout"
End If
sheet.Cells(results(ip), 2) = ping
Next ip
'Clean up.
Kill path & "*"
RmDir path
End Sub
Note that this has exactly zero error handling for the file operations, and doesn't respond to your StopCode flag. It should give the basic gist of it though. Also note that if you need to allow the user to cancel it, you won't be able to remove the temp directory because it will still be in use. If that is the case, only create it if it isn't already there and don't remove it when you're done.
You might be able to implement something like this, but I haven't tried it with multiple servers
if your network is fast you can reduce the timeout to 500 ms or less:
.
Public Function serverOk(ByVal dbSrvrNameStr As String) As Boolean
Const PINGS As Byte = 1
Const PING_TIME_OUT As Byte = 500
Const PING_LOCATION As String = "C:\Windows\System32\"
Dim commandResult As Long, serverIsActive As Boolean
commandResult = 1
serverIsActive = False
If Len(dbSrvrNameStr) > 0 Then
Err.Clear
With CreateObject("WScript.Shell")
commandResult = .Run("%comspec% /c " & PING_LOCATION & "ping.exe -n " & PINGS & " -w " & PING_TIME_OUT & " " & dbSrvrNameStr & " | find ""TTL="" > nul 2>&1", 0, True)
commandResult = .Run("%comspec% " & PING_LOCATION & "/c ping.exe -n " & PINGS & " -w " & PING_TIME_OUT & " " & dbSrvrNameStr, 0, True)
serverIsActive = (commandResult = 0)
End With
If serverIsActive And Err.Number = 0 Then
'"DB Server - valid, Ping response: " & commandResult
Else
'"Cannot connect to DB Server, Error: " & Err.Description & ", Ping response: " & commandResult
End If
Err.Clear
End If
serverOk = serverIsActive
End Function
.
Link to "Run Method (Windows Script Host)" from Microsoft:
https://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
The 3rd parameter of this command can be overlooked: "bWaitOnReturn" - allows you to execute it asynchronously from VBA
This is what I have so far. It works; outputing the folder path to temp to a text file.
What I really want, is to output the data to a variable. Every example I see online, show how to do this using something like:
set objScriptExec = wshShell.Exec (strCommand)
followed by
strresult = LCase(objScriptExec.StdOut.ReadAll. // code
I want this to run with Run, not Exec, because I want the command prompt windows to be hidden as I will performing many commands with the code below. How can I capture that output to a variable?
Set wsShell = CreateObject("WScript.Shell")
strCommand = "cmd /c echo %temp% > %temp%\test.txt"
wsShell.Run strcommand,0,True
This may be done with the Windows Script Host Exec command. StdOut, StdIn, and StdErr may all be accessed, and ERRORLEVEL is available when the command completes.
Dim strMessage, strScript, strStdErr, strStdOut
Dim oExec, oWshShell, intErrorLevel
Dim ComSpec
Set oWshShell = CreateObject("WScript.Shell")
ComSpec = oWshShell.ExpandEnvironmentStrings("%comspec%")
intErrorLevel = 0
strScript = ComSpec & " /C echo %temp%"
On Error Resume Next
Set oExec = oWshShell.Exec (strScript)
If (Err.Number <> 0) Then
strMessage = "Error: " & Err.Message
intErrorLevel = 1
Else
Do While oExec.Status = 0
Do While Not oExec.StdOut.AtEndOfStream
strStdOut = strStdOut & oExec.StdOut.ReadLine & vbCrLf
Loop
Do While Not oExec.StdErr.AtEndOfStream
strStdErr = strStdErr & oExec.StdErr.ReadLine & vbCrLf
Loop
WScript.Sleep 0
Loop
intErrorLevel = oExec.ExitCode
strMessage = strStdOut & strStdErr & CStr(intErrorLevel)
End If
WScript.Echo (strMessage)
NOTE: Replacing "ReadLine" above with "Read(1)" accomplishes the same thing, but adds an ability to process characters rather than whole lines.
Of course Wscript.Shell would be a lot easier, but, since you want more fine grain control of your session, consider using Win32_Process. Usually, one uses this to control the placement of a new window, but, in your case, you want it hidden, so I set startupInfo.ShowWindow = 0 which means SW_HIDE. The following declares a VBScript function called RunCmd and which will run a command in an invisible window saving the output to a text file and then return the contents of the text file to the caller. As an example, I invoke RunCmd with the HOSTNAME command:
Function RunCmd(strCmd)
Dim wmiService
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Dim startupInfo
Set startupInfo = wmiService.Get("Win32_ProcessStartup")
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim cwd
cwd = fso.GetAbsolutePathname(".")
startupInfo.SpawnInstance_
startupInfo.ShowWindow = 0
' startupInfo.X = 50
' startupInfo.y = 50
' startupInfo.XSize = 150
' startupInfo.YSize = 50
' startupInfo.Title = "Hello"
' startupInfo.XCountChars = 36
' startupInfo.YCountChars = 1
Dim objNewProcess
Set objNewProcess = wmiService.Get("Win32_Process")
Dim intPID
Dim errRtn
errRtn = objNewProcess.Create("cmd.exe /c """ & strCmd & """ > out.txt", cwd, startupInfo, intPID)
Dim f
Set f = fso.OpenTextFile("out.txt", 1)
RunCmd = f.ReadAll
f.Close
End Function
MsgBox RunCmd("HOSTNAME")
References:
Create method of the Win32_Process class
Win32_ProcessStartup class
I am trying to connect command prompt through VB script and further its connecting with Oracle enviroment to execute some reports of Oracle discoverer.
But the problem is with this VB script only.
line 2: for establishing the connection.
line 7:fetching the current REQUEST_ID.
line 16:XXDIS_EXPORT_CMD_V is a view and cmd is a column.which select a value like this for corresponding REQUEST_ID.
/CONNECT DISCADMIN:"FAI Financials Intelligence"/discbi#deverp /OPENDB "1 Scheduling" /SHEET "Sheet_1" /EXPORT HTML o27673334.out /LOGFILE l27673334.log /BATCH
In the end i want to execute this cmd using VBScript.
Error coming :
"In line 32 tkgoShell was not
recognized"
This is My code:
' Process job
Set objADO =CreateObject("ADODB.Connection")
objADO.Open "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=deverp; UID=apps; PWD=apps11i;"
MsgBox "Connection Established to Server.", vbExclamation + vbOKOnly, "System"
Do While True
' Check if there is a job to process
Set moRS=objADO.execute("SELECT apps.xxdis_schedule_pkg.start_job REQUEST_ID FROM dual")
moRS.MoveFirst
msRequest = moRS("REQUEST_ID")
'MsgBox msRequest,msRequest
' If no jobs then exit
' If msRequest = "0" Then
Exit Do
' End If
loop
Set moRS=objADO.execute("SELECT cmd EXPORT_CMD FROM apps.xxdis_export_cmd_v " & _
"WHERE request_id = " & msRequest)
MsgBox msRequest,msRequest
moRS.MoveFirst
msExpCmd = moRS("EXPORT_CMD")
' write command into a temporary file
msCmdFile = "r" & msRequest & ".cmd"
dim moOutputStream,filesys,msCommand
Set filesys = CreateObject("Scripting.FileSystemObject")
Set moOutputStream = filesys.CreateTextFile(msCmdFile, True)
' Substitute $SAMBA$ and $TNS$ locally configured variables
moOutputStream.Write Replace(Replace(msCmd, "$SAMBA$", gsOutDir),_
"$TNS$", gsInstance) & vbCRLF
moOutputStream.Close
' Call Discoverer to process the command
msCommand = gsBinDir & gsDiscoExe & " /EUL " & gsEUL & " /CMDFILE " & msCmdFile
Call tkgoShell.Run (msCommand, 1, true)
If I understand your code correctly I can't see anywhere where you're actually creating the tkgoShell.
Try inserting the following 2 rows before your last line:
Dim tkgoShell
Set tkgoShell = WScript.CreateObject ("WScript.Shell")
See here for more information about Shell.Run.