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 10-08-2012, 01:49 PM
dsautter dsautter is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
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
Reply With Quote
  #2  
Old 10-08-2012, 03:26 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
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.
' ...
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 10-09-2012, 01:01 PM
dsautter dsautter is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
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
Reply With Quote
  #4  
Old 10-09-2012, 01:08 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
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.
' ...
' ...
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 10-09-2012 at 01:11 PM. Reason: incomplete post.
Reply With Quote
  #5  
Old 10-10-2012, 06:50 AM
dsautter dsautter is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
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
Reply With Quote
  #6  
Old 10-10-2012, 11:22 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
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
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #7  
Old 12-17-2012, 11:45 AM
Maureen's Avatar
Maureen Maureen is offline
VanDyke Product Director
 
Join Date: Feb 2004
Location: Albuquerque, NM
Posts: 1,612
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
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 12:08 AM.