VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Example: Read Data From Separate Hosts/Commands File And Log To Individual Files (https://forums.vandyke.com/showthread.php?t=10651)

msia0808 04-19-2019 01:25 AM

How to keep all connections tab open
 
How to modify the script in order that existing session remains open and then it open a new session tab then repeat again until all the hosts are completed? I want to manually validate all configuration before committing the change.

Previously I have tried to comment out line 510-514, the first session did not terminate (as how I want it), then it open a new session tab but won’t enter credentials.
Do
g_objNewTab.Session.Disconnect
crt.Sleep 100
Loop While g_objNewTab.Session.Connected

Ulua 04-14-2020 11:52 AM

RunCommandsOnMultipleHostsAndLogResults.py.
 
Can these scripts run-in Windows 10 in a SecureCrt 7.1? Thank you...

Because I am getting these errors below:

https://docs.google.com/drawings/d/1...btCXPHu5o/edit


Ulua.

bgagnon 04-14-2020 12:28 PM

Hi Ulua,

Offhand I do not know of any specific methods or properties used in the script that would prevent it from running successfully in SecureCRT v7.1. However, official support for Windows 10 began with SecureCRT v7.3.x.

If using a newer version is at all possible (version 8.7.1 is available for evaluation), it is recommended.

Ulua 04-14-2020 12:32 PM

Hi bgagnon,

That makes a lot of sense, that error is because of my SecureCRT version. Thank you for the quick response.

Ulua

bgagnon 04-14-2020 12:46 PM

Hi Ulua,

Sorry, I get an email as a result of your post. You must have added the info about the error after the email was sent.

I cannot open that document.

What error are you getting?

gmlabs 06-25-2020 06:43 AM

Running script instead of command.txt
 
I'm trying to run a recorded script instead of text in the command file.
I have had success adding the code just before "g_objNewTab.Screen.Send("{0}\r".format(strCommand))" , but the indent is killing me and quick cut and paste from recorded script would be nice.

Is there an easier way to add a sub that can be called ?

jdev 06-25-2020 12:15 PM

Quote:

Originally Posted by gmlabs (Post 53709)
I'm trying to run a recorded script instead of text in the command file.
I have had success adding the code just before "g_objNewTab.Screen.Send("{0}\r".format(strCommand))" , but the indent is killing me and quick cut and paste from recorded script would be nice.

Is there an easier way to add a sub that can be called ?

There isn't any easy way to do what you want.

This script example is designed to read commands that are literal text strings that get sent (without modification) to the remote system.

--Jake

dav-parker 07-16-2020 03:53 PM

Script issues
 
I'm using SecureCRT 8.7.2 on Windows 10.

I'm wondering if VBScript has changed since this scrtipt was written. First, the string manipulation for the file paths can't work.

g_strHostsFile = g_strMyDocs & "\##hosts.txt" will throw an error when trying to read the file. The preceding "\" has to be removed.

Getting past that, I am trying to do a simple "show int ip brief" on an ASA platform. I have two hosts with the login info saved. When I launch the session from session manager, the session connects and logs in automatically.

I add the two sessions to my ##hosts.txt file and using the default ##commands.txt file with the "show int ip br" command.

When I run the script, I see where it attempts to connect to each session I get something like the following for each session.

Please enter login information for 10.1.20.2.
Username:
username
Please enter login information for 10.1.20.2.
Username: The private key file could not be found:
::rawkey

Note that the public key file and private key file must have the same name (e.g., "Identity.pub" and "Identity") and must be located in the same folder.

The system cannot find the path specified.

Password:
Password authentication failed.
The server has disconnected with an error. Server message reads:
A protocol error occurred. Out of context message type 97

dav-parker 07-16-2020 04:55 PM

Re: Script issues
 
OK, I think I'm making progress. The host name apparently has to match the name of the sessions ini filename.

The script now seems to get stuck after the prompt changes when entering enable mode. The last char changes from a '>' to a '#'.

The script gets hung at:

strResult = g_objNewTab.Screen.ReadString(strPrompt)

dav-parker 07-16-2020 05:20 PM

Re: Script issues
 
##commands.txt
enable
show int ip brief
exit

A02-ASA>
A02-ASA> enable
Password: ********
A02-ASA#

I cancel the script on line 428

strResult = g_objNewTab.Screen.ReadString(strPrompt)

I can see that the first "enable" command gets sent.
The session automatically enters the password but the script is stuck at this point. It appears that the "#" is one of the cases in waitforstrings method.

Case 6,7,8 ' "$", "#", or ">"

Any thoughts?

jdev 07-16-2020 06:40 PM

Hi dav-parker,

Quoting your most recent...
Quote:

Originally Posted by dav-parker (Post 53794)
##commands.txt
Code:

enable
show int ip brief
exit

Output:
Code:

A02-ASA>
A02-ASA> enable
Password: ********
A02-ASA#

I cancel the script on line 428

strResult = g_objNewTab.Screen.ReadString(strPrompt)

I can see that the first "enable" command gets sent.
The session automatically enters the password but the script is stuck at this point. It appears that the "#" is one of the cases in waitforstrings method.

Case 6,7,8 ' "$", "#", or ">"

Any thoughts?

... I do have some thoughts for you!

As documented on lines 365-368 of the example script code itself...
Code:

' Get the shell prompt so that we can know what to look for when
' determining if the command is completed. Won't work if the
' prompt is dynamic
(e.g. changes according to current working
' folder, etc)

...your 'enable' command is issued at a command prompt level that ends in a '>' character, after which the prompt changes to one that ends with a '#' character. This script example isn't designed to accommodate commands that change the shell prompt.

Commands from the ##commands.txt file don't get issued until ***AFTER*** the Connect() function call is made, so your reference to Case 6,7,8 isn't immediately relevant -- "That ship has already sailed".

There's no "easy button" fix for your plight, but one idea would be to modify your Connect() function so that if you detect the ">" user level prompt there, you could then issue the necessary enable command and send the corresponding password needed. This way, your shell prompt will end with a # when the Connect() function returns, allowing your ##commands.txt file to focus on everything but the 'enable' command and its requisite password handling.

For example, inside of your Connect() function, theoretically replacing your 'Case 6,7,8...' block, you'd have this instead:
Code:

Case 6,7,8 ' "$", "#", or ">" <-- Shell prompt means auth success
    g_objNewTab.Session.SetStatusText _
        "Connected to " & g_strHost & " as " & g_strUsername
   
    ' Check to see if the prompt level is a ">". If it is, then
    ' run the 'enable' command and provide the necessary enable
    ' pass, prompting for it as needed.
    If g_objNewTab.Screen.MatchIndex = 8 Then
        ' Special case for the ">" prompt... Cisco user priv prompt, anyone?
        ' Do we have an enable password yet? If not, prompt the
        ' end user for it.
        If g_strEnablePass = "<not-yet-specified>" Then
            g_strEnablePass = crt.Dialog.Prompt( _
                "Please specify the 'enable' password." & vbcrlf & _
                vbcrlf & _
                "If no enable command is desired, enter ""<none>""", _
                "Enable Password?", _
                "<none>", _
                True)
            If g_strEnablePass = "" Then
                ' User canceled the Enable Password prompt. Let's bail:
                g_objNewTab.Screen.SendSpecial("MENU_SCRIPT_CANCEL")
            End If
        End If
       
        If g_strEnablePass <> "<none>" Then
            ' Send the 'enable' command:
            g_objNewTab.Screen.Send "enable" & vbcr
           
            ' Wait for the enable password prompt:
            g_objNewTab.Screen.WaitForStrings("ssword:")
           
            ' Send the enable password:
            g_objNewTab.Screen.Send g_strEnablePass & vbcr
           
            ' If the correct password was entered, then the
            ' rest of this code handles itself because the
            ' next iteration of this loop in Connect() will
            ' Find the # privileged shell prompt.
           
            ' If you want to enable bad password handling,
            ' this is where you'd do it.
        End If
    Else
        ' MatchIndex wasn't 8, so the shell prompt wasn't ">".
        ' This means that we either don't care or don't need
        ' to issue any 'enable' command.
        Exit Do
    End If

Based on the replacement block I've postulated with the untested code snippet idea I've cobbled together above, there would need to be a new line/statement inserted near the top of your script code file (after all the leading comments, but before MainSub() gets called on line 182) that defines the global g_strEnablePass for use in authenticating to those hosts that need it:
Code:

g_strEnablePass = "<not-yet-specified>"
Best wishes to you and your scripting project.

jdev 07-16-2020 06:53 PM

Hi dav-parker,

To clarify...
Quote:

Originally Posted by dav-parker (Post 53793)
The host name apparently has to match the name of the sessions ini filename.

...the host name only needs to match the name of an existing saved session if you want to use existing saved sessions -- that's the default behavior of the script.

If desired, you can use ad hoc connections (specify IP addresses or direct host names instead of saved session names in the ##hosts.txt file)... you just have to turn off the bUseExistingSessions variable in the script that says saved sessions are being used:

Default value (use saved sessions)...
Code:

Dim g_bUseExistingSessions
g_bUseExistingSessions = True

... vs. customized (make ad hoc connections)…
Code:

Dim g_bUseExistingSessions
g_bUseExistingSessions = False


dav-parker 07-17-2020 09:20 AM

Quote:

Based on the replacement block I've postulated with the untested code snippet idea I've cobbled together above, there would need to be a new line/statement inserted near the top of your script code file (after all the leading comments, but before MainSub() gets called on line 182) that defines the global g_strEnablePass for use in authenticating to those hosts that need it:
Code:

g_strEnablePass = "<not-yet-specified>"
Best wishes to you and your scripting project
Cool, thanks. It is working now.
I tweaked it slightly since since the session automatically enters the enable password:

Code:

Else
  ' if enable password is stored in session, just send enable
    g_objNewTab.Screen.Send "enable" & vbcr
End If

Update
FYI, going into conf t mode caused the script to get stuck again.
I noticed in a previous post that to get around this, just hard code the strPrompt = "#".
Looks good to go.
Seems like changing the way dynamic prompting is handled might help a lot of people with scripting. Maybe grab the initial prompt but compare original string to new prompt (length original - 1) + rightmost character to cover the trailing # character.

jdev 07-17-2020 01:40 PM

Quote:

Originally Posted by dav-parker (Post 53797)
Seems like changing the way dynamic prompting is handled might help a lot of people with scripting. Maybe grab the initial prompt but compare original string to new prompt (length original - 1) + rightmost character to cover the trailing # character.

Good thought, except for these issues:
  1. How do you know what the "new prompt" is? You have to know what it is (before hand) in order to wait for it.
  2. Eventually even if you know what the prompt text is right now, you might actually issue a command that causes the prompt to change to be shorter (like exiting config mode or dhcp pool mode, etc).... in which case you would have to know *all* of the possible ways that a command might end up changing the prompt *AND* know exactly what the new prompt text is going to be.
Short of supporting regular expressions in WaitFor*() and ReadString() calls, using # is currently the best bet if you're on a Cisco machine and your commands will end up changing the CLI shell prompt.

--Jake

dav-parker 07-31-2020 03:39 PM

WaitForString strCommand
 
For the most part, I can do most of what I need. But, I've run across a new issue. I'm sending several commands to modify ACLs on an ASA. The script gets stuck at:

g_objNewTab.Screen.WaitForString strCommand

It appears that the ACL command is long enough that it doesn't display the entire command back. It wraps. So basically, the script hangs and cannot proceed.

Any good thoughts as to how to work around this?

Thanks,
David


All times are GMT -6. The time now is 12:26 AM.