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 03-22-2013, 07:53 PM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Automatic telnet command

Hi All,
i've problem with the seguent script:

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

crt.Screen.Synchronous = True

	Dim vHosts(3)
vHosts(0) = "xx.xx.xx.01"
vHosts(1) = "xx.xx.xx.02"
vHosts(2) = "xx.xx.xx.03"

 Dim Tiuser
 Dim Tipass

Sub Main

Tiuser = InputBox("Please enter the username")
Tipass = InputBox("Please enter the password")
 
For Each strHost In vHosts 
 If strHost = "" Then Exit For
 	
' Make sure we are disconnected before attempting a connection 
 If crt.Session.Connected Then
        Do 
        		
            crt.Session.Disconnect
            Loop While crt.Session.Connected
    End If
' Connect to the next host 
 	crt.Session.Connect "/telnet " & strHost 
 	crt.Screen.WaitForString "Username: "
  crt.Screen.Send Tiuser & chr(13)
	crt.Screen.WaitForString "Password: "
	crt.Screen.Send Tipass & chr(13)
	crt.Screen.WaitForString "#"
	crt.Screen.Send "sh run" & chr(13)
	
	 
Next
crt.Screen.Synchronous = False
end sub
I want an automatic telnet connection to list of ip and doing "sh run" command in each device.

The problem is that the crt session open first telnet connection but not send the "sh run" command (it send only "s"). After that first session disconnected and it open the second session but is the same thing. In the third session however the sh run command is executed.
Why? Where I edit my script?

thanks a lot and sorry for my english.
Reply With Quote
  #2  
Old 03-23-2013, 09:02 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
Hi hakelo91,

Thanks for the question. What version of SecureCRT are you using?

What operating system are you using?

What behavior do you get if you manually connect to a host that doesn't work as expected and send the command?

What happens if you change the order of the hosts in the script?

Do you see better results if you move crt.Screen.Synchronous = True so that it is after crt.Session.Connect "/telnet " & strHost
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 03-23-2013 at 09:09 AM.
Reply With Quote
  #3  
Old 03-25-2013, 03:18 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,

Thanks for the question. What version of SecureCRT are you using?

What operating system are you using?

What behavior do you get if you manually connect to a host that doesn't work as expected and send the command?

What happens if you change the order of the hosts in the script?

Do you see better results if you move crt.Screen.Synchronous = True so that it is after crt.Session.Connect "/telnet " & strHost
Thank you for the answer.
My crt version is 6.5.2 (build 446). The OS is Windows 7.
Is not important the function of the command because it is the same thing if I change the string.(there is a cisco router cli command).
I try to move "crt.Screen.Synchronous = True" but it dosn't work.
Reply With Quote
  #4  
Old 03-25-2013, 11:41 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
Hi hakelo91,

Thanks for the update. Would you post your current script that is using a different command?

What behavior do you see when moving crt.Screen.Synchronous = True so that it appears after the Connect() call?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 03-26-2013, 03:22 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,

Thanks for the update. Would you post your current script that is using a different command?

What behavior do you see when moving crt.Screen.Synchronous = True so that it appears after the Connect() call?
probably I've resolve. I reply the string of cisco command and it work :

Code:
....
crt.Session.Connect "/telnet " & strHost 
 	crt.Screen.WaitForString "Username: "
  crt.Screen.Send Tiuser & chr(13)
	crt.Screen.WaitForString "Password: "
	crt.Screen.Send Tipass & chr(13)
  crt.Screen.WaitForString "#"
	crt.Screen.Send "sh run" & chr(13)
	crt.Screen.Send  chr(13)
	crt.Screen.Send  chr(13)
	crt.Screen.WaitForString "#"
	crt.Screen.Send "sh run" & chr(13)
	crt.Screen.Send  chr(13)
	crt.Screen.Send  chr(13)
	next
...
in my opinion it isn't the best solution but you can understand that the cisco cli command is correct and it's indifferent replace the string.
Seems that the first "waiting" dosn't work.
Reply With Quote
  #6  
