Logging PuTTY sessions

As a DBA, it is generally a good idea to log all your activity when working on Unix/Linux servers. Logging provides an audit of changes that you have made to a system as well as being a valuable resource for notes and documentation. This article describes how to log all PuTTY sessions and how to automatically maintain the resulting log files in a logical directory structure based on date as follows:

Directory Structure
Directory Structure

First off, you need to enable logging as the default. Launch PuTTY and navigate to the Logging section and configure the options as shown. Unfortunately, we can’t specify the directory structure within PuTTY as it won’t automatically create directories if they don’t already exist.

PuTTY Logging options
PuTTY Logging options

Finally, navigate back to the Sessions list, highlight “Default Settings” and click Save. Annoyingly, if you have any saved sessions already, you will need to load each one, set the logging options and save it again (as there isn’t any way to  change logging globally).

PuTTY sessions
PuTTY sessions

At this point, all your PuTTY sessions will be logged to a single directory. However, after you’ve logged a few hundred sessions you’ll wish there was a nice easy way to keep the files organised. The easiest way is to create a BAT file with the following code, which you can either put in your startup folder or schedule to run (via the windows scheduler) on a regular basis:

@echo off
cd C:logs
for %%a in (*.*) do (
for /f "tokens=1,2,3 delims=/ " %%x in ("%%~ta") do md %%z%%y%%x&move "%%a" "%%z%%y%%x"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: