Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #31  
Old 10-09-2014, 03:19 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,

I don't think that you need a completely new script. I would likely add another condition after line 199. For example:
Code:
If Instr(strCommand, "copy") > 0 Then
    ' Take special action
    ' Perhaps you can build a prompt
    ' Handle the need to press Enter two times
    ' Etc.
Else
    ' Proceed normally with rest of code
End If
Does this help you modify the script to meet your needs?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #32  
Old 10-09-2014, 03:58 PM
kgaudineer kgaudineer is offline
Registered User
 
Join Date: Jan 2011
Posts: 15
Good afternoon rtb,

Thank you for the post... Unfortunately that won't help. These are prompts returned by the switch.... I am thinking it may just be easier to create a new script.. I have tried copying and pasting sections of this vbs script into a new one and that hasn't worked out either... My dilemma is there are several hundred of these switches I need to hit and it looks like this script has a hard time with a dynamic prompts....

Just out of curiosity is there any documentation on the commands used in Secure CRT. I downloaded a VBS book and other than the if, then, else, and case.... this book isn't much help for your VBS script. I did see the VBS FAQ but that's not much help either... I am looking for info on the commands and their arguments. For example... objNewTab.Screen.CurrentColumn -1 leaves me with 2 questions.... What is objNewTab and what does the -1 mean?
Reply With Quote
  #33  
Old 10-09-2014, 04:26 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,
Quote:
I am thinking it may just be easier to create a new script.
It that is the route you want to go, you are welcome to do so. For me, modifying the existing script would be easier.
Quote:
My dilemma is there are several hundred of these switches I need to hit and it looks like this script has a hard time with a dynamic prompts.
The script is not designed to handle prompts dynamically, but you can build that in. There is no reason that you can't call the WaitForScreenContentsToStopChanging() function again to determine what a new prompt would be.

If you manually type the command all at once, what output do you see?

Please post the output exactly as it would appear. If you need to redact sensitive data, replace each redacted character with an "x".
Quote:
Just out of curiosity is there any documentation on the commands used in Secure CRT.
The SecureCRT API is documented in the help file under the Scripting / Script Objects Reference chapter. VBScript is documented by Microsoft, and page one of the scripting manual provides a reference to Microsoft's documentation that does still work.

The scripting manual is a good place to start if you are learning.

The variable objNewTab is an object. The internet has a wealth of information on learning object oriented programming concepts.
Quote:
objNewTab.Screen.CurrentColumn -1
Here is a breakdown of this statement:
  • objNewTab gets a reference to the currently active tab in SecureCRT. This lets us use the script to interact with the tab using the SecureCRT API.
  • Screen is the SecureCRT API object to use.
  • CurrentColumn is a property that is a member of the Screen object. When it is called, it returns the current column of the cursor in the terminal window which is an integer.
  • Using -1 is simply a mathematical operation. If CurrentColumn returned a 9, the result would be 8.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 10-09-2014 at 04:32 PM.
Reply With Quote
  #34  
Old 10-13-2014, 11:17 AM
kgaudineer kgaudineer is offline
Registered User
 
Join Date: Jan 2011
Posts: 15
Good Morning RTB thank you for the reply...

to answer the first question these are the promps when trying to ftp the config

In the line below Cisco-2960# is the prompt and copy startup-config tftp: is the command. following the command you must hit return
Ciaco-2960#copy startup-config tftp:

The line in red below is what is returned after hitting the return key. 10.24.21.117 is the IP of the tftpo server. must hit retunr to move on...
Address or name of remote host []? 10.24.21.117

The line below is what is returned after presiing enter. The switch will use its host name as a suggestion for the filename. in all cases we want to keep the host name as the file name and add a .txt to the end of it...
Destination filename [Cisco-2960-confg]?

After pressing enter for the 3 rd time you swill see the output below. this indicates a successful config file transfer.
!!
39344 bytes copied in 0.157 secs (250599 bytes/sec)
Cisco-2960#

The file I am working with I have commented out lines 215 & 216 and 259
Lines 215 & 216 are:
' Wait for the command to be echo'd back to us.
g_objNewTab.Screen.WaitForString strCommand

and Line 259 is:
strResult = g_objNewTab.Screen.ReadString(strPrompt)

