Exit a Secure CRT script without clossing the application or session - vbscript

I am using a vbscript via Secure CRT, containing different subs as well as global variables, outside the main sub, in order to be available for the subs.
I would like to ask if there is a way of exiting the script at any point desired, before reaching the end of the process and without using crt.Quit, because it would be better to have secure crt running and available for additional commands.
My sript is as follows
Intro = msgbox ("ONLY FOR AUTHORISED USE!!!", 1+16+0+4096)
If Intro =2 Then
MsgBox"Operation Cancelled"
ThisIsTheEnd
End If
DSLAM=inputbox ("Please Enter Dslam Name","Login")
If DSLAM="" Then
MsgBox"Operation Cancelled"
ThisIsTheEnd
End If
crt.Screen.Send "telnet " & DSLAM & Chr(13)
crt.Screen.WaitForString "login:"
crt.Screen.Send "xxxx" & Chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "xxxxxxxx" & Chr(13)
crt.Screen.WaitForString ">"
crt.Screen.Send "enable" & Chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "xxxxxxxx" & Chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "conf t" & Chr(13)
crt.Screen.WaitForString "(config)#"
crt.Screen.Send "br" & Chr(13)
crt.Screen.WaitForString "#"
CARD=inputbox("Please Enter Dslam Card", "Dslam Card")
If Card="" Then
MsgBox"Operation Cancelled"
crt.Screen.Send "end" & Chr(13)
crt.sleep 500
crt.Screen.Send "exit" & Chr(13)
ThisIsTheEnd
End If
PORT=inputbox("Please Enter Dslam Port", "Dslam Port")
If Port="" Then
MsgBox"Operation Cancelled"
crt.Screen.Send "end" & Chr(13)
crt.sleep 500
crt.Screen.Send "exit" & Chr(13)
ThisIsTheEnd
End If
Sub Main()
Profile
NextAction
ThisIsTheEnd
End Sub
Sub Profile
SELPROFILE=inputbox("1.free_VDSL_8B_Default" & Chr(13) & "2.free_VDSL_12A" & Chr(13) & "3.free_VDSL_17A", "Please Select the Profile")
crt.Screen.Send "port lre" & " " & CARD & "/" & PORT & " disable" & Chr(13)
crt.sleep 500
If SELPROFILE=1 Then
MsgBox "Default 8B Profile Selected"
crt.Screen.Send "lre" & " " & CARD & "/" & PORT & " xdsl line-config free_VDSL_8B_Default" & Chr(13)
ElseIf SELPROFILE=2 Then
MsgBox "12A Profile Selected"
crt.Screen.Send "lre" & " " & CARD & "/" & PORT & " xdsl line-config free_VDSL_12A" & Chr(13)
ElseIf SELPROFILE=3 Then
MsgBox "17A Profile Selected"
crt.Screen.Send "lre" & " " & CARD & "/" & PORT & " xdsl line-config free_VDSL_17A" & Chr(13)
ElseIf SELPROFILE<>1 or 2 or 3 Then
MsgBox "INVALID PROFILE!!!", 0+16+0+4096
Profile
End If
crt.sleep 500
crt.Screen.Send "port lre" & " " & CARD & "/" & PORT & " enable" & Chr(13)
crt.sleep 500
crt.Screen.Send "end" & Chr(13)
crt.sleep 500
crt.Screen.Send "wr mem" & Chr(13)
crt.Screen.WaitForString "#"
End Sub
Sub NextAction
GOON=inputbox("1.Exit" & Chr(13) & "2.Show Sync" & Chr(13) & "3.Repeat", "Please select next action")
if GOON=1 then
crt.Screen.Send "exit" & Chr(13)
ElseIf GOON=2 then
crt.Screen.Send "sho lre" & " " & CARD & "/" & PORT & " xdsl phys-table linerates" & Chr(13)
NextAction
ElseIf GOON=3 then
crt.Screen.Send "conf t" & Chr(13)
crt.Screen.WaitForString "(config)#"
crt.Screen.Send "br" & Chr(13)
crt.Screen.WaitForString "#"
Profile
Else MsgBox "INVALID SELECTION!!!", 0+16+0+4096
NextAction
End if
End Sub
Sub ThisIsTheEnd
MsgBox"Thank you, come again"
crt.Quit
End Sub
So to summarize everything up, I want to have the option of exiting the script at the various message-input boxes stages, via the cancel option and without closing Secure CRT.
The whole script has to run through Secure CRT so the WScript.Quit is not an option.
Thank you for your time!

