#1
|
|||
|
|||
Python Loop failed for device login in Secure CRT
Hi,
I have below code to read list devices name from text file and list of password(RSA keys) from another text file. However the code gets executed only for first time and not looping through. Not sure if I am missing something in 'WaitFor' function. Code:
# $language = "python" # $interface = "1.0" x=open(r"C:\Users\god\Desktop\device.txt","r") y=open(r"C:\Users\god\Desktop\pass.txt","r+").readlines() z='' a=0 for i in x: z=y[a] crt.Screen.Send("ssh %s" %(i)) crt.Screen.WaitForString("word:") crt.Screen.Send(z) crt.Screen.WaitForString("#") crt.Screen.Send("sh clock\n") crt.Screen.Send("exit\n") a+=1 Last edited by jdev; 02-12-2020 at 09:25 AM. Reason: wrap code in [code][/code] blocks for easier viewing |
#2
|
|||
|
|||
Hi GODWIN1989,
It worked as expected for me but you have left off the Synchronous = True line. Code:
crt.Screen.Synchronous = True Code:
crt.Dialog.MessageBox("Device: = " + i) crt.Dialog.MessageBox("Pass: = " + z)
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
Hi GODWIN1989,
For these reasons:
Our script guru has suggested the following improvements: Code:
# $language = "python" # $interface = "1.0" # Get the current shell prompt so we know when we're done # with a remote host and can issue the next 'ssh ' command. # The current shell prompt would be everything that's to # the left of the cursor. strJHPrompt = crt.Screen.Get( crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, crt.Screen.CurrentColumn - 1) x=open(r"C:\Users\god\Desktop\device.txt","r") y=open(r"C:\Users\god\Desktop\pass.txt","r+").readlines() z='' a=0 for i in x: i = i.rstrip() z=(y[a]).rstrip() crt.Screen.Send("ssh {}\r".format(i)) crt.Screen.WaitForString("word:") crt.Screen.Send("{}\r".format(z)) crt.Screen.WaitForString("#") crt.Screen.Send("sh clock\r") crt.Screen.WaitForString("#") crt.Screen.Send("exit\r") # Now that we've sent the 'exit', let's wait until # we see the jump host's shell prompt before we loop # up to the next secondary host... crt.Screen.WaitForString(strJHPrompt) a+=1 It's not advisable to rely on the line read in from the file to have the correct terminator character. A CRLF is not the same as a CR, and can cause problems. Just the same, an LF alone is not the same as a CR. Commands must be sent with \r, not \r\n, and not \n. Else problems will likely arise because there may be two shell prompts that result from a \r\n, or a \n may be insufficient to actually "send" a command. Because of this, in the example above, we have rearranged your code to remove white space at the end of each line... Code:
i = i.rstrip() z = (y[a]).rstrip()
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#4
|
|||
|
|||
Hi bgagnon,
Thank you very much for your quick answer and it worked fine. I am keen to understand the logic behind the "strJHPrompt" variable. What value it contains to wait for? Please explain. strJHPrompt = crt.Screen.Get( crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, crt.Screen.CurrentColumn - 1) |
#5
|
|||
|
|||
Hi GODWIN1989,
The answer is in the comments just above. It's getting the prompt. ![]() Code:
# Get the current shell prompt so we know when we're done # with a remote host and can issue the next 'ssh ' command. # The current shell prompt would be everything that's to # the left of the cursor.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
![]() |
Tags |
login , loop , python , secure crt |
Thread Tools | |
Display Modes | |
|
|