#!/bin/zsh

# Usage:
#  ssh-loglevel-verbose.sh HOST [ HOST ... ]
#
# Set sshd loglevel to VERBOSE on hosts
# including update of logbuch and daemon restart.

# FWIW: this should realy _much_ more general.  A script to do
#       arbitray changes on remote systems (maybe checking some
#       preconditions) and documenting them in the logbuch.  Would be
#       great...
#
# IDEA by Thomas: 1. extend logbuch tools to do and document
#                 arbitrarry actions.
#                 2. extend them to accept optional preconditions
#                 3. Add some mechanism to do call it an an arbitrarry
#                    list of remote hosts.

REMOTE_USER=root

check_LogLevel_is_VERBOSE()
# $1 is host to check
{
  ssh ${REMOTE_USER}@${1} 'cat /etc/ssh/sshd_config' | \
      grep -q '[[:space:]]*LogLevel[[:space:]]*VERBOSE'
}

set_LogLevel_VERBOSE()
# $1 is host to set the LogLevel on
{
  echo "Setting SSH LogLevel to VERBOSE on $host..."
  ssh ${REMOTE_USER}@${1} \
      "sed -i 's/\([[:space:]]*LogLevel\)[[:space:]]\+.*/\1 VERBOSE/' /etc/ssh/sshd_config;
       LOGBUCH_BATCHMODE=yes /usr/local/bin/log \
         \"`echo -e '/etc/ssh/sshd_config:\n    LogLevel VERBOSE\n* /etc/init.d/ssh restart'`\";
       /etc/init.d/ssh restart"
}

for host in "$@"
do
  if check_LogLevel_is_VERBOSE "$host" ; then
      echo "LogLevel on $host is already VERBOSE"
  else
    set_LogLevel_VERBOSE "$host"
  fi
done
