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 05-14-2019, 01:09 AM
Neisany Neisany is offline
Registered User
 
Join Date: Feb 2019
Posts: 8
Python waitforstrings multiple solutions

I am looking for a python solution to wait for multiple options and pick the option that it displays.

for instance, I want to send a password and wait for either success [>] or fail [password:].

Everything that I have found so far are using vbs and I am looking for python. There might be a better way I just dont know.

As of right now the line "nIndex" is where it fails

Code:
def main():
	crt.Screen.Synchronous = True
	crt.Screen.Send('ssh [address]' + '\r')
	crt.Screen.WaitForString("password:")
	file = open("password.txt", "r")
	var_password = file.read()
	crt.Screen.Send(var_password + '\r')
	file.close()
	
	nIndex = crt.Screen.WaitForStrings("password:", ">")
	
	if nIndex = 1 then
		crt.Dialog.MessageBox("Password:")
	elif nIndex =2 then
		crt.Dialog.MessageBox(">")
	end if
Reply With Quote
  #2  
Old 05-14-2019, 08:56 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,830
Hi Neisany,

I think the problem is that Python considers == to be the comparison operator. A single = is the assignment operator. Python also does not use the then/end if keywords, you need a colon instead.

This should give better results:

Code:
	if nIndex == 1:
		crt.Dialog.MessageBox("Password:")
	elif nIndex == 2:
		crt.Dialog.MessageBox(">")
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 05-15-2019, 04:01 AM
Neisany Neisany is offline
Registered User
 
Join Date: Feb 2019
Posts: 8
Thank you for your suggestion.
It never makes it to the if statement.
Here is where it fails:
nIndex == crt.Screen.WaitForStrings("password:", ">")

Error: an integer is required.
Reply With Quote
  #4  
Old 05-15-2019, 08:00 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,830
Hi Neisany,

You only need one equal sign here. You are placing the result into the variable, you are not comparing values.

As shown in SecureCRT's Help topic, for Python, you have to use square brackets for an array:

Quote:
Syntax
[ varname = ] object.WaitForStrings(StringsArray [, timeout] [, bcaseInsensitive])

Example
result = crt.Screen.WaitForStrings(["foo", "bar", "quux", "gee"], 10)
So for you that's:

nIndex = crt.Screen.WaitForStrings(["password:", ">"])
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 05-16-2019, 01:59 AM
Neisany Neisany is offline
Registered User
 
Join Date: Feb 2019
Posts: 8
Awesome thank you so much

here is the code as it sits now that works:
Code:
def main():
	crt.Screen.Synchronous = True
	crt.Screen.Send('ssh user@host -p 22' + '\r')
	crt.Screen.WaitForString("password:")
	file = open("password.txt", "r")
	var_password = file.read()
	crt.Screen.Send(var_password + '\r')
	file.close()
	
	nIndex = crt.Screen.WaitForStrings(["password:", ">"],10)

		
	if nIndex == 1:
		passwordchange()
		ShowNew()
	elif nIndex == 2:
		crt.Dialog.MessageBox(">")


def passwordchange():
	
	file = open("password.txt", "w")
	NewPassword = crt.Dialog.Prompt ('New password')
	file.write(NewPassword)
	file.close()
	crt.Screen.Send(NewPassword +'\r')
def ShowNew():
	file = open("password.txt", "r")
	var_password = file.read()
	crt.Dialog.MessageBox(var_password)
	file.close()	
	
	
	
main()

Last edited by bgagnon; 05-16-2019 at 09:00 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 04:58 PM.