So the problem was finally solved by a declaring globally the variables used on the subs, adding a Do Until Loop and rearranging the subs in order that the script can end with the Exit and End Sub commands.
Looks like the End Sub (when used correctly) is enough for terminating the script at any point desired.

Related

Secure CRT send command to an host VBS

Hey i have tried to make a simple script that can log in and send commands to the host/server:
Sub Main
crt.Screen.Synchronous = True
crt.Session.Connect "ip adress"
crt.Screen.WaitForString "Username: "
crt.Screen.Send "username" & chr(13)
crt.Screen.WaitForString "Password: "
crt.Screen.Send "password" & chr(13)
crt.Screen.Send "?" & chr(13)
crt.Screen.Synchronous = False
End Sub
my problem is it wont send anything.
Its working now:
Sub Main
host = Array("host1", "host2")
For each item in host
Dim user
user = "Username"
Dim passwd
passwd = "password"
cmd = "/SSH2 /L " & user & " /PASSWORD " & passwd & " /C AES-192-CTR /M SHA2-256 " & item
crt.Session.Connect cmd
Dim tab, index, nTabCount
nTabCount = crt.GetTabCount()
for index = 1 to nTabCount
set tab = crt.GetTab(index)
tab.activate
tab.screen.send "command" & vbcr
crt.Sleep 5000
crt.Session.Disconnect()
next
Next
End Sub

vbscript not accessing outlook 2013/2016 subfolder off the inbox using redemption

I am running the below vbscript on outlook 2013/2016 and having issues trying to read emails in sub folders off the Inbox. I can read the Inbox emails. . Can anyone point me in the right directions?
thanks in advance.
Function CheckMail(strMailBox,strFolder,strFolderAbbr,strDetails)
'
olFolderInbox = 6
set Session = CreateObject("Redemption.RDOSession")
'
Set objOutlook = CreateObject("Outlook.Application")
Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
set Store = Session.Stores.GetSharedMailbox(strMailBox)
set Inbox = Store.GetDefaultFolder(olFolderInbox)
'
Wscript.Echo "MailBox: " & Store.Name & " - " & Inbox.Name
If strFolder = "" then
set SubFolder = Inbox
strFolderAbbr = strMailBox & " Inbox"
Else
'set SubFolder = Inbox.Folders(strFolder)
'
set SubFolder = Inbox.Folders.Item(strFolder)
'
strFolderAbbr = strMailBox & " Inbox\" & strFolder
Wscript.Echo " Sub Folder: " & SubFolder
End If
'
nItems = SubFolder.Items.Count
If nHowlong > 1 Then
nHowlong = Round((nItems/110)/60,0)
strTime = " Hour(s)!!"
Else
nHowlong = Round(nItems/110,0)
strTime = " Minute(s)!!"
End If
Wscript.Echo nItems & " - Emails in folder " & strFolderAbbr & " About " & nHowlong & strTime
'" - " & nItems
'
for each Msg in SubFolder.Items
nCounter = nCounter + 1
'Wscript.Echo "Item " & nCounter & "/" & nItems & vbCRLF & "EID: " & Msg.EntryID & vbCRLF & "ABOUT: " & Msg.Subject & vbCRLF & "FROM: " & Msg.SenderName & vbCRLF & "LEVEL: " & IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & vbCRLF & "Status: " & IIf(Msg.UnRead, "Not Read", "Read") & vbCRLF & "Received: " & Msg.ReceivedTime & vbCRLF & "Body: " & Msg.Body
'& nCounter & "/" & nItems & vbCRLF & "EID: " & Msg.EntryID & vbCRLF & "ABOUT: " & Msg.Subject & vbCRLF & "FROM: " & Msg.SenderName & vbCRLF & "LEVEL: " & IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & vbCRLF & "Status: " & IIf(Msg.UnRead, "Not Read", "Read") & vbCRLF & "Received: " & Msg.ReceivedTime & vbCRLF & "Body: " & Msg.Body
'
' process all emails in the box
strRecords = strRecords & "REG-" &strFolderAbbr & "~" & Msg.Subject & "~" & Msg.ReceivedTime & "~" & IIf(Msg.UnRead, "Not Read", "Read") & "~" & Msg.SenderName & "~" &IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & "*"
On Error Resume Next
err.clear
if Err Then
'WScript.Echo "ReceivedTime was null"
End If
On Error GoTo 0
next
CheckMail = strRecords
End Function
Function IIf(bClause, sTrue, sFalse)
If CBool(bClause) Then
IIf = sTrue
Else
IIf = sFalse
End If
End Function
I finally figured out the code that works. I would first like to thank Dmitry for all the help he gave me. But this was a stuburn issue. the following code solved the problem. Please dont ask me to explain it just plain ole luck and trail and error.
set Session = CreateObject("Redemption.RDOSession")
Set objOutlook = CreateObject("Outlook.Application")
Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
'Set Root foldedr of the mail box of the stores
set IPMRoot = Session.Stores.Item(strMailBox).IPMRootFolder
'Set the subfolder to the inbox
If strFolder = "" then
set subFolder = IPMRoot.Folders("InBox")
strFolderAbbr = strMailBox & " Inbox"
Else
'Set subfolder to subfolder chosen
set subFolder = IPMRoot.Folders("InBox").Folders(strFolder)
strFolderAbbr = strMailBox & " Inbox\" & strFolder
End If
'