The script does look like it will run most of the way through, however are having a problem with the last line feed. There is no more information that needs to be added and all that needs to be done is to just hit the return key. How do we put that as a command in the command file being read in?

Also Is there a way to get one log file for each host instead of one log file for every command that is run? This command file has several lines of commands ...
Reply With Quote
  #35  
Old 10-13-2014, 06:11 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,

Thanks for the information.

Here is my understanding from what you posted. If you send the command copy startup-config tftp: the Cisco device responds with the following:
Address or name of remote host []? 10.24.21.117
Do you have to manually enter the IP address or is that automatically populated?
After you press Enter, the Cisco device responds with the following:
Destination filename [Cisco-2960-confg]?
Do you just have to press Enter at this point?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #36  
Old 10-13-2014, 11:51 PM
kgaudineer kgaudineer is offline
Registered User
 
Join Date: Jan 2011
Posts: 15
Good evening rtb,

following the first command copy startup-config tftp the Cisco switch will respond with:
Address or name of remote host []?
The IP address 10.24.21.117 needs to be manually entered or in this case its just a separate line in my commands file that is read.
After entering the IP address press enter
The switch responds with:
Destination filename [Cisco-2960-confg]?
The switch will automatically use its host name as the config file name. For us this is just fine so there is no need to change anything and we just press the enter key to accept the name as it is shown.
Reply With Quote
  #37  
Old 10-14-2014, 09:34 AM
kgaudineer kgaudineer is offline
Registered User
 
Join Date: Jan 2011
Posts: 15
Good morning rtb

I had a second thought this morning... Since there are basically only 3 commands that are being used for this task is there a way to comment out the section of the script that reads commands in from a separate file and just hard code the 3 file transfer steps ? My thoughts are trying it this way...

copy startup-config tftp
10.24.21.117
waitfor string "#", "?" {enter}

OR

copy startup-config tftp://10.24.21.117/{variable to construct file name} {enter}
waitfor string "#", "?" {enter}
waitfor string "#", "?" {enter}
Reply With Quote
  #38  
Old 10-14-2014, 09:52 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,

It seems like your idea would work. Were you able to successfully edit the script to use your idea?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #39  
Old 10-14-2014, 01:38 PM
kgaudineer kgaudineer is offline
Registered User
 
Join Date: Jan 2011
Posts: 15
Good afternoon rtb,

I have not been successful in editing the script yet. Each time I try I break something else with the script....

My plan was to comment out this section starting at line 100....

' Now call the ReadDataFromFile() function for the commands file.
If Not ReadDataFromFile(g_strCommandsFile, _
g_strComment, _
vCommands, _
nCommandCount, _
nCommentLines) Then
DisplayMessage "No commands were found in file: " & vbcrlf & _
vbtab & g_strCommandsFile
Exit Sub
End If

There must be some other dependencies for this section because the error handling and the log file don't work correctly now....
Reply With Quote
  #40  
Old 10-14-2014, 05:12 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,

There are other dependencies. I am not sure how your script may be different, but in the unmodified script, it is lines 91 to 99 that read in the data from the commands file. The ReadDataFromFile() function definition begins on line 326. You can see that vCommands is an array that is passed by reference when the function is called to read the data from the commands file. After vCommands is populated with the commands to run, it is the foundation of the For Each...Next loop that starts on line 198.

I think that rather than try and get rid of the commands file, it may be easier to add in a little code.

