Search This Blog

2012-12-08

Logging SSH Session Output

Its generally a good idea to keep some kind of log of your any changes/activities on a machine that you may be changing. One good way is to ensure you have a large enough history and ensure that you are time-stamping that history as well as discussed in Bash History previously.

The best way to keep a log of specific activities that are being outputted to the screen during an ssh session though can be done multiple ways.

Putty Logging

putty logging window
PuTTY Logging Options
Under the Session options in putty you can specify varying logging levels of output. The problem I have found with these options is that they are generally not formatted as they are displayed on the screen at the time of logging. What is beneficial though is that you can capture more detail than the secondary method I use.

PuTTY does have the option to append/overwrite log files for each session but there is no concept of log rotation unfortunately.

Tee

The second method and my favourite method for its simplicity is to "tee" the output of an ssh session to the file. That can be done as follows from any existing terminal window:
ssh user@remote.server.com | tee logfile
Be aware that using tee will overwrite any existing files of the same name and will not be as verbose as PuTTY in logging contents of open applications (such as the changes to a file when using vi)

Script

This method is developed for logging and collaboration of terminal output
You can log the output of commands to a file using the "script" command and specify many different options for logging.
script -af /path/to/file

The options are as follows;
-a - append to log file instead of overwriting
-f - flush the results of the file for easy collaboration with co-workers after each command is typed they can check the results using tail -f

No comments:

Post a Comment