Skip to content

Instantly share code, notes, and snippets.

@samloh84
Last active September 23, 2015 08:50
Show Gist options
  • Save samloh84/ba1cdbacc0443d1c4a45 to your computer and use it in GitHub Desktop.
Save samloh84/ba1cdbacc0443d1c4a45 to your computer and use it in GitHub Desktop.
Bash script to check remote servers via SFTP

Assign a value to the RECIPIENTS variable to enable email.

Assign a value to the LOG_FILE variable to enable logging to a file.

#!/bin/bash
#RECIPIENTS="email@domain.com email2@domain.com"
#LOG_FILE="/path/to/log"
if [[ $# -lt 2 ]]; then
printf "Usage: $0 host paths...\n"
exit 0
fi
HOST=$1
shift
SFTP_COMMANDS=""
for i in $@; do
SFTP_COMMANDS="${SFTP_COMMANDS}ls ${i}\n"
done
OUTPUT=`script -qc "printf \"${SFTP_COMMANDS}\" | sftp ${HOST}"`
echo "${OUTPUT}"
if [[ "${RECIPIENTS}" != "" ]]; then
printf "To: ${RECIPIENTS}\nSubject: [${HOST}][$(date)] SFTP Check\n${OUTPUT}" | sendmail "${RECIPIENTS}"
fi
if [[ "${LOG_FILE}" != "" ]]; then
printf "$(date)\n${OUTPUT}\n\n" >> "${LOG_FILE}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment