VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Logging on each tab (https://forums.vandyke.com/showthread.php?t=10708)

dsautter 10-08-2012 01:49 PM

Logging on each tab
 
I'm trying to create a VBS script to perform the following,

1) connect to multiple hosts within tabs
2) log (raw) each tab with the "TabName__%Y%M%D%h%m%s.txt"
3) go to each tab and send a certain text string.


I'm able to create the connections and name the tab as the host, at this point a box to name the log file opens for each connection. Once each connection has a file name then the text is sent.

What is the trick to automatically name the files?

Here is the script,

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

'===============================================
'LANG :VBScript
'VERSION : 1
'===============================================

Dim FSO, Shell, Windir, Runservice, File, File1
Dim g_objTab, g_strSkippedTabs, objProdTab, objTestTab, objDevTabm, objCurrentTab, nIndex
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set FSO = CreateObject("scripting.filesystemobject")
Set Shell = CreateObject("WScript.Shell")
Set DeviceIP = FSO.opentextfile("c:\ftp\Rush\PORTS.txt", ForReading, False)
Set Device1IP = FSO.opentextfile("c:\ftp\Rush\PORTS1.txt", ForReading, False)
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objSc = crt.Screen
Set objD = crt.Dialog
Set objSe = crt.Session
Set objW = crt.Window
Logfiles = ("c:\ftp\Rush")

While Not DeviceIP.atEndOfStream
PORTS = DeviceIP.Readline()
' connect To host On port 23 (the Default telnet port)

On Error Resume Next
Set objProdTab = crt.Session.ConnectInTab("/telnet 10.255.0.94 "&PORTS&"")
objProdTab.Screen.Synchronous = True
objProdTab.Screen.WaitForString "]$", 1
objProdTab.Caption = ""&PORTS&""
wend

Sub Main()

Set g_objTab = crt.GetScriptTab
g_objTab.Screen.Synchronous = True

Dim nTest
For nTest = 1 to crt.GetTabCount
Set objCurrentTab = crt.GetTab(nTest)
objCurrentTab.Activate
if objCurrentTab.Session.Connected = True Then
objCurrentTab.Session.Log True,False,True
objCurrentTab.Session.LogFileName = "c:\1ftp\rush\"&PORTS&"_%Y%M%D%h%m%s%t.txt"
end if
next

Dim nIndex2

For nIndex2 = 1 to crt.GetTabCount
Set objCurrentTab = crt.GetTab(nIndex2)
objCurrentTab.Activate
objCurrentTab.Screen.Send chr(13)
objCurrentTab.Screen.WaitForString "#",5
objCurrentTab.Screen.Send "thequickbrownfoxjumpsoverthelazydog123456789!@#$%^&*()_+" & vbcr
objCurrentTab.Screen.WaitForString "#",5
next
end sub



Thanks,
Don

miked 10-08-2012 03:26 PM

Hello,

Before reviewing and testing your code to see if I can spot the issue, I'd like to mention the SecureCRT scripting manual. Section 7.1, Logging with SecureCRT's Session Object, seems like a good place to start.

I see that the code you posted enables logging before the log file name is set.
Code:

objCurrentTab.Session.Log True,False,True
objCurrentTab.Session.LogFileName = "c:\1ftp\rush\"&PORTS&"_%Y%M%D%h%m%s%t.txt"

That seems like a problem.

The SecureCRT scripting manual (p. 85) shows disabling logging before setting the log file name. Start logging after the filename is changed.
Code:

If crt.Session.Logging Then
    ' Turn off logging before setting up our script's
    ' logging...
    crt.Session.Log False
End If

' Set up the log file so that it resides in the path
' defined in the TEMP environment variable, and is
' named according to the remote IP address:
crt.Session.LogFileName = "%TEMP%\" & _
"LogFileForIP(" & crt.Session.RemoteAddress & ").txt"
crt.Session.LogUsingSessionOptions
' Send commands, etc.
' ...


dsautter 10-09-2012 01:01 PM

Mike,

Good catch on the logging enable in the wrong place. I've been playing with this script for a couple of days and have made numerous changes during that time, this being one of them.

As far as the manual I've looked through it a few times looking for a solution with no luck.

Thanks,
Don

