Skip to content

Instantly share code, notes, and snippets.

@arnehormann
Last active February 23, 2016 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnehormann/6028664 to your computer and use it in GitHub Desktop.
Save arnehormann/6028664 to your computer and use it in GitHub Desktop.
#!/bin/bash
# stop on errors and undeclared variables
set -e -u -o pipefail
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
@arnehormann
Copy link
Author

All cronjobs on a root accessible computer to markdown (call as lscronjobs HOSTNAME):

lscronjobs() {
    echo "## SERVER $1"
    ssh -o ConnectTimeout=1 -o ConnectionAttempts=2 "root@$1" 'bash -s' <<-'LS_CRONS'
        set -u -o pipefail
        for f in /etc/crontab /etc/cron.d/*
        do
            [ -f "$f" ] \
                && echo -e "\n### FILE $f\n" \
                && sed -e '/^$/d' -e 's/^/    /' < "$f" -e '/^[ \t#]+$/d'
        done
        for u in $(getent passwd | cut -f1 -d:)
        do
            crontab -u "$u" -l >& /dev/null \
                && echo -e "\n### USER $u\n" \
                && ( \
                    crontab -u "$u" -l 2> /dev/null \
                    | sed -e '/^$/d' -e 's/^/    /' -e '/^[ \t#]+$/d' )
        done
    LS_CRONS
    echo
}

@arnehormann
Copy link
Author

Print a help text for environment variables used in the current file.
The variables must have a postfix comment starting with #@@ (Bash 4):

used_env() {
  echo "Environment variables influencing ${BASH_SOURCE}:"
  while IFS='' read -r LINE ; do
    # delete all before double-@; inject "" so the line is not matched itself
    local HELPTEXT="${LINE##*@""@}"
    if [[ "${HELPTEXT}" == "${LINE}" ]]; then
      continue
    fi
    local NAME="${LINE%%=*}"
    local CURRENT_VALUE="${!NAME}"
    echo -n -e "  ${NAME}:\t${HELPTEXT} [${CURRENT_VALUE}]"

    # find default value
    local DEFAULT_VALUE="${LINE#*=\"\${*:-}"
    DEFAULT_VALUE="${DEFAULT_VALUE%%\}\"*}"
    if [[ "${#DEFAULT_VALUE}" && "${CURRENT_VALUE}" != "${DEFAULT_VALUE}" ]] ; then
      echo ", default is '${DEFAULT_VALUE}'"
    else
      echo ""
    fi
  done < "${BASH_SOURCE}"
}

e.g.

BASE_DIR="${BASE_DIR:-$(pwd)}"         #@@ base directory for ssh tunnel
KEY_DIR="${KEY_DIR:-${BASE_DIR}/.ssh}" #@@ ssh keys for tunnel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment