#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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. |
#4
|
|||
|
|||
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:
nIndex = crt.Screen.WaitForStrings(["password:", ">"])
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#5
|
|||
|
|||
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 10:00 AM. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|