#1
|
|||
|
|||
Log to different files inside a loop
Hi,
I was trying to log to different files for different BGP peers inside a while loop but the contect of the log files are out of sync and some files are even empty, however the screen output are all correct and all log files have created with correct peer name. Here is my script, please if anyone could help: #$language = "VBScript" #$interface = "1.0" sub Main 'VERSION 4.0 'CONSTANTS VALUES '----------------------------------------------------------------- Const ForWriting = 2 Const ForAppending = 8 Const COMMANDS_FILE_PATH = ".\sc1-bgp-peers.txt" const uid_path = "c:\SecureCRT\scripts\uid.txt" const psw_path = "c:\SecureCRT\scripts\psw.txt" '----------------------------------------------------------------- 'END OF CONSTANT VALUES 'FILE SYSTEM OBJECTS '----------------------------------------------------------------- dim fso set fso = CreateObject("Scripting.FileSystemObject") dim uid set uid = fso.OpenTextFile(uid_path) dim psw set psw = fso.OpenTextFile(psw_path) '----------------------------------------------------------------- 'END OF FILE SYSTEM OBJECTS 'SYNCHRONOUS MODE ON (to capture all session data) '----------------------------------------------------------------- crt.Screen.Synchronous = True '----------------------------------------------------------------- 'END OF SYNCHRONOUS MODE ON 'COMMANDS '----------------------------------------------------------------- username = uid.ReadLine password = psw.ReadLine crt.Screen.Send "telnet np06scar01" & vbcr crt.Screen.WaitForString "Username:" crt.Screen.Send username & vbcr crt.Screen.WaitForString "Password:" crt.Screen.Send password & vbcr crt.Screen.WaitForString "#" crt.Screen.Send "terminal length 0" & vbcr dim com set com = fso.OpenTextFile(COMMANDS_FILE_PATH) While Not com.AtEndOfStream ioscommand = com.ReadLine crt.Sleep 1000 'ENABLING LOGGING '----------------------------------------------------------------- If crt.Session.Logging Then crt.Session.Log False End If crt.Session.LogFileName = ".\logs\np-sc1-bgp-" & ioscommand & "-%M%D_%h%m.log" If crt.Session.Logging <> True Then crt.Session.Log True End If '----------------------------------------------------------------- 'END OF ENABLING LOGGING crt.Screen.Send "sh ip bgp neighbors " & ioscommand & " route" & vbcr crt.Screen.WaitForString "#" crt.Screen.Send vbcr crt.Screen.Send "sh ip bgp neighbors " & ioscommand & " advert" & vbcr crt.Screen.WaitForString "#" crt.Screen.Send vbcr crt.Screen.Send vbcr crt.Sleep 5000 'DISABLING LOGGING '----------------------------------------------------------------- crt.Session.Log False '----------------------------------------------------------------- 'END OF DISABLING LOGGING 'crt.Dialog.MessageBox "current BGP peer is " & ioscommand wend com.Close '----------------------------------------------------------------- 'END OF COMMANDS crt.Screen.Send "exit" & vbcr crt.Screen.WaitForString "$ " 'DISABLING LOGGING '----------------------------------------------------------------- crt.Session.Log False '----------------------------------------------------------------- 'END OF DISABLING LOGGING 'SYNCHRONOUS MODE OFF '----------------------------------------------------------------- 'turn off synchronous mode to restore normal input processing crt.Screen.Synchronous = False '----------------------------------------------------------------- 'END OF SYNCHRONOUS MODE OFF end Sub |
#2
|
||||
|
||||
K_Lu,
Which version of SecureCRT is shown in Help / About SecureCRT? It sounds like you're saying some log files don't contain data, and other log files contain the wrong data. I walked through your script (mentally, not actual testing) and don't see anything wrong. Sometimes data can appear to be out of order or incomplete in a log file if the remote is sending VT or ANSI escape sequences to redraw the screen, position the cursor, etc.. Raw logging takes care of that. I see that you're session's logging mechanism, based on code below, so we can use Session Options to enable raw logging. Code:
crt.Session.Log False crt.Session.LogFileName = ".\logs\np-sc1-bgp-" & ioscommand & "-%M%D_%h%m.log" crt.Session.Log True Is Append to file enabled?
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#3
|
|||
|
|||
Thanks for the reply.
I am runing 6.5.2(build446), the Raw log and Append to file are all enabled. It seems the screen output is not sync up with the log files; for example, I saw the log file got created for the 5th peers but screen output was still sending the results for 2nd peer, the first couple log files either has portion of the result or empty. |
#4
|
||||
|
||||
Can you add a message box after setting the log filename to see if you're able to spot whether the logfile name is set correctly everytime?
Code:
crt.Session.LogFileName = ".\logs\np-sc1-bgp-" & ioscommand & "-%M%D_%h%m.log" crt.Dialog.MessageBox("LogFileName = " & crt.Session.LogFileName) What happens if you use only one log file (for all hosts)? If you discontinue changing the log file and the order is still strange, or empty for any hosts?
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#5
|
|||
|
|||
If I have only one log file then I can get all the outputs without any missing info but it will be a huge file and hard to do the comparison before and after for our changes (we have close to 50 peers and every peer has more than 10K prefixes).
Customer was requesting to make them as separated files per BGP peer. The crt.Dialog.MessageBox for "LogFileName" is out of sync with the screen output, for example the 2nd peer Dialog.MessageBox poped out when it was still doing the "sh ip bgp neighbors " & ioscommand & " route" for first peer. |
#6
|
||||
|
||||
I don't see any problems with the script, but I think it would require using actual data that was generated manually, and maybe setting some breakpoints in the code. More specifically, message boxes help debug the code - make it possible to see what's happening as data arrives, while the script is running (Crt.Dialog.MessageBox "Found This: ...").
Alternatively, we have an example script, ReadDataFromHostFile-SendCommandsFromCommandsFile-LogResultsToIndividualFiles.txt , which you may find helpful. As the script name indicates, its purpose is to log different commands to different files. As far as I know, we haven't received reports of problems logging separately. That might be a good starting point.
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|