View Single Post
  #1  
Old 10-14-2020, 10:09 AM
Ahmed Yousry Ahmed Yousry is offline
Registered User
 
Join Date: Feb 2020
Posts: 0
Stuck in while loop cannot send commands

Hi,

i am a little bit new in securecrt scripting , i have a task to access to many devices trying different usernames and passwords , i am trying the below script but i think it stuck in the while loop so i cannot execute the rest of the code.

please advise on this issue.

Code:
# $language = "python"
# $interface = "1.0"

# Connect to an SSH server using the SSH2 protocol. Specify the
# username and password and hostname on the command line as well as
# some SSH2 protocol specific options.

import csv
import SecureCRT, sys, os
import time






def main():

	crt.Screen.Synchronous = True
	if crt.Session.Connected:
		crt.Session.Disconnect()
	crt.Session.Connect("/TELNET IP")

	




	vPossibleResponses = [\
        			"sername",
					"assword",
        			"Access denied",
				"Authentication failed"
				]

	while True:
        	crt.Screen.WaitForStrings(vPossibleResponses)
        	if crt.Screen.MatchIndex > 0:
            	strStringFound = vPossibleResponses[crt.Screen.MatchIndex - 1]

		if crt.Screen.MatchIndex == 1:

            		crt.Screen.Send("xx\r")

		elif crt.Screen.MatchIndex == 2:
			 crt.Screen.WaitForString("ssword:", 1)
			 crt.Screen.Send("xx\r")
		elif crt.Screen.MatchIndex == 3:
			 crt.Screen.WaitForString("sername:", 1)
			 crt.Screen.Send("xx\r")
			 crt.Screen.WaitForString("ssword:", 1)
			 crt.Screen.Send("xx\r")
		elif crt.Screen.MatchIndex == 3:
			 crt.Screen.WaitForString("sername:", 1)
			 crt.Screen.Send("xx\r")
			 crt.Screen.WaitForString("ssword:", 1)
			 crt.Screen.Send("xx\r")

		else:
             # Let user know that there is an unhandled case
             crt.Session.SetStatusText("Unhandled " + strStringFound)
            # Yikes... Never expect to be here, but if we got here, it's
            # probably a programming error you've introduced with the
            # 'vPossibleResponses' variable that you'll need to fix
             crt.Dialog.MessageBox("Yikes!\r\n\r\n"
                "We never expect to get here. if you see this, you\r\n"
                "have probably introduced a programming error into\r\n"
                "your script code which will you will need to fix.\r\n\r\n"
                "Chances are you added a string to vPossibleResponses\r\n"
                "but you haven't added the code to handle\r\n"
                "what to do when that special string was found:\r\n\r\n"
                "\t" + strStringFound)
             crt.Screen.SendSpecial("MENU_SCRIPT_CANCEL")

	# Send a 'cat' command to direct everything we are about to send into a file.
	#
	crt.Screen.WaitForString("#")
	crt.Screen.Send("cat > output.txt" + '\r')

	# The next line causes the script to wait until the 'cat' command has been
	# sent (and executed) by the server. So that the data we're about to send
	# doesn't get sent until redirected 'cat' command is actually in effect.
	#
	crt.Screen.WaitForString("output.txt")


	# Send the file a line at a time.
	# Note: A IOError exception will be generated if 'commands.txt' doesn't exist.
	#
	for line in open("D:\\Securecrt\\commands.txt", "r"):
		crt.Screen.Send(line + '\r')
	# send an EOF character to end output to terminate 'cat'
	#

	crt.Screen.Send("\004")



	# Send the file a line at a time.
	# Note: A IOError exception will be generated if 'commands.txt' doesn't exist.
	#


	# send an EOF character to end output to terminate 'cat'
	#

	crt.Screen.Synchronous = False



main()

Last edited by cboyack; 10-14-2020 at 10:20 AM. Reason: Please use [CODE] and [/CODE] tags to specify areas of code
Reply With Quote