Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-11-2009, 02:21 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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>
Reply With Quote
  #2  
Old 02-11-2009, 05:53 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 02-12-2009, 06:27 AM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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
Attached Images
File Type: gif ScreenHunter_08 Feb. 12 08.24.gif (5.7 KB, 265 views)
Reply With Quote
  #4  
Old 02-12-2009, 05:11 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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?
Attached Files
File Type: txt ConnectionProtocolFallback-PromptForTarget.txt (8.6 KB, 257 views)
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 02-13-2009, 02:51 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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.
Reply With Quote
  #6  
Old 02-13-2009, 03:48 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Great - I'm glad to hear the modified script worked. Thanks for posting to let us know.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #7  
Old 02-18-2009, 10:10 AM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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.
Attached Files
File Type: txt dmz.txt (897 Bytes, 198 views)
Reply With Quote
  #8  
Old 02-18-2009, 03:00 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #9  
Old 02-18-2009, 03:44 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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).
Attached Files
File Type: txt dmz.txt (880 Bytes, 218 views)
Reply With Quote
  #10  
Old 02-18-2009, 05:07 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 02-18-2009 at 05:11 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 08:16 AM.