Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 10-14-2020, 11: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 11:20 AM. Reason: Please use [CODE] and [/CODE] tags to specify areas of code
Reply With Quote
  #2  
Old 10-14-2020, 01:02 PM
cboyack cboyack is offline
VanDyke Technical Support
 
Join Date: Apr 2020
Location: Albuquerque, NM
Posts: 57
Hi Ahmed,

From the example you shared, it's clear that you've taken an example script and attempted to modify it.

From where did you obtain the example script?

What does the output of the screen look like when you've tried a few incorrect credentials?
__________________
Thanks,
--Cameron

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 10-14-2020, 02:12 PM
Ahmed Yousry Ahmed Yousry is offline
Registered User
 
Join Date: Feb 2020
Posts: 0
Hi Cameron,


The Script That i have attempted to Modify it exist in the below link .
https://forums.vandyke.com/showpost....62&postcount=1


Commands exist in commands File:

Show run | i hostname
show run | i username | ex aaa
exit


Output of the Screen:



User Access Verification

Username: xxx
Password:

Password:
% Access denied

Username: xxx
Password:

Switch#
Switch#
Switch#
Switch#exit
Reply With Quote
  #4  
Old 10-14-2020, 03:41 PM
cboyack cboyack is offline
VanDyke Technical Support
 
Join Date: Apr 2020
Location: Albuquerque, NM
Posts: 57
Hi Ahmed,

Is there any particular reason why you're not simply using the script as-is to attempt multiple users and passwords?

The original script includes support for multiple user name and password values as seen in the example script code itself:

Attached Images
File Type: png MultiHostPassword.png (131.5 KB, 66 views)
__________________
Thanks,
--Cameron

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 10-14-2020, 03:54 PM
Ahmed Yousry Ahmed Yousry is offline
Registered User
 
Join Date: Feb 2020
Posts: 0
Hi Cameron,

Device Prompts username & Passwords 3 times only & After three failures it closes the session, so if i made two List for usernames & Passwords it will fails for sure. as it will loop through this list trying each username in the list with every password in the Password list requires more than three trial to match which will not be suitable in my case.

The Most Appropriate solution is to send standard username & password in every trial.

Do you have any recommendation for code success?

Thanks
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 06:42 AM.