Disabling/ignoring the Windows service "Windows Registry"

I have this VBscript, which is fairly simple, however, I'm asking if its possible to add a line of code, that disables the "Windows Registry" service, that usually runs on every windows machine.
'----------------------------------------------------------------
' Type: Monitor
' Desc: Check to see if ANY services are set to auto start but are not running
' Input:
' Output: Output (String) - Output message to return
' Alert: If ANY services meet the criteria
' Author: Simon McBryde
' Ver: 1.0
'----------------------------------------------------------------
'----------------------------------------------------------------
' Initialize input variables
'----------------------------------------------------------------
Set wshShell = CreateObject( "WScript.Shell" )
strAttemptStart = wshShell.ExpandEnvironmentStrings( "%AttemptStart%" )
'----------------------------------------------------------------
' Main script logic
'----------------------------------------------------------------
Dim objWMIService, objService, colService, strComputer, strOutput
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colService = objWMIService.ExecQuery ("Select * from Win32_Service")
strOutput = ""
strRestart = ""
For Each objService in colService
If objService.StartMode = "Auto" And objService.State <> "Running" Then
strOutput = strOutput & objService.DisplayName & " (" & objService.Name & "), " & "Current state: " & objService.State
If strAttemptStart = "true" Then
strResult = objService.StartService()
Select Case strResult
Case 0 strOutput = strOutput & ", Restart result: " & "Success"
Case 1 strOutput = strOutput & ", Restart result: " & "Not Supported"
Case 2 strOutput = strOutput & ", Restart result: " & "Access Denied"
Case 3 strOutput = strOutput & ", Restart result: " & "Dependent Services Running"
Case 4 strOutput = strOutput & ", Restart result: " & "Invalid Service Control"
Case 5 strOutput = strOutput & ", Restart result: " & "Service Cannot Accept Control"
Case 6 strOutput = strOutput & ", Restart result: " & "Service Not Active"
Case 7 strOutput = strOutput & ", Restart result: " & "Service Request Timeout"
Case 8 strOutput = strOutput & ", Restart result: " & "Unknown Failure"
Case 9 strOutput = strOutput & ", Restart result: " & "Path Not Found"
Case 10 strOutput = strOutput & ", Restart result: " & "Service Already Running"
Case 11 strOutput = strOutput & ", Restart result: " & "Service Database Locked"
Case 12 strOutput = strOutput & ", Restart result: " & "Service Dependency Deleted"
Case 13 strOutput = strOutput & ", Restart result: " & "Service Dependency Failure"
Case 14 strOutput = strOutput & ", Restart result: " & "Service Disabled"
Case 15 strOutput = strOutput & ", Restart result: " & "Service Logon Failure"
Case 16 strOutput = strOutput & ", Restart result: " & "Service Marked For Deletion"
Case 17 strOutput = strOutput & ", Restart result: " & "Service No Thread"
Case 18 strOutput = strOutput & ", Restart result: " & "Status Circular Dependency"
Case 19 strOutput = strOutput & ", Restart result: " & "Status Duplicate Name"
Case 20 strOutput = strOutput & ", Restart result: " & "Status Invalid Name"
Case 21 strOutput = strOutput & ", Restart result: " & "Status Invalid Parameter"
Case 22 strOutput = strOutput & ", Restart result: " & "Status Invalid Service Account"
Case 23 strOutput = strOutput & ", Restart result: " & "Status Service Exists"
Case 24 strOutput = strOutput & ", Restart result: " & "Service Already Paused"
End Select
End If
strOutput = strOutput & " ***** "
End If
Next
If strOutput <> "" Then GenerateAlert strOutput, 1
'----------------------------------------------------------------
' Output result and quit with required exit code
'----------------------------------------------------------------
Sub GenerateAlert (strOutput, intExitCode)
WScript.Echo "<-Start Result->"
WScript.Echo "Output=" & strOutput
WScript.Echo "<-End Result->"
WScript.Quit intExitCode
End Sub
Restrict the results of your WQL query if you want to ignore the service:
"SELECT * FROM Win32_Service WHERE Name <> 'RemoteRegistry'"
Add a condition if you want to stop the service:
If strAttemptStart = "true" Then
If objService.Name = "RemoteRegistry" Then
strResult = objService.StopService()
Else
strResult = objService.StartService()
End If
Select Case strResult
...
End Select
End If

