#1
|
|||
|
|||
![]()
Hi All,
I would like to have a separate log file for every hostname I connect to. I know that in the logging options there is a %H variable, but in my case it is not helpful. The reason is that I have a Linux server I connect to, and I telnet/ssh from there to different hosts. I may sometimes even do a third jump from a secondary device. What I would like to do is to read the hostname from the screen (everything previous to "#" or ">") and use the hostname as the log file name. Currently I have a script running that changes the tabname based on the hostname. I would like to add the log file part to it: Code:
Sub Main() DIM ScreenString DIM StartPos DIM Waiting 'DIM strprofiledir '---- Get profile dir from windows ---- 'Set wshShell = CreateObject( "WScript.Shell" ) 'strprofiledir = wshShell.ExpandEnvironmentStrings( "%USERPROFILE%" ) '---- Get profile dir from windows ---- Do ScreenString = crt.Screen.WaitForStrings ("#", "bash-3.2$", "netcss001{", "}:", ">") If ScreenString = 1 then Set objTab = crt.GetScriptTab() nRow = objTab.Screen.CurrentRow hostname = objTab.screen.Get(nRow, 0, nRow, objTab.Screen.CurrentColumn - 2) hostname = Trim(hostname) crt.window.caption = hostname 'logfile = (strprofiledir & "\SecureCRT Logs\" & hostname & ".txt") 'crt.Session.LogFileName = hostname End If If ScreenString = 2 then crt.window.caption = "NETCSL001" End If If ScreenString = 3 then crt.window.caption = "NETCSS001" End If If ScreenString = 4 then crt.window.caption = "NETCSS001" End If If ScreenString = 5 then Set objTab = crt.GetScriptTab() nRow = objTab.Screen.CurrentRow MyString = objTab.screen.Get(nRow, 0, nRow, objTab.Screen.CurrentColumn) nColumn = InStr(MyString, "@") hostname = objTab.screen.Get(nRow, nColumn + 1, nRow, objTab.Screen.CurrentColumn - 2) hostname = Trim(hostname) crt.window.caption = hostname Waiting = crt.Screen.WaitForStrings( "quit", "exit", "sername", "ogin", "q") End If Loop End Sub Please help . Thank you |
#2
|
|||
|
|||
Hello redespace,
It sounds like the detect prompt portion is working for you if you are successfully changing the tab name, but there is a very good block of code in the scripting manual provided on our website here that also explains how it can be done. It's in section 7.2, but I just search for the term heuristic. (It does not appear often.) ![]() So what were the results when you attempted to add the hostname variable to your log file specification? Did you get an error? Did the script seem to hang? If so, is Cancel available in the Script menu? (And if so, and you select it, what is the error?) I see where you set up logging and defined the filename, but not where you actually enabled logging (crt.Session.Log True). It does not seem that you would only want logging in certain "cases" (two of five If/Then cases), but I am not exactly sure what your overall goal is. Please elaborate. I don't even see that you are sending any commands so that the results can be logged. (Or was that just code you held back?) However, I am a bit puzzled why you went to the trouble of concatenating a full path to be saved in the variable logfile, but then set crt.Session.LogFileName to hostname: Code:
logfile = (strprofiledir & "\SecureCRT Logs\" & hostname & ".txt") crt.Session.LogFileName = hostname Code:
logfile = (strprofiledir & "\SecureCRT Logs\" & hostname & ".txt") crt.Session.LogFileName = logfile A good troubleshooting mechanism would be to add a message box to display what is contained in logfile (or hostname) to be sure it is a valid path (where you have write permissions) and that it is a filename that is acceptable on your OS. Windows OS does not allow the following characters to be used in filenames: \/:*?"<>|
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
![]()
Thank you bgagnon!
I made the mistake of assigning the Logfilename to hostame instead of logfile. That helped, but for some reason, the files were being created, but there was nothing being logged. I tried setting "crt.Session.Log True", but an error came up saying that logging was already enabled.. I did some searching, and it seems I had to start and append. Now is working!!! I'm sharing the code just in case someone else needs it. Code:
Sub Main() DIM ScreenString DIM StartPos DIM Waiting Do '==1.Any Cisco Hostname ; 2. Server1 ; 3. Server 2; 4. Any Juniper device== ScreenString = crt.Screen.WaitForStrings ("#", "bash-3.2$", "netcss001{", ">") If ScreenString = 1 then Set objTab = crt.GetScriptTab() nRow = objTab.Screen.CurrentRow hostname = objTab.screen.Get(nRow, 0, nRow, objTab.Screen.CurrentColumn - 2) hostname = Trim(hostname) crt.window.caption = hostname crt.session.log (start) crt.session.log (append) logfile = ("H:\Sync\SecureCRT\Logs\" & hostname & ".txt") crt.Session.LogFileName = logfile crt.session.Log(true),true Waiting = crt.Screen.WaitForStrings( "quit", "exit", "sername", "ogin", "q") End If If ScreenString = 2 then crt.window.caption = "NETCSL001" crt.session.log (start) crt.session.log (append) logfile = ("H:\Sync\SecureCRT\Logs\NETCSL001.txt") crt.Session.LogFileName = logfile crt.session.Log(true),true Waiting = crt.Screen.WaitForStrings( "quit", "exit", "sername", "ogin", "q") End If If ScreenString = 3 then crt.window.caption = "NETCSS001" crt.session.log (start) crt.session.log (append) logfile = ("H:\Sync\SecureCRT\Logs\NETCSS001.txt") crt.Session.LogFileName = logfile crt.session.Log(true),true Waiting = crt.Screen.WaitForStrings( "quit", "exit", "sername", "ogin", "q") End If If ScreenString = 4 then Set objTab = crt.GetScriptTab() nRow = objTab.Screen.CurrentRow MyString = objTab.screen.Get(nRow, 0, nRow, objTab.Screen.CurrentColumn) nColumn = InStr(MyString, "@") '==If no "@" before ">", Do nothing== If nColumn = 0 then Exit Do hostname = objTab.screen.Get(nRow, nColumn + 1, nRow, objTab.Screen.CurrentColumn - 2) hostname = Trim(hostname) crt.window.caption = hostname crt.session.log (start) crt.session.log (append) logfile = ("H:\Sync\SecureCRT\Logs\" & hostname & ".txt") crt.Session.LogFileName = logfile crt.session.Log(true),true Waiting = crt.Screen.WaitForStrings( "quit", "exit", "sername", "ogin", "q") End If Loop End Sub |
#4
|
|||
|
|||
![]()
I would like to improve the script now, by adding the date and month every time I connect to a new host.
I was looking into the logging options of Secure CRT, and I can add custom data to the logs upon connect. I added: ========== %D/%M ========== The idea would be to have the line mentioned above every new day, so that when searching through the log files, I could quickly get to the device and date I want. Unfortauntely it didn't work, because Secure CRT only adds the custom data to my first jump point (the server 1). If I telnet/ssh from there to another device, the custom data won't get added. Please help me write a script (to add to my original one) that adds the date and month to the log file only once per day. Is it possible? Thank you |
#5
|
|||
|
|||
Hello redespace,
There is still quite a bit unknown about your overall goal. How are you making the initial connection? It sounds like you have a specific number of sessions that you use for the initial connection before you actually run the script: '==1.Any Cisco Hostname ; 2. Server1 ; 3. Server 2; 4. Any Juniper device== And in those sessions, you have configured the Upon connect option in the Terminal / Log File category of Sessions Options. Is that the case? If so, you are absolutely correct. The Upon connect configuration is only applicable to that initial connection. When you are on that jump host and, at that host's shell prompt, type ssh IP_address or telnet IP_address, you have now transferred control to that Telnet or SSH client that is running on the jump host. SecureCRT does not know you have made a connection from the jump host machine to another remote machine (after all it is just a command that is executed by the jump host server). How do you run your script? Manually via the Script menu? As a Logon script in the Connection / Logon Actions category? If my understanding of your scenario is accurate, you will have to stop session logging and actually write the desired comment to the log file when a jump connection is made.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#6
|
|||
|
|||
![]()
Hi bgagnon!
Quote:
Quote:
Quote:
Do you know how can I add the desire comment (Date and month) upon connect to a new hostname? Thank you! |
#7
|
|||
|
|||
Hello redespace,
Thanks for the clarification and additional info. I'm just going to throw this out there, but you know, since you have already started configuring Logon Actions in your jump host (gateway session), another approach would be to duplicate that session, add additional entries that send the ssh IP_address or telnet IP_address command and then you could actually *rename* the copy of the original session by final destination IP and that would solve the tab caption dilemma as well. (See attached for a simple example -- the bottom one.) That way all session logging configured would be specific to that end destination device. Of course this is not a logical approach if you have 2000 devices you need to reach from the jump host. ![]() However, if you are determined to perform this via a logon script, I think at this point we have to take a step back as there a few issues with the current version of the script. First, what version of SecureCRT are you using? Not to nitpick but here are some of the "pitfalls" of the current script: Code:
ScreenString = crt.Screen.WaitForStrings ("#", "bash-3.2$", "netcss001{", ">") Code:
crt.session.log (start) crt.session.log (append) Please review SecureCRT's Help topic Scripting / Script Objects Reference / Session Object for the correct syntax associated with the Log() method. Let me know about the version. I have an example script in mind that I think will be simpler for your objective.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#8
|
|||
|
|||
Thank you bgagnon for your replies!
Quote:
Regarding the "pitfalls", indeed is not 100% safe proof, but every time I detect an error, I create an exception on the script. The banner of devices are mostly standard, so I know what to expect after telneting or SSHing to a device. Code:
crt.session.log (start) crt.session.log (append) Thank you |
#9
|
|||
|
|||
Hello redespace,
Quote:
I have attached the example script I had in mind. However, seeing your new post (which my colleague, Todd, responded to), you may also want to check out the Excel example for "GatewayLogins". Note that the example script in the attachment presently references an entry from the session INI file that is only applicable to SecureCRT v7.2 and later. However, there is also code for earlier versions, you will just need to comment and uncomment as noted (lines 64-70).
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
![]() |
Tags |
hop , hostname , jump , log file , logfilename |
Thread Tools | |
Display Modes | Rate This Thread |
|
|