View Single Post
  #5  
Old 10-13-2016, 06:04 AM
Zander Zander is offline
Registered User
 
Join Date: Oct 2016
Posts: 11
Hi Jake,

Again, thank you so much for your help - As I said at the start I'm a complete novice so thank you for bearing with me

I followed your advice about the indentation and have found the issues with my 'End If' statements I didn't realise something so simple would help so much!
Thank you also for the help in defining strings for the username & password aspects! I've now got the script to a place I'd like it to be So thank you so much for that!

I do have one last question - With the IP address validation that's now in place I'm encountering an issue where if I copy a cell from Excel with an IP address inside and then run this script in SecureCRT it doesn't recognise it as an IP address whereas if I copy the IP address from within the cell the validation is happy with this - Do you have any ideas as to why this is? It's not a huge issue, just a bugbear This used to work fine before the validation was in place so it seems that when the Excel cell itself is copied something else is in the clipboard along with the IP address to cause the validation to believe it's not following the IP address conventions

Cheers,

Zander

Code:
Sub Main()
	'Defining IP address as string and removing all spaces, CRs, LFs, and TAB characters
		strAddress = crt.Clipboard.Text
		strAddress = Replace(strAddress, " ", "")
		strAddress = Replace(strAddress, vbcr, "")
		strAddress = Replace(strAddress, vblf, "")
		strAddress = Replace(strAddress, vbtab, "")

	'Defining SSH & Telent commands
		strSSHCmd = "ssh USERNAME@" & strAddress
		strTelCmd = "telnet " & strAddress
	
	'Defining variables
		strPasswordPrompt = "assword: "
		strNewHostKeyPrompt = "Are you sure you want to continue connecting (yes/no)?"
		strConnectionRefused = "onnection refused"
		strControlC = "^C"
	
	'Defining strings for cases
	vWaitFors = Array(_
        strPasswordPrompt, _
        strNewHostKeyPrompt, _
        strConnectionRefused, _
        strControlC)
		
	Set re = New RegExp
		' Set up a pattern that starts at the beginning ("^"), and looks for a
		' digit that's between 1 and 3 characters long ("\d{1,3}"), followed by
		' a dot character ("\.") (repeated again 3 times, but the last one
		' doesn't have a trailing dot character), followed immediately by the
		' end of the input ("$"):
		re.Pattern = "^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$"

    If Trim(crt.Clipboard.Text) = "" Then
		' If there isn't anything in the Windows Clipboard, it doesn't make any
		' sense to continue with the script
        crt.Dialog.MessageBox "No text found in the Windows Clipboard"
        Exit Sub
	End If
	
	Set matches = re.Execute(crt.Clipboard.Text)

	If matches.Count < 1 Then
		MsgBox "Invalid IPv4 address copied!"
		Exit Sub
	Else
		'Now check to see if each octet is within a valid range
		bValid = True
		For Each nOctet In matches(0).Submatches
		If nOctet < 0 Or nOctet > 255 Then
			crt.Dialog.MessageBox _
			"Clipboard Contains an Invalid IPv4 address"
			Exit Sub
		Else
			bValid = False
		End If
		Next
	End If
	
	'Send the SSH command to USERNAME
	crt.Screen.Send strSSHCmd & vbcr
	nResult = crt.Screen.WaitForString (strSSHCmd, 3)
	If nResult = 0 Then
		MsgBox "Error Occurred"
		Exit Sub
	Else
		'Send the remaining text to log into the CPE (Return Key)
		crt.Screen.Send chr(13)
	End If
	
	If crt.screen.WaitForStrings(vWaitFors, 5) Then
		strWhatWasFound = vWaitFors(crt.Screen.MatchIndex - 1)
		
		Select Case strWhatWasFound
		
			Case strPasswordPrompt 'Password prompt is seen
				'Nothing needs to happen so continue script
				
			Case strNewHostKeyPrompt 'SSH First Connection Prompt
				crt.Screen.Send "yes" & chr(13)
				crt.Screen.WaitForString "Password: "
				
			Case strConnectionRefused 'Equipment needs Telnet not SSH
				crt.Screen.WaitForString "$"
				crt.Screen.Send strTelCmd & vbcr
				Dim nResult
					nResult = crt.Screen.WaitForString (strTelCmd, 3)
				If nResult = 0 Then
					crt.Dialog.MessageBox("Error Occurred")
					crt.Screen.Send Chr(3) & chr(13)
					Exit Sub
				End If
				crt.Screen.WaitForString "Password: "
					
			
			Case strControlC 'User cancelled the script
				crt.Screen.Send Chr(3) & chr(13)
				Exit Sub
		
			Case Else 'Need to add another variable!
				crt.Dialog.MessageBox(_
                    "Whoa!" & vbcrlf & vbcrlf & _
                    "You've something in your WaitFors array doesn't have " & _
                    "'Case handler' code to deal with: " & vbcrlf & vbcrlf & _
                    vbtab & strWhatWasFound & vbcrlf & vbcrlf & _
                    "Time to work on your script code some more and handle " & _
                    "this new thing you added to your vWaitFors array...")
		End Select
		
	Else 'Connection attempt has timed out with SSH - Attempt Telnet, if this times out then exit script
		crt.Screen.Send Chr(3) & chr(13)
		crt.Screen.WaitForString "$"
		crt.Screen.Send strTelCmd & vbcr
		nResult = crt.Screen.WaitForString ("$", 4)
		If nResult = 0 Then
			MsgBox "Timeout reached, equipment unreachable or an error has occured"
			crt.Screen.Send Chr(3) & chr(13)
			Exit Sub
		End If
		nResult = crt.Screen.WaitForString (strTelCmd, 3)
		If nResult = 0 Then
			MsgBox "Error Occurred"
			Exit Sub
		Else
			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)
				Exit Sub
			End If
		End If
	End If
				
	'Send rest of login information to device	
	crt.Screen.Send "PASSWORD1" & chr(13)
	nResult = crt.Screen.WaitForString (">", 7)
	If nResult = 0 Then
		MsgBox "Password Incorrect"
		Exit Sub
	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"
		Exit Sub
	Else
		crt.Screen.Send "sh ip int brief" & chr(13)
	End If
End Sub

Last edited by Zander; 10-13-2016 at 06:06 AM. Reason: Added clarification on the Excel question
Reply With Quote