VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Multiple methods of connecting to device (https://forums.vandyke.com/showthread.php?t=3433)

1720 02-11-2009 03:21 PM

Multiple methods of connecting to device
 
I am trying to use SecureCRT to connect to a device first trying ssh and if it fails, then use telnet. The script runs fine if the device supports ssh, but if it does not and only supports ssh, I am getting a Runtime error. Here is my code -
<CODE>
# $language = "VBScript"
# $interface = "1.0"

Sub Main

crt.Screen.Synchronous = TRUE

Dim prompt
prompt = "Password: "

Dim device
device = crt.Arguments(0)

Dim user_name
user_name = crt.Dialog.Prompt("Enter your TACACS userID ", user_name, "TACACS Login",False)

Dim passwd
passwd = crt.Dialog.Prompt("Enter your TACACS Password ", passwd, "TACACS Password",True)


cmd = "/SSH2 /L " & user_name & " /PASSWORD " & passwd & " " & device

crt.Session.Connect cmd

if crt.Screen.WaitForString(device, 4) = True then
crt.window.caption = device
' crt.Screen.Send passwd & vbCr
else
crt.Screen.Send "telnet " & device & chr(13)
crt.Screen.WaitForString "Username:"
crt.Screen.Send user_name & vbCr
crt.Screen.WaitForString "Password:"
crt.Screen.Send passwd & vbCr
end if
End Sub
<CODE>

miked 02-11-2009 06:53 PM

Hello,

It sounds like you're saying that SSH works fine but that the telnet fallback is causing an error.

I'd like to verify that I understand how the script is called and the logic involved.

You call the script using a command like the following:
SecureCRT.exe /S "Session Name" /SCRIPT "c:\Scripts\ProtocolFallbackScript.vbs" /ARG "devicename"
The script puts the devicename into the "device" variable and tries to connect using SSH2. If SecureCRT detects the string in the device variable (within 4 seconds) then you're connected. Otherwise you want to run connect using the Telnet protocol.

I think that the following line will cause a runtime error because you're not connected:
crt.Screen.Send "telnet " & device & chr(13)
The CRT Scripting Runtime error probably says "Error: Screen.Send: not connected".

Is this summary correct?

1720 02-12-2009 07:27 AM

1 Attachment(s)
Hi Mike,

You are pretty much correct with your summary. It first attempts to use ssh, and if fails within 4 seconds, it tries to use telnet or atleast that is the intent of the script. Your statement on how I call the script is also correct. I have attached a screenshot of the error window. But basically it is -

CRT Scripting Runtime Error
Error: Connection failed
File:.....
Line: 23

miked 02-12-2009 06:11 PM

1 Attachment(s)
Hi,

Thanks for confirming that you're wanting to use a protocol fallback. The current problem looks like it's coming as a result of not being able to connect using SSH2. If you were to trap the error (On Error Resume Next) then this may not happen and the script should continue. However, there would still be the issue of trying to connect to a telnet server using Crt.Screen.Send.

I ran some tests and modified your code slightly. In my tests the script seemed to work as I think you're wanting. The modified code is below.
Code:

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

Sub Main
    crt.Screen.Synchronous = TRUE

    Dim prompt
    prompt = "Password: "

    Dim device
    device = crt.Arguments(0)
   
    Dim user_name
    user_name = crt.Dialog.Prompt("Enter your TACACS userID ", user_name, "TACACS Login",False)

    Dim passwd
    passwd = crt.Dialog.Prompt("Enter your TACACS Password ",

    cmd = "/SSH2 /L " & user_name & " /PASSWORD " & passwd & " " & device

    ' Prevent an error from stopping script execution
    On Error Resume Next

    crt.Session.Connect cmd

    if crt.Screen.WaitForString(device, 1) <> True then
          cmd = "/TELNET " & device
          crt.Session.Connect cmd
          crt.Screen.WaitForString "Username:"
          crt.Screen.Send user_name & vbCr
          crt.Screen.WaitForString "Password:"
          crt.Screen.Send passwd & vbCr
    else
          crt.window.caption = device
    end if
End Sub

I'm also attaching a script which you might be interested in looking through. It uses telnet to determine whether or not there is an SSH server (if telnet server port 22 returns a response, that's an SSH server). I think it's a pretty robust script and you might find some useful code in it.

Does the modified script above work any better for you?

Do you think you might be able to modify the ConnectionProtocolFallback-PromptForTarget script to work for your particular goal?

1720 02-13-2009 03:51 PM

Hi Mike,

Thank-you so much. The script works fine. I also looked at the attachment you sent and it looks very useful. I am probably going to try it out on Monday to see how it works.

Once again, thank-you so much.

miked 02-13-2009 04:48 PM

Great - I'm glad to hear the modified script worked. Thanks for posting to let us know. :)

1720 02-18-2009 11:10 AM

1 Attachment(s)
Hi Mike,

Sorry to again bother you, but the script seems to have an anomaly. Unless I am missing something, it does not seem to work. Here's what I mean -
(1) When the device connects via ssh, it connects fine but then for some reason it issues a telnet to the same device
(2) If the device supports only telnet, it is not connecting now. Not sure why.
I am attaching the script.

miked 02-18-2009 04:00 PM

Quote:

(1) When the device connects via ssh, it connects fine but then for some reason it issues a telnet to the same device
I think this could be due to timing. You might try increasing the WaitForString timeout to 5 seconds. Rather than waiting for a string,
what happens if you use Crt.Session.Connected instead of Crt.Screen.WaitForString?

Quote:

[(2) If the device supports only telnet, it is not connecting now. Not sure why.
I am attaching the script.
It looks like you reverted your code to much the same code you originally had.

Your code for the else statement is trying to send a string to a session that is not connected, which is the problem.

Here is the problematic snippet of the code:
Code:

else
    crt.Screen.Send "telnet "  & device & chr(13)
    crt.Screen.WaitForString "Username:"
    crt.Screen.Send user_name & vbCr
    crt.Screen.WaitForString "Password:"
    crt.Screen.Send passwd & vbCr
end if

Using Crt.Screen.Send when SecureCRT is not connected causes the CRT Scripting Runtime error you saw before: "Error: Screen.Send: not connected". On Error Resume Next is hiding this error from you, but it's still an error.

Since you're not connected you cannot send a string to the screen.

Use a variable to build the command and use Crt.Session.Connect, instead of using Crt.Screen.Send.
Code:

    cmd = "/TELNET " & device
    crt.Session.Connect cmd

Does the following code snippet work better for you?
Code:

if crt.Screen.Connected = True then
    crt.window.caption = device
else
    cmd = "/TELNET " & device
    crt.Session.Connect cmd
    crt.Screen.WaitForString "Username:"
    crt.Screen.Send user_name & vbCr
    crt.Screen.WaitForString "Password:"
    crt.Screen.Send passwd & vbCr
end if


1720 02-18-2009 04:44 PM

1 Attachment(s)
Hi Mike,

Changing to
Code:

crt.Screen.Connected = True
now no longer tries to do the telnet when connected via ssh, so issue # 1 is resolved. Thank-you. However, the telnet portion is not working. It does not seem to do anything. I am attaching my updated code (and sorry for not catching your modifications earlier).

miked 02-18-2009 06:07 PM

Sorry about that - my code had a mistake. The code I sent should have been using the Session object not the Screen object
crt.Session.Connected not crt.Screen.Connected.

If you change the line to use crt.Session.Connected does the script work better?


All times are GMT -6. The time now is 08:00 PM.