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

For reference: SSH debugging

First export these vars to make the next part copy-pastable:

export PORT=2222
export USER="me"
export HOST="example.com"
export PRIVATE_KEY="~/.ssh/id_rsa"

Test an SFTP client:
sftp -vvv -o Port="${PORT}" -o IdentityFile="${PRIVATE_KEY}" "${USER}@${HOST}"

Test an SSH server (no deamon, intensive logging to stderr):
/usr/sbin/sshd -ddde -o Port="${PORT}"

Also SSH server, a little more serious:
strace /usr/sbin/sshd -D -q -e -o Port="${PORT}"

@arnehormann
Copy link
Author

some useful functions

# SSH tunnel to remote MySQL; call as "sshmysql SSH_LOGIN@HOST"
alias sshmysql='ssh -L 3306:localhost:3306 -N'

# Recursively download a website with all assets; call as "download-website http://SITE.URL"
alias download-website='wget --mirror --page-requisites --html-extension --convert-links'

# serve current directory and open it in a browser (port may be given, default is 8080)
servecd() {
  python -m SimpleHTTPServer ${1:-8080} &
  open http://localhost:${1:-8080}
}

# open source of python module in editor
pysource() { subl -w $(python -c "import ${1}; import inspect; print inspect.getsourcefile(${1})"); }

# OS X specific

# man page as pdf
pman() { man -t "${1}" | open -f -a /Applications/Preview.app; }

# sometimes my Macbook imagines there's a CD when there's not
alias ejectcd='drutil tray eject'

# Sublime Text
export EDITOR_BIN='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
export EDITOR="$EDITOR_BIN -w"
export VISUAL=$EDITOR
alias subl="$EDITOR_BIN"

@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