Old 03-26-2013, 08:04 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
Hi hakelo91,

I am not sure how your script is working. I would not expect your solution to be reliable in the long run.

Here is the change I asked you to make originally:
Code:
crt.Session.Connect "/telnet " & strHost
crt.Screen.Synchronous = True
crt.Screen.WaitForString "Username: "
If you undo what you did to try and resolve the issue, and make the change above, do you get better results?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 03-27-2013, 04:36 AM
GJB56 GJB56 is offline
Registered User
 
Join Date: Nov 2011
Posts: 20
You might also try putting a WaitFor after the sh run

crt.Screen.Send "sh run" & chr(13)
crt.Screen.WaitForString "???"
Reply With Quote
  #8  
Old 03-27-2013, 06:14 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Hi hakelo91,

I am not sure how your script is working. I would not expect your solution to be reliable in the long run.

Here is the change I asked you to make originally:
Code:
crt.Session.Connect "/telnet " & strHost
crt.Screen.Synchronous = True
crt.Screen.WaitForString "Username: "
If you undo what you did to try and resolve the issue, and make the change above, do you get better results?
First, I try this tip but there isn't any change in the output.

Quote:
You might also try putting a WaitFor after the sh run

crt.Screen.Send "sh run" & chr(13)
crt.Screen.WaitForString "???"
Is what I do!

Code:
crt.Screen.Send  chr(13)
	crt.Screen.Send  chr(13)
This strings are negligible but I think it isn't the best solution
Reply With Quote
  #9  
Old 03-27-2013, 09:47 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
Hi hakelo91,

I think that GJB56 may be correct. I looked more closely at your script, and I can imagine that since you are not waiting for the command to complete, the script is disconnecting prior to the command actually being sent. If you wait for your prompt to confirm that the command is complete, do you see better behavior when connecting to subsequent hosts?

As a note, I actually tested this, and I don't even see any of the command sent because SecureCRT is executing the script so quickly.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #10  
Old 03-28-2013, 04:15 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
I'm sorry, I didn't understand!
I reread the post, you are right! In a nutshell I just add another wait after the firts(and only) cli command and it's works!! And now the script it's also quickly!

Code:
....
crt.Session.Connect "/telnet " & strHost 
 	crt.Screen.WaitForString "Username: "
  crt.Screen.Send Tiuser & chr(13)
	crt.Screen.WaitForString "Password: "
	crt.Screen.Send Tipass & chr(13)
  crt.Screen.WaitForString "#"
	crt.Screen.Send "sh run" & chr(13)
	crt.Screen.Send  chr(13)
	crt.Screen.Send  chr(13)
	crt.Screen.WaitForString "#"
	next
...
Now is clear why first it dosen't works!
Thank you a lot!!!!!
Reply With Quote
  #11  
Old 03-28-2013, 09:08 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
Hi hakelo91,

Thanks for the update. I am glad to hear that you have a solution that is working for you.

We would still recommend that you enable Synchronous after the Connect() call. Not doing this could result in unexpected behavior in the future. Additionally, unless there is some reason to send two carriage returns after the command, the lines of code can probably be removed.

Lastly, it is almost always necessary to follow each Send() call with a WaitForString() call (or WaitForStrings()/ReadString()). Executing multiple Send() calls in a row can result in unexpected or erratic behavior of a script.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #12  
Old 03-29-2013, 09:19 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,

Thanks for the update. I am glad to hear that you have a solution that is working for you.

We would still recommend that you enable Synchronous after the Connect() call. Not doing this could result in unexpected behavior in the future. Additionally, unless there is some reason to send two carriage returns after the command, the lines of code can probably be removed.

Lastly, it is almost always necessary to follow each Send() call with a WaitForString() call (or WaitForStrings()/ReadString()). Executing multiple Send() calls in a row can result in unexpected or erratic behavior of a script.
Hi,
I execute multiple Send() because after "sh run" command i need to respond with "enter,enter" so it is correct.

