Hi Jake,
Thank you so much for your response, it's really helped me! I wasn't aware of regular expressions so I've added that in now which has also allowed me to get rid of the 'Syntax Error' and 'Unknown Address' variables
I've tried to tidy it up now but there are still a few bits that I'm a bit stuck on...
Code:
Sub Main()
'Defining variables
strPasswordPrompt = "assword: "
strNewHostKeyPrompt = "Are you sure you want to continue connecting (yes/no)?"
strConnectionRefused = "onnection refused"
strControlC = "^C"
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})$"
' This loop allows us to continue prompting for valid input until either
' the user cancels, or provides a valid IP address.
' 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
Else
If Trim(crt.Clipboard.Text) = re.Pattern Then
End If
Set matches = re.Execute(crt.Clipboard.Text)
If matches.Count < 1 Then
MsgBox "Invalid IPv4 address entered!"
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
bValid = False
End If
Next
'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 (Return Key)
crt.Screen.Send chr(13)
Dim nIndex
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 "telnet "
crt.Screen.WaitForString "$"
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: "
Case strControlC 'User cancelled the script
crt.Screen.Send Chr(3) & chr(13)
Exit Sub
Case 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 "telnet "
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
Dim nResult
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
Else
crt.Screen.WaitForString "Password: "
End If
End Select
'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 If
End If
End If
End Sub
I've had to throw in a lot of 'End If' statements at the end to stop to script erroring but I can't understand why as everywhere I have an 'If' statement there is a corresponding 'End If' - Unless of course I'm missing something?
I've tried to tidy up the telnet attempts later on but they're still quite long, I've tried taking bits out/ changing bits to shorten in but I'm having no luck - Is there perhaps a way of making the first 'paste clipboard contents bit so that I can reuse it later to cut down on the length? (this bit...)
Code:
' 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 (Return Key)
crt.Screen.Send chr(13)
Thank you again for your help, it's very very much appreciated
