View Single Post
  #3  
Old 02-12-2020, 11:00 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,293
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