-3

I am having trouble executing if else statement in vba for secure crt, it seems the statement if is not being understood by the complier.

I wrote a code so that I could login in using host name "ABCDE" and if I get an error message saying that the host name is incorrect then I will login in using the host ip

crt.Screen.Send "ssh 22008130@ABCDE"   &  Chr(13)

If

Crt.Screen.WaitForString ("ssh: ABCDE: Temporary failure in name resolution",10) = True

crt.Screen.Send "ssh 22008130@100.127.16.159"   &  Chr(13)

crt.Screen.WaitForString "Password:"

crt.Screen.Send "Asdf@123" & chr(13)

crt.Screen.Send "exit" & chr(13)

Else If

 

crt.Screen.WaitForString "Password:"

crt.Screen.Send "Asdf@123" & chr(13)

crt.Screen.WaitForString"ABCDE#"

crt.Screen.Send "exit" & chr(13)

end if

end sub 

Microsoft VBScript compilation error
Error: Syntax error
File: C:\Users\22005873\Desktop\Data for script Script for Sfp DATA\SFPSCRIPT FORERRORINNAME.vbs
Line: 9

braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

2

There are a few problems in the code:

If needs Then

You are missing a Then. The correct syntax as described in the documentation

' Block syntax: 
If condition Then
   [statements]
[ElseIf condition-n Then
   [elseifstatements]] . . .
[Else
   [elsestatements]]
End If 

Single-Line syntax:
If condition Then statements [Else elsestatements ]

Else If <> ElseIf

Again, refer to the documentation. In this case, within the Else block there is a new If block that is missing a Then and an End If.
Presumably the correct syntax in this case is ElseIf

Too much newlines

VBScript syntax is line delimited. That means you have to put the If condition Then on a single line.

No indentation

The compiler/interpreter doesn't care about indentation, but us mere humans need indentation to be able to follow the code. Using indentation correctly will help you spot syntax mistakes, like missing an End If.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50