miked 10-09-2012 01:08 PM

From pages 85-86 of the SecureCRT Scripting Manual, here is the code I thought would be the solution you needed, at least as far as logging is concerned.
Code:

If crt.Session.Logging Then
    ' Turn off logging before setting up our script's
    ' logging...
    crt.Session.Log False
End If

' Set up the log file so that it resides in the path
' defined in the TEMP environment variable, and is
' named according to the remote IP address:
crt.Session.LogFileName = "%TEMP%\" & _
"LogFileForIP(" & crt.Session.RemoteAddress & ").txt"
crt.Session.LogUsingSessionOptions

' Send commands, etc.
' ...

' Stop logging
crt.Session.Log False

' Perform other script work, or setup on the remote for the
' next batch of logging...
' ...

' Start logging again (be sure to append or else we'll
' overwrite the data we've logged so far).
crt.Session.Log True, True

' Send more commands, etc.
' ...
' ...


dsautter 10-10-2012 06:50 AM

Micheal,

That looks like a solution for a single connection. I'm struggling with the logging for each tab. I need to open a raw log for each tab and leave them open until all commands are sent.


Thanks,
Don

miked 10-10-2012 11:22 AM

Sorry about that oversight. The idea is the same - just replace crt.Session with objCurrentTab.Session.

I think the code which sets the log file name and starts logging should be called in your loop.
Code:

Dim nTest
For nTest = 1 to crt.GetTabCount
  Set objCurrentTab = crt.GetTab(nTest)
  objCurrentTab.Activate

  if objCurrentTab.Session.Connected = True Then
    ' This seems like a good place to enable logging
    EnableLoggingForTab(objCurrentTab)
  end if
next

It's a good idea to put the code into a separate function, perhaps named EnableLoggingForTab(...). Some advantages are:
  • A function or subroutine keeps logging modular
  • Facilitates code re-use (say on another scripting project)
  • Helps with debugging, because you can use On Error Resume Next for just the function
Regarding the last point above, I noticed your code used On Error Resume Next, but never used On Error Goto 0. The SecureCRT Scripting Manual (p. 32) discusses On Error Resume Next and dangers of using it in a global way:
Quote:

It is important to recognize that the On Error Resume Next statement enables error handling for the procedure in which the statement is made. If the On Error Resume Next statement is made globally (outside of any function or subroutine) all run-time errors will be suppressed, potentially masking errors other than those you intend to handle yourself.
The following code should shows the way I think you'll have the most success with your code, using a modular logging function, enabling logging for each tab.
Code:

Sub Main()
  Dim nTest
  For nTest = 1 To crt.GetTabCount
    Set objCurrentTab = crt.GetTab(nTest)
    objCurrentTab.Activate
    if objCurrentTab.Session.Connected = True Then
      ' This seems like a good place to enable logging
      EnableLoggingForTab(objCurrentTab)
    end if
    objCurrentTab.Screen.Send "ls" & vbcr
  Next
End Sub

Sub EnableLoggingForTab(ByRef ThisTab)
  If ThisTab.Session.Logging Then
  ' Turn off logging before setting up our script's
  ' logging...
  ThisTab.Session.Log False
  End If

  ' Set up the log file so that it resides in the path
  ' defined in the TEMP environment variable, and is
  ' named according to the remote IP address:
  ThisTab.Session.LogFileName = "c:\Temp\" & PORTS & "_%Y%M%D%h%m%s%t.txt"


  ' Start logging again (be sure to append or else we'll
  ' overwrite the data we've logged so far).
  'ThisTab.Session.Log True, True
  ThisTab.Session.LogUsingSessionOptions
End Sub


Maureen 12-17-2012 11:45 AM

A logging substitution variable for the port has been added to a pre-beta version of SecureCRT. This means that the following script line:

Code:

ThisTab.Session.LogFileName = "c:\Temp\" & PORTS & "_%Y%M%D%h%m%s%t.txt"
could be written as:

Code:

ThisTab.Session.LogFileName = "c:\Temp\" & "%P_%Y%M%D%h%m%s%t.txt"
Please send e-mail to me at Maureen.Jett@vandyke.com if you would be interested in trying it.

Maureen


All times are GMT -6. The time now is 01:12 PM.