VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   General (https://forums.vandyke.com/forumdisplay.php?f=11)
-   -   Python Loop failed for device login in Secure CRT (https://forums.vandyke.com/showthread.php?t=14069)

GODWIN1989 02-11-2020 08:33 PM

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


bgagnon 02-12-2020 08:58 AM

Hi GODWIN1989,

It worked as expected for me but you have left off the Synchronous = True line.
Code:

crt.Screen.Synchronous = True
If you still don't get the expected behavior, what is displayed if you put these two lines in after z=y[a] line (and comment out everything else except the line that is incrementing a):
Code:

    crt.Dialog.MessageBox("Device: = " + i)
    crt.Dialog.MessageBox("Pass: = " + z)

Does it loop through as expected?

bgagnon 02-12-2020 11:00 AM

Hi GODWIN1989,

For these reasons:
  • You don't have Screen.Synchronous = True

  • You are using \n instead of \r to simulate pressing the Enter key -- it's \r that must be used, not \n.

  • You are not waiting for the results of either the sh clock command nor the exit command (which is potentially the most important).

  • You are not waiting for evidence that the jumphost shell prompt is ready for the next command.
...what's likely happening is that the script is looping up to the next ssh ... command and sending it before the exit command has even been received by the first secondary host in the loop. In other words, the second time through the loop, the ssh ... command is being sent to the first host, and the script is hanging on the line with crt.Screen.WaitForString("word:").


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()

...and to use format() with an explicit string that uses \r to explicitly make sure \r is being used to "send" commands and data to the remote.

GODWIN1989 02-15-2020 11:51 AM

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)

bgagnon 02-17-2020 08:23 AM

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.



All times are GMT -6. The time now is 09:30 AM.