Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-18-2015, 06:43 AM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Logging long output issue

Hi,

I want to create a single log file containing only the output of a list of "show commands" on a single Cisco router ( or "display commands on a Huawei router). The output can be very long (more than 1,000 rows). I have disabled paging using "terminal monitor 0" on Cisco or "screen-length 0 temporary" on Huawei. I'm using preconfigured sessions with Log file always enabled.

I can log all the output using the "session option - log file", but I would like to save "the ouputs" in a different log file.

I have prepared a vbs script to stop "default logging", create a new log file, send the commands, and restore "default logging". Unfortunately I manage to log only the first rows. Could you help me?

This is the script I'm using.

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

' This script captures the running-config to a specific logfile, 
' then restore default logging configuration.

Sub Main

	' Turn on synchronous mode while performing Send/Wait sequences
	' so no input is missed. 
	crt.Screen.Synchronous = True
	
	If crt.Session.Logging Then
		' Turn off logging before setting up our script's logging...
		crt.Session.Log False
	End If
	
	' Set up the config log file
	crt.Session.LogFileName = "temporary log.log"
	
	' Setup CLI (Cisco)
	crt.Screen.Send "!" & chr(13)
	crt.Screen.WaitForStrings "#", ">", 5
	crt.Screen.Send "enable" & chr(13)
	crt.Screen.WaitForStrings "#", ">", 5
	crt.Screen.Send "terminal length 0" & chr(13)
	crt.Screen.WaitForString "#", 5
	
	' Enable logging
	crt.Session.Log True
	
	' Show a very long output (Cisco)
	crt.Screen.Send "show tech-support" & chr(13)
	crt.Screen.WaitForString "#", 15
	
	' Turn off logging
	crt.Session.Log False
	
	' Restore the default log file
	crt.Session.LogFileName = "default log.log"	
	
	' Turn on logging
	crt.Session.LogUsingSessionOptions
End Sub
Reply With Quote
  #2  
Old 03-18-2015, 08:07 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Sansone,

Thanks for the post. I am sorry to hear about the problem.

What version of SecureCRT are you using?

What operating system are you using?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 03-18-2015, 10:13 AM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Quote:
Originally Posted by rtb View Post
Hi Sansone,

Thanks for the post. I am sorry to hear about the problem.

What version of SecureCRT are you using?

What operating system are you using?

Hi rtb,

I have the latest release of SecureCRT: version 7.3.2 (x64 build 752) - Official Release - February 26, 2015.

I have Windows 7 Professional SP1 64bit.

Do you think that a different script based on "TextStream Write/WriteLine" could be a workaround?
Reply With Quote
  #4  
Old 03-18-2015, 12:38 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Sansone,

I tested your script in SecureCRT 7.3.2, and didn't have any difficulty capturing the entire output of a command. I don't have a Cisco device to test against, but it did work against a UNIX machine.

If you are only capturing a portion of the data in the log file, my guess is that the string you are waiting for to know if the command is complete is just not unique enough.

Is it possible that the "#" character is present in the output of the command?

If it is, then I don't think another approach will work any better.

If you modify your script to wait for the entire prompt, do you get better results?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 03-18-2015, 04:27 PM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Hi rtb,

You've probably found the issue: the "#" or "]" characters used in the "crt.Screen.WaitForString" are present in the output of the command.

Can I use a regex, such as "%S#" or "[%S]", in the "crt.Screen.WaitForString"?
Reply With Quote
  #6  
Old 03-18-2015, 05:19 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Sansone,

Currently, WaitForString() does not allow the use of regular expressions. We will post to this thread if we add this in a future release.

Do you have any other way to make the string more unique?

What does "%S" represent?

Do you have the prompt string saved in the session anywhere?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 03-18-2015, 07:14 PM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Hi rtb,

I need a script for many devices, each with a different hostname/prompt. I'd like using a regex "%S" = session name = hostname, as for the "LogFileName".

An alternative solution could be to detect command prompt as in the following code (from "A Guide to Using VBScript in SecureCRT", page 90-94):

Code:
Do
     ' Attempt to detect the command prompt heuristically...
     Do
          bCursorMoved = crt.Screen.WaitForCursor(1)
          Loop Until bCursorMoved = False
          ' Once the cursor has stopped moving for about a second, we'll
          ' assume it's safe to start interacting with the remote system.

          ' 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.)
          nRow = crt.Screen.CurrentRow
          strPrompt = crt.screen.Get(nRow, _
                                                  0, _
                                                  nRow, _
          crt.Screen.CurrentColumn - 1)
          ' Loop until we actually see a line of text appear (the
          ' timeout for WaitForCursor above might not be enough
          ' for slower-responding hosts).
          strPrompt = Trim(strPrompt)
          If strPrompt <> "" Then Exit Do
     Loop

     ' Send each command to the remote system.
     For Each strCommand in vCommands
          ' Exit the For..Next loop if our current command is empty...
          ' otherwise, we'll just be pressing the Enter key pointlessly.
          If Trim(strCommand) = "" Then Exit For

          crt.Screen.Send strCommand & vbcr

          ' Wait for the command to complete before moving on to
          ' the next command
          crt.Screen.WaitForString strPrompt
     Next
Reply With Quote
  #8  
Old 03-19-2015, 08:50 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Sansone,

You found one solution to the problem.

If the hostname is part of the prompt, an alternative to using the code to detect the prompt would be to get it from the session.

For example:
' Get hostname from session
Set objTab = crt.GetScriptTab()
Set objConfig = objTab.Session.Config
strHostname = objConfig.GetOption("Hostname")
strPrompt = strHostname & "#"

' Setup CLI (Cisco)
crt.Screen.Send "!" & chr(13)
crt.Screen.WaitForStrings strPrompt, ">", 5
Does this information help you get your script working?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #9  
Old 03-22-2015, 01:00 PM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Hi Todd,

Do you know how to get the "session name" instead of "session hostname"? The typical prompt is "device_hostname#" and I save all my sessions using the "device_hostname" as "session name". My "session hostname" are IP address not present in the prompt.

Where could I find all the available "options" of a "Session"?

Thanks,

Claudio
Reply With Quote
  #10  
Old 03-23-2015, 02:32 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Claudio,
Quote:
Do you know how to get the "session name" instead of "session hostname"?
There isn't a way to get at the session name using the SecureCRT scripting API. We will post to this thread if we add a method for getting at the session name.

If you would like to be notified directly, please complete and submit the form at the following location:
Submit Feature Request
Since the session name is not the same as the hostname, I would suggest using the example from the scripting manual that you found and posted previously.
Quote:
Where could I find all the available "options" of a "Session"?
The session .ini file has a list of all of the strings (S), DWORDS (D), and multi-strings(Z) that can be accessed using the SessionConfiguration object.

Does this information help to answer your questions?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #11  
Old 03-26-2015, 06:21 PM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Hi Todd,

My script can successfully log my command output in a single file named "D:\Log\%S\%S_config_%Y-%M-%D.log"
%S = "session name"

Is it possible to compare the new log file with the previous one in the same folder? In case of difference I could launch another software to visual compare the different logs.

How could I find the name of the previous log file? Maybe saving all the log file names in a dedicated txt file?

Thanks,

Claudio
Reply With Quote
  #12  
Old 03-27-2015, 12:21 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Claudio,
Quote:
My script can successfully log my command output in a single file named "D:\Log\%S\%S_config_%Y-%M-%D.log"
%S = "session name"
The logging substitution variables are not related to the scripting API or available to the scripting API.
Quote:
Is it possible to compare the new log file with the previous one in the same folder? In case of difference I could launch another software to visual compare the different logs.

How could I find the name of the previous log file? Maybe saving all the log file names in a dedicated txt file?
SecureCRT does not include an API for accessing previous log files or comparing log files.

It is conceivable that you could use VBScript to get a file system object handle, iterate over the files in a folder, find the files you want and launch some external program to compare the files.

You could also use a VBScript file system object handle to open a file and append a log file name to make things easier to determine what log files you want to compare.

Chapter 7.2 of the scripting manual illustrates use of the FileSystemObject:
http://www.vandyke.com/support/tips/...ing/index.html
Does this help?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #13  
Old 04-01-2015, 12:09 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Claudio,
Quote:
Do you know how to get the "session name" instead of "session hostname"?
I forgot about the Path property available to the Session object. You could use this to get at the session name. You would have to parse out the session name, but this may be something you could use in additional to what has already been posted.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #14  
Old 04-06-2015, 06:18 AM
Sansone Sansone is offline
Registered User
 
Join Date: Feb 2015
Posts: 7
Hi Todd,

I have found an easy solution in another thread. I can get the right prompt from the tab caption using the following function.

Code:
Sub Main
 ...
 ' Set up the prompt
 strPrompt = fnGetTabName() & "#"
 ...
' Show current configuration (Cisco)
 crt.Screen.Send "show running-config" & chr(13)
 crt.Screen.WaitForString strPrompt
 ...
End Sub

Function fnGetTabName()
  Dim objScriptTab, objCurTab
  
  Set objScriptTab = crt.GetScriptTab
  Set objCurTab = crt.GetTab(objScriptTab.index)
  
  fnGetTabName = objCurTab.Caption
End Function
Thanks,

Claudio
Reply With Quote
  #15  
Old 04-06-2015, 09:02 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hello Claudio,

Todd is not available today.

Thanks for posting your solution.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply

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 05:37 PM.