E-Mailbenachrichtigung bei Shell-Anmeldung

Auf meinen Web-Servern halte ich es so, dass ich bei einer Shell-Anmeldung (SSH oder lokale Konsole) eine E-Mail Benachrichtigung bekomme. So bekomme ich sofort mit, wenn sich jemand an der Maschine anmeldet – und das solle im Normalbetrieb nicht vorkommen.

Dazu wird in der Datei /etc/profile folgende Zeilen am Anfang eingefügt:
# 20140813 ASC Notify about logins
#              Usually there is no need to log into the web server so we
#              send a notification if this happens.
function NotifyShellAccess() {
  local SENDTO=$1
  local NOW=`date +"%d.%m.%Y %H:%M:%S"`
  local HOST=`hostname --fqdn`
  local TERMINAL=`ps -p $$ | awk '{print $2}' | tail -1`
  local LOGIN=`who | grep ${TERMINAL} | awk '{print $1}'`
  local REMOTEHOST=`who | grep ${TERMINAL} | awk '{print $6}' | cut -d"(" -f2 | cut -d")" -f1`
  echo "Shell access from ${REMOTEHOST} for user ${USER} on ${HOST} at ${NOW}." | mailx -s "SHELL ACCESS from ${REMOTEHOST} at ${NOW}" ${SENDTO}
}

NotifyShellAccess 'status@schaefer-it.net'

So bekomme ich dann eine eMail dir mir sagt wann, welcher Benutzer von welchem Computer aus sich angemeldet hat.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.