VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Telnet script not working (https://forums.vandyke.com/showthread.php?t=10429)

rainman 04-25-2012 09:38 AM

Telnet script not working
 
Hello. The following script is not working. Basically I want to telnet and login to the 3 switches one at at time then run a simple command and logout and then move onto the next switch, but the telnet command appears to be incorrect ? Can someone advise please ? Thanks.

#$interface = "1.0"

crt.Screen.Synchronous = True

Sub Main

Dim vHosts(3)
vHosts(0) = "10.1.1.1"
vHosts(1) = "10.1.1.2"
vHosts(2) = "10.1.1.3"

For Each strHost In vHosts
If strHost = "" Then Exit For

crt.Session.Connect "/Telnet " & strHost
crt.Screen.WaitForString "login: "
crt.Screen.Send "username" & vbCr
crt.Screen.WaitForString "Password: "
crt.Screen.Send "pass1234!" & vbCr
crt.Screen.WaitForString "> "
crt.Screen.Send "Show ver" & vbCr
crt.Screen.WaitForString "> "
crt.Screen.Send "logout" & vbCr

Next

' Turn off synchronous mode
crt.Screen.Synchronous = false

End Sub

bgagnon 04-25-2012 10:11 AM

Hello rainman,

What do you mean by "the following script is not working"?

Do you get an error message? (If so, what is the exact error message?)

Does the script appear to hang?

If so, if you choose Cancel from the Script menu, what is the error message?

Or does the script just not do what is expected?

rainman 04-25-2012 10:17 AM

Hello. I receive the error message: Session.connect: already connected. I then select OK. Nothing else happens. The script is not running. Your help is much appreciated. Thanks.

bgagnon 04-25-2012 10:27 AM

Hi rainman,

That matches my test results. You have not disconnected the current connection.

If you want all three sessions to remain open, use ConnectInTab().

Otherwise, you would want to include a disconnect within the For loop.

rainman 04-25-2012 11:00 AM

Hello Brenda, I have now added the session disconnect, but I still receive the same error messsage as previous. Line 18 of the script is were the error is given, this is where the crt.Session.Connect "/Telnet " & strHost line is. See updated script below. Can you please confirm line 18 is correct ? Thanks.

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

' Turn on synchronous mode while performing Send/Wait sequences
' so no input is missed.
crt.Screen.Synchronous = True

Sub Main

Dim vHosts(3)
vHosts(0) = "10.1.1.1"
vHosts(1) = "10.1.1.2"
vHosts(2) = "10.1.1.3"

For Each strHost In vHosts
If strHost = "" Then Exit For

crt.Session.Connect "/Telnet " & strHost
crt.Screen.WaitForString "login: "
crt.Screen.Send "username" & vbCr
crt.Screen.WaitForString "Password: "
crt.Screen.Send "pass1234!" & vbCr
crt.Screen.WaitForString "> "
crt.Screen.Send "Show ver" & vbCr
crt.Screen.WaitForString "> "
crt.Screen.Send "logout" & vbCr

crt.Session.Disconnect

Next

' Turn off synchronous mode
crt.Screen.Synchronous = false

End Sub

bgagnon 04-25-2012 11:08 AM

Hi rainman,

Do you have Reuse disconnected tabs enabled in the Terminal / Tabs category of SecureCRT's Global Options?

What version of SecureCRT are you using? (Help / About SecureCRT...)

Line 18 does appear to be syntactically correct.

My tests are slightly different (SSH2 instead of Telnet), but when I added the logout and disconnect lines, using Connect() works fine (with Reuse disconnected tabs enabled).

rainman 04-25-2012 11:14 AM

Hello Brenda. Yes I have reuse disconnected tabs enabled. I am using version
6.1.1. Thanks.

bgagnon 04-25-2012 11:20 AM

Hi rainman,

I have further information after consulting with a colleague. This is likely an issue of timing. In other words, the script executes too fast to give time for the session to disconnect.

What are the results if you add the following after you send the logout command?

Code:

    .
    .
    .
    crt.Screen.Send "logout" & vbCr

    ' Wait for the 'logout' command we just issued to disconnect us.
    ' Otherwise, when the script loops up to the top of the For..Next
    ' crt.Session.Connect() will likely fail since the script
    ' executes asynchronously from (and much faster than) the
    ' network activity associated with a disconnect...
    Do
        crt.Sleep(25)
    Loop While crt.Session.Connected

Next


rainman 04-25-2012 11:40 AM

Hello Brenda, still the same error, see updated code below , Thanks

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

' Turn on synchronous mode while performing Send/Wait sequences
' so no input is missed.
crt.Screen.Synchronous = True

Sub Main

Dim vHosts(3)
vHosts(0) = "10.1.1.1"
vHosts(1) = "10.1.1.2"
vHosts(2) = "10.1.1.3"

For Each strHost In vHosts
If strHost = "" Then Exit For

crt.Session.Connect "/Telnet " & strHost
crt.Screen.WaitForString "login: "
crt.Screen.Send "username" & vbCr
crt.Screen.WaitForString "Password: "
crt.Screen.Send "pass1234!" & vbCr
crt.Screen.WaitForString "> "
crt.Screen.Send "Show ver" & vbCr
crt.Screen.WaitForString "> "
crt.Screen.Send "logout" & vbCr

Do
crt.Sleep(25)
Loop While crt.Session.Connected

Next

crt.Session.Disconnect

' Turn off synchronous mode
crt.Screen.Synchronous = false

End Sub

bgagnon 04-25-2012 12:22 PM

Hi rainman,

Since you are using an older version of SecureCRT, you might want to check your upgrade eligibility here.

Are you already connected when you run the script?

If so, you would need to include a check for this condition in your script. The below version of the script illustrates this:

Code:

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

' Turn on synchronous mode while performing Send/Wait sequences
' so no input is missed.
crt.Screen.Synchronous = True

Sub Main

Dim vHosts(3)
vHosts(0) = "10.1.1.1"
vHosts(1) = "10.1.1.2"
vHosts(2) = "10.1.1.3"

For Each strHost In vHosts
    If strHost = "" Then Exit For

    If crt.Session.Connected Then
        Do
            crt.Session.Disconnect
            crt.Sleep(25)
        Loop While crt.Session.Connected
    End If

    crt.Session.Connect "/Telnet " & strHost
    crt.Screen.WaitForString "login: "
    crt.Screen.Send "username" & vbCr

    crt.Screen.WaitForString "Password: "
    crt.Screen.Send "pass1234!" & vbCr

    crt.Screen.WaitForString "> "
    crt.Screen.Send "Show ver" & vbCr

    crt.Screen.WaitForString "> "
    crt.Screen.Send "logout" & vbCr

Next

' Turn off synchronous mode
crt.Screen.Synchronous = false

End Sub


rainman 04-25-2012 12:42 PM

Hello Brenda, No I'm not connected before I run the script. After updating the script with your previous post, I now have a slightly different error message. It is - CRT Scritping Runtime error

Error:Connection failed

Thanks.

bgagnon 04-25-2012 03:06 PM

Hi rainman,

You lost me. :confused:

Do you get the new error as soon as you run the script (ie: first host in the array)?

If so, can you verify that you can connect manually to this IP address?

If the error occurs on a different iteration (second or third host), could you verify connectivity to that particular host(s)?

rainman 04-25-2012 03:22 PM

Hello Brenda, yes as soon as I run the script I get the error message.
I can telnet to all hosts fine , but if I try and run the script to connect
I get the error message as detailed previously. Can you help thanks ?

bgagnon 04-25-2012 03:37 PM

Hi rainman,

Did you check your upgrade eligibility?

Are you able to use a more recent version of SecureCRT?

I want to be sure my tests are in the same version of the product.

Please post the current rendition of the script. I ran the last known version in SecureCRT 6.1.1 and it runs flawlessly except when I include an incorrect IP address, and then I get the same error you reported.

rainman 04-25-2012 03:41 PM

Hi Brenda. I will check the upgrade path. Which version do you think I need ?
Thanks.


All times are GMT -6. The time now is 10:17 AM.