#1
|
|||
|
|||
On error close CRT..... or something like that
Hello again.
Trying to make another script(s) to make my life easier. I have a scheduled task which runs nightly and calls a BAT file. This BAT file calls SecureCRT with a script from the command line. All works fine......unless one of my remote sites is down. I have numerous remote sites and the above BAT file runs the backup script (for my cisco configs). Each site has its own script. This is necessary due to the fact that the backups need to be pushed via TFTP to various locations depending on the site. My issue is that if one site goes down, the BAT file hangs b/c SecureCRT is waiting for a user to "click" on OK as the device is not reachable. Therefore, the remaining sites are not backed up due to the hung process. Is there a way to force CRT to try and connect for "X" seconds and if no connect, simply move onto the next device? Or a "FORCE" option on the CLI exe that will not prompt for user intervention? My scheduled task runs with no user login. I have found the cResult = crt.Screen.waitforcursor (5) command in another post but I am not sure if this will solve my issue. Please advise. Thanks in advance FYI: VERSION: SecureCRT 6.1 Code: vbScript Last edited by bangyourhead; 06-02-2009 at 06:16 AM. |
#2
|
|||
|
|||
Hi bangyourhead,
It should be possible to add some error handling to your script as follows: Code:
On Error Resume Next crt.Session.Connect "/SSH2 /PASSWORD password username@hostname", True If Err.Number = 0 Then 'back up device End If Does this help to accomplish your scripting goal? |
#3
|
|||
|
|||
RTB,
Thank you for your direction. Once again you have saved me lots of headaches. It did work. I had to play around for a bit with it but in the end all is well. My specific scenario is pulling from a flat text file a list of IP addresses to connect to. I am also posting the hostname and the last line of the TFTP transfer to a text file for a report that another vb script emails to our network admins. What was happening is that the report file populated my spacer character " : " was being populated with the remaining devices becauce err.number = 1. I had to specify a err.number = 0 statement after the End If statement before the script "wend" to the next device. Example: Code:
# $language = "VBScript" # $interface = "1.0" Const ForAppending = 8 Sub Main filepath = "C:\backupscript\email\daily\report.txt" Const username = "USERNAME" ' Username to use for login Const password = "PASSWORD" ' Password for corresponding user Const DEVICE_FILE_PATH = "c:\BackupScript\device.txt" Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim fil Set fil = fso.OpenTextFile(DEVICE_FILE_PATH) Set file = fso.OpenTextFile(filepath, ForAppending, True) Dim ip Dim cnxnString On Error Resume Next While Not fil.AtEndOfStream ip = fil.ReadLine cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip If Err.Number = 0 Then ' Connect crt.Screen.Synchronous = True crt.Session.Connect cnxnString ' Lets start crt.Screen.WaitForString "#" ' Gather the hostname for the tftp file strPrompt = "#" crt.Screen.Send " " & vbcr crt.Screen.WaitForString vbcr strInfo = crt.Screen.ReadString(strPrompt) strInfo = Replace(strInfo, vbcrlf, vbcr) strInfo = Replace(strInfo, vblf, vbcr) vLines = Split(strInfo, vbcr) For each strLine in vLines If Instr(strLine, "EGGR-") Then vSystemHostname = Split(strLine, "#", 1) hostname = Trim(vSystemHostname(0)) End If Next '*********Testing to see if Hostname is what we want. ********* '****Uncomment the next line to receive prompt of hostname***** 'crt.Dialog.MessageBox hostname ' Lets TFTP the config crt.Screen.Send "copy flash:config.text tftp" & vbCr crt.Screen.WaitForString "?" crt.Screen.Send "IP OF TFTP SERVER" & vbCr crt.Screen.WaitForString "?" crt.Screen.Send ("DIRECTORY_PATH\" & hostname & "-config.text") & vbCr crt.Screen.WaitForString "#" screenrow = crt.screen.CurrentRow - 1 Dim result result = crt.Screen.Get(screenrow, 1, screenrow, 70 ) crt.Sleep 1000 if err.number = 0 then 'Lets write to the Log File file.write hostname file.write " : " file.write result & vbCrLf else 'ERROR WRITE file.write "" & vbCrLf file.write "" & vbCrLf file.write "" & vbCrLf file.write ip & "-----ERROR ON CONNECT-----" & vbCrLf file.write "" & vbCrLf file.write "" & vbCrLf file.write "" & vbCrLf end if ' Time to close the session crt.Screen.Synchronous = False crt.Session.Disconnect ' Lets clear the hostname variable End if hostname = "" result = "" Err.Number = 0 ' Next device in the list Wend fil.Close ' Time to close CRT! crt.quit end sub This ensures that all my veriables are reset and cleared before moving on to the next device. The above script sends my start-config to a tftp server and saves the file with the hostname for easy sorting and location then gives me an output log file of the hostname and last line of the file transfer. If I am unable to connect to a device in my devices.txt file it appends a big gap in my log file, the IP address and a statement that CRT could not connect to the device. The bat file that I have scheduled to run this script, then calls another vb script which emails me the log file nightly, then renames the file with the date and moves it to a directory that gets backed up. This has all (most???? I HOPE) of the error handling that I have experienced to date and lets all my Network Admins know the status of last night's backup. Once again.... thank you for your help and I hope the above code can help someone else out in the future! |
#4
|
|||
|
|||
Hi bangyourhead,
I am glad that you were able to successfully use VBScript's built in error handling. The Err object has various methods and properties. In the future, you may want to use the Clear method rather than setting Err.Number to zero. I forgot to include this initially. For example: Code:
While Not objFile.AtEndOfStreamOn Error Resume Next crt.Session.Connect "/SSH2 /PASSWORD password username@hostname", True If Err.Number = 0 Then 'back up device End If Err.ClearWend |
#5
|
|||
|
|||
Hi bangyourhead,
I have some additional information that may help with your future scripting efforts. The error handling information that I previously provided is not necessarily complete. VanDyke Software has recently published a scripting manual that may be of interest to you. This is a work in progress, so you may see things change, but the scripting manual can be found at the following location: http://www.vandyke.com/support/tips/scripting/index.html In particular, we have updated chapter 3.6 Handling Connection Failures Within a Script with information on how to implement error handling in VBScripts for SecureCRT. This change has not yet been published to the website yet so I will include it below for your convenience. The following example illustrates how to check for errors. Code:
' Instruct the script host we want to handle errors ourselves, now: On Error Resume Next ' Attempt to connect to the remote machine: crt.Session.Connect "/SSH2 /L user /PASSWORD vuln3r4ble 192.168.0.2" ' Capture error code and description (if any) nError = Err.Number strErr = Err.Description ' Now, tell the script host that it should handle errors as usual now: On Error Goto 0 If nError <> 0 Then ' Handle the error (log to a file, etc.) . . . Else ' Do work on the remote machine. . . . End If Code:
#$Language="VBScript" #$Interface="1.0" ' Connect-DetectErrorConnecting.vbs Dim g_strError Sub Main() Dim nResult, strConnectInfo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Example of host not found strConnectInfo = "/TELNET host_not_found" nResult = Connect(strConnectInfo) If nResult <> 0 Then ' Instead of displaying a message to the user, you would maybe ' write an error code to a file... MsgBox is just for sample MsgBox "Error connecting with info: " & strConnectInfo & _ vbcrlf & vbcrlf & g_strError Else ' Connection was successful. ' Do the required work ' . . . ' Now that the work is done, disconnect from the remote... crt.Session.Disconnect End If End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function Connect(strConnectInfo) ' Workaround that uses "On Error Resume Next" VBScript directive to detect ' Errors that might occur from the crt.Session.Connect call and instead of ' stopping the script with an unrecoverable error, allow for error handling ' within the script as the script author desires. g_strError = "" ' Turn off error handling before attempting the connection On Error Resume Next crt.Session.Connect strConnectInfo ' Capture the error code and description (if any) nError = Err.Number strErr = Err.Description ' Restore normal error handling so that any other errors in our ' script are not masked/ignored On Error Goto 0 Connect = nError If nError <> 0 Then g_strError = strErr End If End Function |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|