Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-19-2015, 04:45 PM
mms mms is offline
Registered User
 
Join Date: Jun 2015
Posts: 4
Smile syntax not working after failed telnet connection

Any help guys?
My script actually is not mine. It does simply telnet multiple devices then issue commands.
I tried to modify it that if telnet fails it should try to use ssh instead by if else and waitforstring
However from our Jumpserver, when the telnet fails like this below (simulate):
10.10.10.10 only allows ssh, it should try to waitforstring and then if the condition is true(login prompt) use ssh instead
if > = false means it was able to go to the router privilege mode



Trying 10.10.10.10...
telnet: connect to address 10.10.10.10: Connection refused
telnet: Unable to connect to remote host: Connection refused
JUMPSERVER#

All the syntax below are not being evaluated after the failed telnet
I tried to verify and just send a string after the failed connection and it doesn't work.
So there must be some additional lines to add to the telnet connection if it fails.
Am i missing something here?

I need to make the If then statement work if the telnet fails to use ssh instead.

Attaching the script:

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

crt.Screen.Synchronous = True

sub Main
	Const DEVICE_FILE_PATH = "C:\XXXX\devices.txt"
	Const COMMANDS_FILE_PATH = "C:\XXXX\commands.txt"
	
	dim fso	
	set fso = CreateObject("Scripting.FileSystemObject")

	dim fil
	set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

	const username = "XXXX" ' Username for login
	const password = "XXXX" ' Password for login 
	const enable = "XXXX" ' Enable for login 

	'turn on synchronous mode so we don't miss any data
	crt.Screen.Synchronous = True


	'ENABLING LOGGING
	'-----------------------------------------------------------------
	If crt.Session.Logging Then	
	 crt.Session.Log False
	End If	
		
	crt.Session.LogFileName = "C:\XXXX.txt"

	If crt.Session.Logging <> True Then
	 crt.Session.Log True
	End If
	'-----------------------------------------------------------------
	'END OF ENABLING LOGGING

    While Not fil.AtEndOfStream
	ip = fil.ReadLine

   	crt.Screen.Send "telnet " & ip & vbcr
	crt.Screen.WaitForString "Username: "
	crt.Screen.Send username & vbcr
	crt.Screen.WaitForString "Password:"
	crt.Screen.Send password & vbcr

	dim result
	result = crt.Screen.WaitForStrings("JUMPSERVER#", ">", 10)
	
	If result = 1 Then
	crt.Screen.WaitForString "JUMPSERVER#"
        crt.Screen.Send "ssh -l XXXX " & ip & vbcr
        crt.Screen.WaitForString "Password: "
	crt.Screen.Send password & vbcr
        End If
	
	If result = 2 Then     
   	crt.Screen.WaitForString ">"
	crt.Screen.Send "en" & vbcr
	crt.Screen.Send enable & vbcr
	crt.Screen.WaitForString "#"
	End If
	
	 dim com
	 set com = fso.OpenTextFile(COMMANDS_FILE_PATH)
	 
	While Not com.AtEndOfStream
	 
	   ioscommand = com.ReadLine
	   crt.Screen.Send ioscommand & vbcr
    	 wend
	
	com.Close

	crt.Screen.Send "exit" & vbcr
	crt.Screen.WaitForString "JUMPSERVER#"
	
    wend

        fil.Close
     
	'turn off synchronous mode to restore normal input processing
	crt.Screen.Synchronous = False

end Sub
Reply With Quote
  #2  
Old 06-19-2015, 05:23 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi mms,

Thanks for the post.

What version of SecureCRT are you using?

One way to troubleshoot this would be to use a message box to find out what "result" contains.

What do you get in the failure case?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 06-19-2015, 07:31 PM
mms mms is offline
Registered User
 
Join Date: Jun 2015
Posts: 4
Hi rtb, thank you for your quick response
I'm using this version.
Version 7.0.3 (x64 build 480) - Official Release - January 17, 2013

I did add the line below after this line result = crt.Screen.WaitForStrings and the messagebox don't appear.

Code:
crt.Dialog.MessageBox result
Does it have to do with the escape sequences? I've read something about Screen.IgnoreEscape property is this applicable here?

When the telnet failed, after Connection refused error, syntax below are not evaluated.

Thank you
Reply With Quote
  #4  
Old 06-22-2015, 08:28 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi mms,

I am not sure what might be happening based on what you have said.

Please post the modified script with the message box line added.
NOTICE: The requested troubleshooting data may include sensitive information (usernames, passwords, publicly-accessible host names or IP addresses, etc.).

Please redact sensitive information that would not be appropriate for a public forum prior to posting the requested information.

If there is sensitive information that must be conveyed in order to provide a complete picture of the scenario you're facing, please let us know and we will set up a secure upload mechanism that can be used.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 06-22-2015, 09:09 AM
mms mms is offline
Registered User
 
Join Date: Jun 2015
Posts: 4
Here it is. I did modified some of the details same as previous due to sensitive info.

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

crt.Screen.Synchronous = True