how to echo a command to terminal in vbs

I am using SecureCRT and want to run a vbs script while I am connected in a session, I want to get a site name from user by prompt and then use this variable inside a specific command (pmxh is a session specific command) to send this command to the session, here is my code but I don't know why my echo is not working and returning error (i just want to send pmxh command to the terminal that I have an already open session in it)
Sub Main()
' Prompt the end user for data
strAnswer = InputBox("Please enter site Name:")
' Check to see if the user provided any data, or canceled.
If strAnswer = "" Then
MsgBox "Canceled."
Exit Sub
End If
wscript.echo("pmxh strAnswer -m 0.25 -a pmTotNoRrcConnectReq")
End Sub
I believe you are looking for the crt.Screen.Send command.
crt.Screen.Synchronous = True
' This automatically generated script may need to be
' edited in order to work correctly.
Sub Main()
crt.Screen.Send "cd vshell" & chr(9) & chr(13)
crt.Screen.WaitForString "$ "
crt.Screen.Send "ls" & chr(13)
crt.Screen.WaitForString "$ "
crt.Screen.Send "rpm -U vshell-3.5.0-3" & chr(9) & chr(13)
crt.Screen.WaitForString "$ "
crt.Screen.Send "telnet localhost 22" & chr(13)
crt.Screen.Send chr(13)
End Sub
Read more on sending commands to the terminal -> here (page 10)

VBscript to target specific IP of NIC and change its DNS settings

