Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1  
Old 05-26-2016, 08:41 PM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
python - login delay

Hello,

Below is my code i use when connecting to X amount of devices, and it works fine, but to get it to work i added a 5 second delay when i connect to each device.

The reason for the 5 seconds, is i am waiting to see if i see the message "Password authentication failed" - if i do then i can take action. i.e. try the next username/password possibility.

I could simply reduce the 5 to 1, but then commands start running before they should, and problems start occurring.

Could you suggest a better way to accomplish the goal ?

Andy


Code:
def connector(each_node) :

	try:
		# Make sure we are not connected to anything before we try and connect to something
		if crt.Session.Connected: 
			crt.Session.Disconnect()

		# try and log in with the first credentials
		cmd = "/SSH2 /L %s /ACCEPTHOSTKEYS /PASSWORD %s /C 3DES /M SHA1 %s" % (user1, passwd1, each_node.rstrip())
		crt.Session.Connect(cmd, False)

		
		# look for string "Password authentication failed", for 5 seconds only, if we dont see it, then move on
		# if we see this string we should disconnect. This moves us on to the next try statement 
		if (crt.Screen.WaitForString("Password authentication failed", 5) == True):
			#crt.Dialog.MessageBox("Failed to detect login!")
			crt.Session.Disconnect()
		
		# If we managed to connect then Return, this stops us trying any more usernames and passwords
		if crt.Session.Connected:
			# We return the errcode 0, becuase this is the safe connection.
			return 0
		
	except ScriptError:
		# return 333, this can be picked up by the calling method, and we know the node is down.
		return 333

	
	try:
		if crt.Session.Connected: 
			crt.Session.Disconnect()

		cmd = "/SSH2 /L %s /ACCEPTHOSTKEYS /PASSWORD %s /C 3DES /M SHA1 %s" % (user2, passwd2, each_node.rstrip())
		crt.Session.Connect(cmd, False)

		if (crt.Screen.WaitForString("Password authentication failed", 5) == True):
			crt.Session.Disconnect()		
		
		if crt.Session.Connected: 
			return 0
		
	except ScriptError:
		return 333
		
	try:
		if crt.Session.Connected: 
			crt.Session.Disconnect()

		cmd = "/SSH2 /L %s /ACCEPTHOSTKEYS /PASSWORD %s /C 3DES /M SHA1 %s" % (user3, passwd3, each_node.rstrip())
		crt.Session.Connect(cmd, False)

		if (crt.Screen.WaitForString("Password authentication failed", 5) == True):
			crt.Session.Disconnect()		
		
		if crt.Session.Connected: 
			return 0
		
	except ScriptError:
		return 333

	# if we get here then all three passwords have been tried and the node is not down, we have a bigger issue at hand, call an engineer 
	return 666
Reply With Quote
  #2  
Old 05-27-2016, 10:14 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,093
I'd suggest trying to handle authentication in the terminal window, as is exemplified in the Example: Connect in Tab to Host and Handle Authentication in Terminal Window found in the scripting sticky.

Yes, it's VBScript, but the concepts are the same. Use WaitForStrings to wait for all possible replies (one of which could be "password failure"). Then you can decide what to do w/o having to sleep, and things will go as fast as they can with each host, independent of connection speed, latency, etc.

--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support

Last edited by jdev; 11-09-2017 at 11:25 AM.
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 07:17 PM.