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, 03: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, 06: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, 07: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, 292 views)
Reply With Quote
  #4  
Old 02-12-2009, 06: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, 278 views)
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 02-13-2009, 03: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, 04: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
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 12:44 PM.