View Single Post
  #1  
Old 10-11-2016, 09:17 AM
Zander Zander is offline
Registered User
 
Join Date: Oct 2016
Posts: 11
SecureCRT Vbs Script Help

Hello all,

I'm a complete newbie to script writing so please accept my apologies in advance for wrong terminology etc.

I'm currently trying to write a script for work that helps me automate log on to the Cisco kit that I access but I've having some trouble! It works mostly but there are some aspects that don't function like I want them to, plus I think it could probably be a lot shorter? I could be wrong though!

The overall aim is for me to be able to copy an IP address and then by running the script it will SSH to the device.
I have created a 'case' to help with some of the different aspects of devices (e.g. some only have Telnet rather than SSH) or if I make a mistake (e.g. copying something that isn't an IP)
Below is the script...

Code:
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

crt.Screen.Synchronous = True
crt.Screen.IgnoreEscape = True

Sub Main()
    ' If there isn't anything in the Windows Clipboard, it doesn't make any
    ' sense to continue with the script.
    If Trim(crt.Clipboard.Text) = "" Then
        crt.Dialog.MessageBox "No text found in the Windows Clipboard."
        Exit Sub
    End If
    
                'Send the SSH command to USERNAME
                crt.Screen.Send "ssh USERNAME@"
                
    ' Multiple lines in the Windows Clipboard are typically stored with a CRLF
    ' separator, but this script example tries to accommodate other line endings
    ' that might be supported by some editors. Break up lines into an array
    ' (each line as an element within the array).
    If Instr(crt.Clipboard.Text, vbcrlf) > 0 Then
        vLines = Split(crt.Clipboard.Text, vbcrlf)
    ElseIf Instr(crt.Clipboard.Text, vblf) > 0 Then
        vLines = Split(crt.Clipboard.Text, vblf)
    Else
        vLines = Split(crt.Clipboard.Text, vbcr)
    End If
    
    nLineNumber = 0
    For Each strLine In vLines
        ' Send the next line to the remote
        crt.Screen.Send strLine & vbcr
        
        ' Wait for the remote to echo the line back to SecureCRT; bail if the
        ' remote fails to echo the line back to us within 3 seconds.
        If Not crt.Screen.WaitForString(strLine, 3) Then
            crt.Dialog.MessageBox _
                "Sent " & nLineNumber + 1 & " lines, but the last one was " & _
                "not echoed back to SecureCRT within 3 seconds." & vbcrlf & _
                vbcrlf & _
                "Abandoning paste operation."
            Exit Sub
        End If
        nLineNumber = nLineNumber + 1
    Next
    
                'Send the remaining text to log into the CPE
                crt.Screen.Send chr(13)
		Dim nIndex
		nIndex = crt.screen.WaitForStrings( _
		"assword: ", _
		"Are you sure you want to continue connecting (yes/no)?", _
		"onnection refused", _
		"no address associated with name", _
		"syntax error", _
		"^C", _
		5 )
		
		Select Case nIndex
		
			Case 1 'Password
                crt.Screen.Send "PASSWORD1" & chr(13)
                nResult = crt.Screen.WaitForString (">", 7)
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "enable" & chr(13)
                crt.Screen.WaitForString "Password: "
                crt.Screen.Send "PASSWORD2" & chr(13)
				End If
                nResult = crt.Screen.WaitForString ("#", 2)
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "sh ip int brief" & chr(13)
				End If
				
			Case 2 'SSH First Connect Prompt
				crt.Screen.Send "yes" & chr(13)
				crt.Screen.WaitForString "Password: "
                crt.Screen.Send "PASSWORD1" & chr(13)
                nResult = crt.Screen.WaitForString (">", 7)
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "enable" & chr(13)
                crt.Screen.WaitForString "Password: "
                crt.Screen.Send "PASSWORD2" & chr(13)
                nResult = crt.Screen.WaitForString ("#", 2)
				End If
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "sh ip int brief" & chr(13)
				End If
				
			Case 3 'Equipment needs Telnet not SSH
					crt.Screen.Send "telnet "
					crt.Screen.WaitForString "$"
					If Instr(crt.Clipboard.Text, vbcrlf) > 0 Then
						vLines = Split(crt.Clipboard.Text, vbcrlf)
					ElseIf Instr(crt.Clipboard.Text, vblf) > 0 Then
						vLines = Split(crt.Clipboard.Text, vblf)
					Else
						vLines = Split(crt.Clipboard.Text, vbcr)
					End If
    
					nLineNumber = 0
					For Each strLine In vLines
						' Send the next line to the remote
						crt.Screen.Send strLine & vbcr
        
						' Wait for the remote to echo the line back to SecureCRT; bail if the
						' remote fails to echo the line back to us within 3 seconds.
					If Not crt.Screen.WaitForString(strLine, 3) Then
						crt.Dialog.MessageBox _
						"Sent " & nLineNumber + 1 & " lines, but the last one was " & _
						"not echoed back to SecureCRT within 3 seconds." & vbcrlf & _
						vbcrlf & _
						"Abandoning paste operation."
					Exit Sub
				End If
					nLineNumber = nLineNumber + 1
				Next
				crt.Screen.WaitForString "Password: "
				crt.Screen.Send "PASSWORD1" & chr(13)
                nResult = crt.Screen.WaitForString (">", 7)
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "enable" & chr(13)
                crt.Screen.WaitForString "Password: "
                crt.Screen.Send "PASSWORD2" & chr(13)
                nResult = crt.Screen.WaitForString ("#", 2)
				End If
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "sh ip int brief" & chr(13)
				End If
			
			Case 4 'No SSH address associated with name
				crt.Screen.Send Chr(3) & chr(13)
				MsgBox "Incorrect IP address entered"
				
			Case 5 'Sytax Error
				crt.Screen.Send Chr(3) & chr(13)
				MsgBox "Syntax Error"
			
			Case 6 ' Manually Cancelled
				crt.Screen.Send Chr(3) & chr(13)
		
			Case Else 'Timeout
				crt.Screen.Send Chr(3) & chr(13)
				crt.Screen.WaitForString "$"
				crt.Screen.Send "telnet "
					If Instr(crt.Clipboard.Text, vbcrlf) > 0 Then
						vLines = Split(crt.Clipboard.Text, vbcrlf)
					ElseIf Instr(crt.Clipboard.Text, vblf) > 0 Then
						vLines = Split(crt.Clipboard.Text, vblf)
					Else
						vLines = Split(crt.Clipboard.Text, vbcr)
					End If
    
					nLineNumber = 0
					For Each strLine In vLines
						' Send the next line to the remote
						crt.Screen.Send strLine & vbcr
        
						' Wait for the remote to echo the line back to SecureCRT; bail if the
						' remote fails to echo the line back to us within 3 seconds.
					If Not crt.Screen.WaitForString(strLine, 3) Then
						crt.Dialog.MessageBox _
						"Sent " & nLineNumber + 1 & " lines, but the last one was " & _
						"not echoed back to SecureCRT within 3 seconds." & vbcrlf & _
						vbcrlf & _
						"Abandoning paste operation."
					Exit Sub
				End If
					nLineNumber = nLineNumber + 1
				Next
		
				nResult = crt.Screen.WaitForString ("assword: ", 4)
				If nResult = 0 Then
				MsgBox "Timeout reached, equipment unreachable or an error has occured"
				crt.Screen.Send Chr(3) & chr(13)
				Else
				crt.Screen.WaitForString "Password: "
				crt.Screen.Send "PASSWORD1" & chr(13)
				End If
                nResult = crt.Screen.WaitForString (">", 7)
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "enable" & chr(13)
                crt.Screen.WaitForString "Password: "
                crt.Screen.Send "PASSWORD2" & chr(13)
                nResult = crt.Screen.WaitForString ("#", 2)
				End If
				If nResult = 0 Then
				MsgBox "Password Incorrect"				
				Else
                crt.Screen.Send "sh ip int brief" & chr(13)
				End If

	End Select
End Sub
As mentioned above it mostly works, the main issue is my 'Case Else' statement. I'd like the 'Case Else' to end if line 188
Code:
MsgBox "Timeout reached, equipment unreachable or an error has occured"
is used as I do not need the rest of the script after this but I cannot find anything online that says how to make this happen

Thank you in advance for any help, it's very much appreciated!

Cheers,

Zander
Reply With Quote