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-01-2014, 01:54 AM
redespace redespace is offline
Registered User
 
Join Date: Oct 2014
Posts: 12
Question Create separate log file for every hostname

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
I tried something, but it is not working. I commented out those parts.
Please help .
Thank you
Reply With Quote
  #2  
Old 10-01-2014, 08:57 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Don't you want it to be?:

Code:
logfile = (strprofiledir & "\SecureCRT Logs\" & hostname & ".txt")
crt.Session.LogFileName = logfile
Your code did work in my tests (after enabling logging). Both the file was created and it contained results from a few commands I sent.

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
Reply With Quote
  #3  
Old 10-02-2014, 02:38 AM
redespace redespace is offline
Registered User
 
Join Date: Oct 2014
Posts: 12
Thumbs up

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
Thank you for your help!
Reply With Quote
  #4  
Old 10-02-2014, 02:55 AM
redespace redespace is offline
Registered User
 
Join Date: Oct 2014
Posts: 12
Lightbulb

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
Reply With Quote
  #5  
Old 10-02-2014, 09:42 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Reply With Quote
  #6  
Old 10-03-2014, 03:36 AM
redespace redespace is offline
Registered User
 
Join Date: Oct 2014
Posts: 12
Lightbulb

Hi bgagnon!

Quote:
There is still quite a bit unknown about your overall goal.
My goal is to have the script always running and when I jump into another device the script changes the tab name and also the log file name.

Quote:
How are you making the initial connection?
I open my default session of Secure CRT, which is (NETCSL001=Server 1). From there I jump to other network devices(Switches, Routers, CSS, ACE, etc). Sometimes it may happen that I use (NETCSS001=Server 2) as my starting jump point.

Quote:
And in those sessions, you have configured the Upon connect option in the Terminal / Log File category of Sessions Options.
Is that the case?
I have just one session. I have set my script to run as "Logon script:"



Do you know how can I add the desire comment (Date and month) upon connect to a new hostname?

Thank you!
Reply With Quote
  #7  
Old 10-03-2014, 11:30 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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{", ">")
It is common just to "wait for" the last character of the prompt (ie: # or >) in WaitForStrings(), but this is often problematic if your device spews any "legalese" during the connection process. If that single character is found at any point before the shell prompt (which is what it is assumed you *really* are waiting for), then the script will start executing at that point instead of where expected.


Code:
        crt.session.log (start)
        crt.session.log (append)
Both the above are using incorrect syntax and should just cause logging to be set to false.

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.
Attached Files
File Type: pdf Logon Actions_jumphost_scenario.pdf (729.0 KB, 1134 views)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #8  
Old 10-09-2014, 12:25 AM
redespace redespace is offline
Registered User
 
Join Date: Oct 2014
Posts: 12
Thank you bgagnon for your replies!

Quote:
First, what version of SecureCRT are you using?
I am using version 6.7.3 (build 292).

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)
I needed to add these entries, because "crt.session.Log(true),true" , was not working, as the script was always coming up with the error that Logging was already on, and it seems that you need that comment, because after changing the log file name, you have to re-enable logging.


Thank you
Reply With Quote
  #9  
Old 10-09-2014, 08:48 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hello redespace,

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.
OK, sounds like you are aware of the possible issues.

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
Reply With Quote
Reply

Tags
hop , hostname , jump , log file , logfilename

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:57 AM.