Archive for August 10th, 2007

10
Aug

Logging SSH sessions

One of the main rules when working on production servers is to keep trace of your actions. That is, so called, “cover your ass” policy. :) As most of my colleagues in IBM are using Windows on their workstations (strange isn’t it?!) they are using putty which provides logging options for their SSH sessions. But I am using Linux and OpenSSH client does not provide this luxury so I had to create this short script to save my SSH logs. It will start SSH client with all the parameters you pass over the command line but at the same time it will also start script command and log everything in right log file. Very neat. :)

#!/bin/sh
 
USER=$(whoami)
LOG_FOLDER=/log/ssh/${USER}
DATE=$(date +’%Y-%m-%d_%H:%M’)
 
case “$1″ in
    ’hostA’)
        HOST=”admin@hostA.example.com”
        ;;
    ’hostB’)
        HOST=”admin@hostB.example.com”
        ;;
    *)
        HOST=$1
        ;;
esac
 
LOG_FILE=${LOG_FOLDER}/${HOST}_${DATE}.log
 
[ ! -d ${LOG_FOLDER} ] && mkdir -p ${LOG_FOLDER}
 
shift
 
script -c “ssh ${HOST} $*” ${LOG_FILE}