Thursday, May 10, 2012

Check for logged users

At office, we have a cluster made of several machines used for performance experiments. We are not a lot of people, so we explicitly avoided installing software for launching experiments in a controlled way.

Nevertheless, seldom happens that someone remotely logs in and launches stuff, without noticing, for example, if other users were logged or where running programs, thus invalidating any output from (long) runs.

So, at last, I wrote the following bash script which, upon ssh login to the servers, outputs a fancy colored message telling that someone else is logged in (if any).

#!/bin/bash

#for user in $(last | grep still | sort | uniq);
ME=`whoami`



for user in $(last -Rw | grep still | sed 's/ .*//' | sort | uniq);
do
        if [ "$ME" != "$user" ]
        then
                echo -en '\E[;31m'"\033[1mWARNING:\033[0m"   # Red
                echo -en '\E[;34m'"\033[1m \033[4m$user\033[0m \033[0m"
                tput sgr0
                echo -n "still logged in since "
                echo `last -RFw | grep $user | head -1 | awk '{print $4" "$5" "$6}'`
        fi
done

I've simply added this script to /etc/bash.bashrc, so that every user gets a warning message, if needed.

2 comments:

  1. Interessante! io ho scoperto solo di recente l'esistenza dei comandi last e lastb :-)

    ReplyDelete
  2. Eh sì guarda, last è stra comodo! :)

    ReplyDelete