sub Main
	Const DEVICE_FILE_PATH = "C:\XXXX\devices.txt"
	Const COMMANDS_FILE_PATH = "C:\XXXX\commands.txt"
	
	dim fso	
	set fso = CreateObject("Scripting.FileSystemObject")

	dim fil
	set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

	const username = "XXXX" ' Username for login
	const password = "XXXX" ' Password for login 
	const enable = "XXXX" ' Enable for login 

	'turn on synchronous mode so we don't miss any data
	crt.Screen.Synchronous = True


	'ENABLING LOGGING
	'-----------------------------------------------------------------
	If crt.Session.Logging Then	
	 crt.Session.Log False
	End If	
		
	crt.Session.LogFileName = "C:\XXXX.txt"

	If crt.Session.Logging <> True Then
	 crt.Session.Log True
	End If
	'-----------------------------------------------------------------
	'END OF ENABLING LOGGING

    While Not fil.AtEndOfStream
	ip = fil.ReadLine

   	crt.Screen.Send "telnet " & ip & vbcr
	crt.Screen.WaitForString "Username: "
	crt.Screen.Send username & vbcr
	crt.Screen.WaitForString "Password:"
	crt.Screen.Send password & vbcr

	dim result
	result = crt.Screen.WaitForStrings("JUMPSERVER#", ">", 10)
	crt.Dialog.MessageBox result

	If result = 1 Then
	crt.Screen.WaitForString "JUMPSERVER#"
        crt.Screen.Send "ssh -l XXXX " & ip & vbcr
        crt.Screen.WaitForString "Password: "
	crt.Screen.Send password & vbcr
        End If
	
	If result = 2 Then     
   	crt.Screen.WaitForString ">"
	crt.Screen.Send "en" & vbcr
	crt.Screen.Send enable & vbcr
	crt.Screen.WaitForString "#"
	End If
	
	 dim com
	 set com = fso.OpenTextFile(COMMANDS_FILE_PATH)
	 
	While Not com.AtEndOfStream
	 
	   ioscommand = com.ReadLine
	   crt.Screen.Send ioscommand & vbcr
    	 wend
	
	com.Close

	crt.Screen.Send "exit" & vbcr
	crt.Screen.WaitForString "JUMPSERVER#"
	
    wend

        fil.Close
     
	'turn off synchronous mode to restore normal input processing
	crt.Screen.Synchronous = False

end Sub
Reply With Quote
  #6  
Old 06-22-2015, 10:28 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi mms,

The issue is not related to escape sequences.

The problem is actually hard to describe.

If you cancel your script when you are testing a Telnet failure, what line is indicated?

My guess would be that the script is stuck waiting for the username prompt on a connection where Telnet is failing.

The basic concept to overcome this is to issue a command, and then wait for all possible responses including success, failure, etc. in a loop handling each possibility as it is received. If you wait for only one possible response, then you loose the possibility of dealing with any other responses. Your script is probably hanging, so you aren't loosing data, but since you are not finding what you need the script is not working.

We have an example that script that illustrates how to handle many possible responses in a loop. Please find it attached.

You will need to modify the example to wait for the responses that you know are possible. That string is on lines 17 to 20. Also, each time you add a possible response, you will have to add a case to the Select statement (beginning on line 40) to deal with the possible response.

Does this help you fix your script?
Attached Files
File Type: txt HandleAuthInTerminalWindow.vbs.txt (4.2 KB, 2830 views)
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 06-22-2015, 11:41 AM
mms mms is offline
Registered User
 
Join Date: Jun 2015
Posts: 4
Hi rtb,

With your help, I finally found what is wrong with the script.
I verified and cancelled the Script and it is true that it points me to line
after

Code:
crt.Screen.Send "telnet " & ip & vbcr
it stucks here because it still waits for the Username Prompt which won't happen since I did have an error failed remote connection
Code:
crt.Screen.Send username & vbcr
It is working now after fixing and transferring my if statements after sending telnet. So I can expect here 2 possible outcomes.

Code:
	crt.Screen.Send "telnet " & ip & vbcr

	dim result
	result = crt.Screen.WaitForStrings("Jumpserver#", "Username: ", 10)
        crt.Dialog.MessageBox result

	If result = 1 Then
   	crt.Screen.Send "ssh " & ip & vbcr
        crt.Screen.WaitForString "Password: "
	crt.Screen.Send password & vbcr
      		End If

	If result = 2 Then
	crt.Screen.Send username & vbcr
	crt.Screen.WaitForString "Password:"
	crt.Screen.Send password & vbcr
		End If
	     
   	crt.Screen.WaitForString ">"
	crt.Screen.Send "en" & vbcr
	crt.Screen.Send enable & vbcr
	crt.Screen.WaitForString "#"
the way the script that you send me uses an array to define all possible responses, l will try to study it if I have time. I'm not yet familiar with On Error.

Great help Todd! Thank you so much.
Reply With Quote
  #8  
Old 06-22-2015, 11:58 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi mms,

Thanks for the update. I am glad that you found a solution. The example script may be something to try in the future. It should allow you to create a more robust solution.
__________________
--Todd

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

Tags
failedremote , ssh , telnet , waitforstring

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:42 PM.