VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Stuck in while loop cannot send commands (https://forums.vandyke.com/showthread.php?t=14310)

Ahmed Yousry 10-14-2020 10:09 AM

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


cboyack 10-14-2020 12:02 PM

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?

Ahmed Yousry 10-14-2020 01:12 PM

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

cboyack 10-14-2020 02:41 PM

1 Attachment(s)
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:

https://forums.vandyke.com/attachmen...achmentid=1988

Ahmed Yousry 10-14-2020 02:54 PM

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


All times are GMT -6. The time now is 05:38 AM.