If you were to use the concept that I suggested initially, you could modify the script to handle many different types of commands. The For Each...Next loop would look something like the following:
Code:
For Each strCommand In vCommands
    If strCommand = "" Then Exit For
    
    ' Handle interactive commands.  This could have multiple ElseIf statements.
    ' It would even be possible to include the IP Address in the commands file
    ' and parse it out in the code below.  Commented lines 10-14 and 21 are
    ' related to this possibility.
    If Instr(strCommand, "copy") > 0 Then
        ' Take special action
        ' If the command was of the following format:
        '     copy startup-config tftp, 10.10.10.10
        ' vData = Split(strCommand, ",")
        ' Send the command text to the remote
        ' g_objNewTab.Screen.Send vData[0] & vbcr
        g_objNewTab.Screen.Send strCommand & vbcr

        ' Wait "Address or name of remote host []" step
        g_objNewTab.Screen.WaitForStrings "#", "?"
        
        ' Send the IP address
        ' g_objNewTab.Screen.Send vData[1] & vbcr
        g_objNewTab.Screen.Send "10.10.10.10" & vbcr
        
        ' Wait for "Destination filename [Cisco-2960-confg]" step
        g_objNewTab.Screen.WaitForStrings "#", "?"
        
        ' Press Enter to execute the command
        g_objNewTab.Screen.Send vbcr
        
    Else
        ' Proceed normally with a portion of the code
        ' Send the command text to the remote
        g_objNewTab.Screen.Send strCommand & vbcr

        ' Wait for the command to be echo'd back to us.
        g_objNewTab.Screen.WaitForString strCommand
    End If
        
    ' Since we don't know if we're connecting to a cisco switch or a
    ' linux box or whatever, let's look for either a Carriage Return
    ' (CR) or a Line Feed (LF) character in any order.
    vWaitFors = Array(vbcr, vblf)
    bFoundEOLMarker = False
    Do
        ' Call WaitForStrings, passing in the array of possible
        ' matches.
        g_objNewTab.Screen.WaitForStrings vWaitFors, 1
        
        ' Determine what to do based on what was found)
        Select Case g_objNewTab.Screen.MatchIndex
            Case 0 ' Timed out
                Exit Do
            
            Case 1,2 ' found either CR or LF
                ' Check to see if we've already seen the other
                ' EOL Marker
                If bFoundEOLMarker Then Exit Do
                
                ' If this is the first time we've been through
                ' here, indicate as much, and then loop back up
                ' to the  top and try to find the other EOL
                ' marker.
                bFoundEOLMarker = True
        End Select
    Loop

    ' Now that we know the command has been sent to the remote
    ' system, we'll begin the process of capturing the output of
    ' the command.
    
    Dim strResult
    ' Use the ReadString() method to get the text displayed
    ' while the command was runnning.  Note that the ReadString
    ' usage shown below is not documented properly in SecureCRT
    ' help files included in SecureCRT versions prior to 6.0
    ' Official.  Note also that the ReadString() method captures
    ' escape sequences sent from the remote machine as well as
    ' displayed text.  As mentioned earlier in comments above,
    ' if you want to suppress escape sequences from being
    ' captured, set the Screen.IgnoreEscape property = True.
    strResult = g_objNewTab.Screen.ReadString(strPrompt)
    
    Dim objFile, strLogFile
    
    ' Set the log file name based on the remote host's IP
    ' address and the command we're currently running.  We also
    ' add a  date/timestamp to help make each filename unique
    ' over time.
    strLogFile = Replace( _
        g_strLogFileTemplate, _
        "IPADDRESS", _
        g_objNewTab.Session.RemoteAddress)

    ' Replace any illegal characters that might have been
    ' introduced by the command we're running (e.g. if the
    ' command had a path or a pipe in it)
    strCleanCmd = Replace(strCommand, "/", "[SLASH]")
    strCleanCmd = Replace(strCleanCmd, "\", "[BKSLASH]")
    strCleanCmd = Replace(strCleanCmd, ":", "[COLON]")
    strCleanCmd = Replace(strCleanCmd, "*", "[STAR]")
    strCleanCmd = Replace(strCleanCmd, "?", "[QUESTION]")
    strCleanCmd = Replace(strCleanCmd, """", "[QUOTE]")
    strCleanCmd = Replace(strCleanCmd, "<", "[LT]")
    strCleanCmd = Replace(strCleanCmd, ">", "[GT]")
    strCleanCmd = Replace(strCleanCmd, "|", "[PIPE]")
    
    strLogFile = Replace(strLogFile, "COMMAND", strCleanCmd)
    
    ' Add Time stats to the log file name based on the Template
    ' defined by the script author.
    strLogFile = Replace(strLogFile, "YYYY-", Year(Date) & "-")
    strLogFile = Replace(strLogFile, "-MM-", "-" & NN(Month(Date)) & "-")
    strLogFile = Replace(strLogFile, "-DD-", "-" & NN(Day(Date)) & "-")
    strLogFile = Replace(strLogFile, "-hh'", "-" & NN(Hour(Time)) & "'")
    strLogFile = Replace(strLogFile, "'mm'", "'" & NN(Minute(Time)) & "'")
    strLogFile = Replace(strLogFile, "'ss", "'" & NN(Second(Time)))
                    
    Set objFile = g_fso.OpenTextFile(strLogFile, ForAppending, True)

    ' If you want the command logged along with the results,
    ' uncomment the next two lines
    ' objFile.WriteLine "Results of command """ & strCommand & _
    '    """ sent to host """ & g_strHost & """: "

    ' Write out the results of the command and a separator
    objFile.WriteLine strResult
    
    ' Close the log file
    objFile.Close
Next
There may be additional changes needed, but this is an example of how it would look.

Does this help you move forward in creating your script?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #41  
Old 10-15-2014, 04:59 PM
kgaudineer kgaudineer is offline
Registered User
 
Join Date: Jan 2011
Posts: 15
Good afternoon rtb,

Thank you very much for the example. This is working out much better than trying to comment out the section.

If I understand the explanation correctly. I can continue to add 'Else If' blocks just in case I come across a device that has a new prompt that I am not familiar with.... Is that correct....
Reply With Quote
  #42  
Old 10-15-2014, 05:35 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,

You are welcome. I am hoping that this discussion will benefit others that want to use this example and read this thread.

I can see that my explanation is not very good. Adding additional ElseIf statements to the If...Then...Else condition would allow one to handle other interactive commands. For example:
Code:
If Instr(strCommand, "copy") > 0 Then
    ' Take special action
    ' If the command was of the following format:
    '     copy startup-config tftp, 10.10.10.10
    ' vData = Split(strCommand, ",")
    ' Send the command text to the remote
    ' g_objNewTab.Screen.Send vData[0] & vbcr
    g_objNewTab.Screen.Send strCommand & vbcr

    ' Wait "Address or name of remote host []" step
    g_objNewTab.Screen.WaitForStrings "#", "?"
    
    ' Send the IP address
    ' g_objNewTab.Screen.Send vData[1] & vbcr
    g_objNewTab.Screen.Send "10.10.10.10" & vbcr
    
    ' Wait for "Destination filename [Cisco-2960-confg]" step
    g_objNewTab.Screen.WaitForStrings "#", "?"
    
    ' Press Enter to execute the command
    g_objNewTab.Screen.Send vbcr

ElseIf Instr(strCommand, "<additional interactive command>") > 0 Then
    ' Take special action for additional interactive command
Else
    ' Proceed normally with a portion of the code
    ' Send the command text to the remote
    g_objNewTab.Screen.Send strCommand & vbcr

    ' Wait for the command to be echo'd back to us.
    g_objNewTab.Screen.WaitForString strCommand
End If
Does this help to clarify what additional ElseIf blocks can be used to do?

If you found another prompt that you needed to handle when trying to use the copy command, then you would need to modify the first If block. I can help explain how to do that, but I would need example output including the new prompt.

Did you find a new prompt that you need to handle as a part of the copy command?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #43  
Old 10-22-2014, 02:08 PM
iabudi iabudi is offline
Registered User
 
Join Date: Oct 2014
Posts: 4
Using Python Script

Quote:
Originally Posted by rtb View Post
Demonstrates how to connect to hosts read in from a "hosts" file and for each host, send a list of commands read in from a "commands" file. Results of each command run are logged to separate, uniquely-named files based on the host address and the command that is run.
who do I fix he login to into a cisco like the follwoing

telnet 1.1.1.1
password: ********
cisco> enable
password : *******
Cisco#

if we can't get a solution
how can I ignore the prompt change.

Thanks
Reply With Quote
  #44  
Old 10-22-2014, 06:04 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi iabudi,

Thanks for the post. What errors can you get if enable fails?

What error do you see if the password is invalid?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #45  
Old 11-19-2014, 02:15 PM
iabudi iabudi is offline
Registered User
 
Join Date: Oct 2014
Posts: 4
Python Script

Quote:
Originally Posted by rtb View Post
Hi iabudi,

Thanks for the post. What errors can you get if enable fails?

What error do you see if the password is invalid?
I resolved the login issue by adding all the commands under the username


but I am running into a new issue where it runs the first command then stops at the prompt and when I stop the script it tells me its stoped at line 399

# Wait for the command to be echo'd back to us.
g_objNewTab.Screen.WaitForString(strCommand + "\r\n")

The window shows: Router-name#

I not sure what is the script looking for?

Thanks
Reply With Quote
Reply

Tags
example script


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 02:45 AM.