I already enable Synchronous at the beginning of the script, is it the same??

Thanks Again!!
Reply With Quote
  #13  
Old 03-29-2013, 09:46 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
Hi hakelo91,
Quote:
I execute multiple Send() because after "sh run" command i need to respond with "enter,enter" so it is correct.
Sounds good.
Quote:
I already enable Synchronous at the beginning of the script, is it the same??
No, it is not the same. You may see unexpected behavior since you are not enabling the Synchronous property after you connect.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #14  
Old 04-02-2013, 05:35 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,
Sounds good.
No, it is not the same. You may see unexpected behavior since you are not enabling the Synchronous property after you connect.
Okk thank you!! I modify the script!
Reply With Quote
  #15  
Old 12-12-2018, 07:15 AM
myawan myawan is offline
Registered User
 
Join Date: Dec 2018
Posts: 8
similar problem

Gents, I am also having similar problem. My script jumps to next host before completing commands on first host.

Below is the code
Code:
#$language = "VBScript"
#$interface = "1.0"



Dim vHosts(2)
vHosts(0) = "10.251.168.9"
vHosts(1) = "10.251.168.33"


Sub Main
 
For Each strHost In vHosts 
 If strHost = "" Then Exit For
 	
' Make sure we are disconnected before attempting a connection 
 If crt.Session.Connected Then
        Do 
        		
            crt.Session.Disconnect
            Loop While crt.Session.Connected
    End If
' Connect to the next host

 	crt.Session.Connect "/telnet " & strHost
	crt.Screen.Synchronous = True
	crt.Screen.WaitForString "Password: "
	crt.Screen.Send "[redacted]" & chr(13)
	crt.Screen.WaitForString ">"
	crt.Screen.Send "en" & chr(13)
	crt.Screen.WaitForString "Password: "
	crt.Screen.Send "[redacted]" & chr(13)
	crt.Screen.WaitForString "#"
	crt.Screen.Send "config" & chr(13)
	crt.Screen.WaitForString "(config)#"
	crt.Screen.Send "ftp server name remotehost address 10.215.134.10" & chr(13)
	crt.Screen.WaitForString "(config)#"
	crt.Screen.Send "ftp active configuration-report remotehost" & chr(13)
	crt.Screen.WaitForString "(config)#"
	crt.Screen.Send "exit" & chr(13)
	crt.Screen.WaitForString "#"
	crt.Screen.Send "write" & chr(13)
	crt.Screen.WaitForString "#"
	crt.Screen.Send "generate-report report-data-content rl equipment ethernet inventory ne" & chr(13)
	crt.Screen.WaitForString "#"
	crt.Screen.Send "exit" & chr(13)

Next
crt.Screen.Synchronous = False

End Sub
Output is below:


Quote:
cli_relay got SIGWINCH!
Got SIGWINCH!
--------------------------------------------------
MINI-LINK Traffic Node Command Line Interface
--------------------------------------------------

Welcome to BA0222
Password: ********

BA0222>en
Password: ********

BA0222#config

BA0222(config)#ftp server name remotehost address 10.215.134.10

BA0222(config)#ftp active configuration-report remotehost

BA0222(config)#





--------------------------------------------------
MINI-LINK Traffic Node Command Line Interface
--------------------------------------------------

cli_relay got SIGWINCH!
Welcome to BA0162
Got SIGWINCH!
Password: ********

BA0162>en
Password: ********

BA0162#config

BA0162(config)#ftp server name remotehost address 10.215.134.10

BA0162(config)#ftp active configuration-report remotehost

BA0162(config)#exit

BA0162#write

BA0162#generate-report report-data-content rl equipment ethernet inventory ne
% INFO Start to generate report...................................................................% INFO Configuration report uploaded successfully
BA0162#exit

Last edited by jdev; 12-12-2018 at 08:49 AM. Reason: Add CODE tags around the code
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 02:33 AM.