I have currently been tasked to put together a script that will change the DNS settings of 15,000 ish servers. However, there is no common unique identifer of these NIC's other than their current DNS IP. My Question, Is it possible to somehow have my script do an ipfonfig /all and then if one of the NIC's reports back with the current DNS settings target that NIC for the new updated settings?
I was currently using the below script until i was made aware that some of the NIC will not be called "Production". Any suggestions are welcome! (powershell was not an option as we may be targeting some very old servers)
Dim strDns1
Dim strDns2
strDns1 = "10.10.10.10"
strDns2 = "10.10.10.10"
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set dns name=""Production"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Production"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit
You can do the ipconfig query with a script like this:
Set wso = CreateObject("WScript.Shell")
Set execo = wso.Exec("ipconfig /all")
Set stdout = execo.StdOut
While Not stdout.AtEndOfStream
cmdOutput = cmdOutput & VbCrLf & stdout.ReadLine
Wend
wscript.echo cmdOutput
The main problem is this is just a string, so you have to parse it yourself, which is annoying and probably error prone.
I would rather suggest you take a look at the wmi class
Win32_NetworkAdapterConfiguration
which has a lot of information on the network connections. Only caveat with this WMI class is that it stores many values in arrays instead of strings so you can not just query for everything easily. Either pick something to query against that is a string like DNSDomain or just handle the logic within the script.
I'm agree with Syberdoor , You should use the Wmi Class
Win32_NetworkAdapterConfiguration
This code give you some informations :
Call ListDNSInfo()
'********************************************************************
Sub ListDNSInfo()
Dim ComputerName,IPConfigSet,IPConfig,BailObtenu,BailExpirant
ComputerName="."
On error resume next
set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & ComputerName).ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")
If Err.Number <> 0 Then
wscript.echo " - non accessible -"
Else
for each IPConfig in IPConfigSet
BailObtenu = IPConfig.DHCPLeaseObtained
BailExpirant = IPConfig.DHCPLeaseExpires
'---- Convertion des date et heure d'obtention et d'expiration des baux DHCP en un format lisible par l'utilisateur. ----
BailObtenu = mid(BailObtenu, 7, 2) & "/" & mid(BailObtenu, 5, 2) & "/" & mid(BailObtenu, 1, 4) & " - " & mid(BailObtenu, 9, 2)& ":" & mid(BailObtenu, 11, 2)& ":" & mid(BailObtenu, 13, 2)
BailExpirant = mid(BailExpirant, 7, 2) & "/" & mid(BailExpirant, 5, 2) & "/" & mid(BailExpirant, 1, 4) & " - " & mid(BailExpirant, 9, 2)& ":" & mid(BailExpirant, 11, 2)& ":" & mid(BailExpirant, 13, 2)
MsgBox " Configuration réseau de l'ordinateur " & ComputerName & vbcrlf & vbcrlf & _
"Nom Machine " & vbtab & " : " & IPConfig.DNSHostName & vbcrlf & _
"Carte active" & vbtab & " : " & IPConfig.Description & vbcrlf & _
"Adresse MAC " & vbtab & " : " & IPConfig.MACAddress & vbcrlf & _
"DHCP Activé" & vbtab & " : " & IPConfig.DHCPEnabled & vbcrlf & _
"Adresse IP " & vbtab & " : " & IPConfig.IPAddress(0) & vbcrlf & _
"Masque " & vbtab & vbtab & " : " & IPConfig.IPSubnet(0) & vbcrlf & _
"Passerelle " & vbtab & " : " & IPConfig.DefaultIPGateway(0) & vbcrlf & _
"Serveur DHCP " & vbtab & " : " & IPConfig.DHCPServer & vbcrlf & vbcrlf & _
"Serveur DNS " & vbtab & " : " & IPConfig.DNSServerSearchOrder(0) & vbcrlf & _
" " & vbtab & vbtab & " : " & IPConfig.DNSServerSearchOrder(1) & vbcrlf & _
"Serveur WINS " & vbtab & " : " & IPConfig.WINSPrimaryServer(0) & vbcrlf & _
" " & vbtab & vbtab & " : " & IPConfig.WINSSecondaryServer(0) & vbcrlf & vbcrlf & _
" Bail obtenu " & vbtab & " : " & BailObtenu & vbcrlf & _
" Bail expirant " & vbtab & " : " & BailExpirant _
,VbInformation,"Configuration réseau de l'ordinateur "
Next
End If
End Sub

Resources