Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > General

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2020, 08:33 PM
GODWIN1989 GODWIN1989 is offline
Registered User
 
Join Date: Feb 2020
Posts: 2
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
Reply With Quote
  #2  
Old 02-12-2020, 08:58 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 02-12-2020, 11:00 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #4  
Old 02-15-2020, 11:51 AM
GODWIN1989 GODWIN1989 is offline
Registered User
 
Join Date: Feb 2020
Posts: 2
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)
Reply With Quote
  #5  
Old 02-17-2020, 08:23 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Reply With Quote
Reply

Tags
login , loop , python , secure crt

Thread Tools
Display Modes

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:14 PM.