10
Aug
07

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}


10 Responses to “Logging SSH sessions”


  1. 1 lewus Aug 29th, 2007 at 1:31 pm

    Hi Miljan,

    I was reading your note about “How to Change Default System Dump Device in AIX”. You can check out the dumpcheck command (full path is /usr/lib/ras/dumpcheck). Hint: dumpcheck -p ;)

    Regards,
    Levente

  2. 2 miljan Sep 1st, 2007 at 3:29 am

    Hey Levente,

    Nice to see you here. :) dumpcheck looks very useful, I will use it next time for sure. Thanx for the tip. ;)

    BTW, how is it back in the homeland? :)

  3. 3 lewus Sep 3rd, 2007 at 3:18 pm

    it’s sweeeeeeeeeeeet :)

  4. 4 miljan Sep 3rd, 2007 at 3:38 pm

    Hehehehe… thought so. :) But I bet you miss us a lot. :P

  5. 5 sms Apr 29th, 2008 at 10:21 am

    nice one! thank you! :)

  6. 6 LGee Jul 16th, 2008 at 10:11 am

    Well, this is a bit late, but I have read this entry recently.

    First, there is PuTTY for Linux as well if you need (more or less) the same. It works fine, but you don’t have the ‘window context menu’ (no Duplicate Session or Change Settings etc.) Second, the apostrophes used in the above code are not suitable for direct copy paste into the shell (they are not ‘typewriter apostrophes’, rather ‘acute accents’.)

    LGee

  7. 7 miljan Jul 16th, 2008 at 10:17 am

    Hello LGee, thanx for dropping by. :)

    Yes, there is PuTTY for Linux, but why would you use it when you have “normal” ssh client from the console? I don’t like using additional tools unless it is really necessary.

    And you are also right about the quotes and apostrophes, it is the problem of WordPress software which is used on this blog. Unfortunately, there is no workaround for this.

  8. 8 Grusskarten kostenlos Oct 7th, 2008 at 12:23 pm

    thanks for the information but Linux is very good..

  9. 9 Yury Dec 2nd, 2009 at 5:45 am

    There is an alternative: just use tee command.
    example:

    ssh user@host | tee -a my-nice.log

    that’s it.

  10. 10 bloguser Nov 9th, 2010 at 7:42 pm

    Awesome! This last command save me lots of time.. and is very useful…
    Regards and Thank You to Yury.

Comments are currently closed.