capslock avatar

[Shell Script] Check SFTP Availability With shell script

capslock

Published: 31 Aug 2017 › Updated: 31 Aug 2017[Shell Script] Check SFTP Availability With shell script

[Shell Script] Check SFTP Availability With shell script

This article demonstrates how to use the community-distributed plugin check_sftp_avail in Nagios.

alt

If you have this issue :
[nagios@supervision plugins]# ./check_sftp -p 22 -H 192.168.0.1
WARNING: Host key verification failed - unable to authenticate server

Solve it by add the RSA Fingerprint :
[nagios@supervision plugins]# sftp maintenance@192.168.0.1
Connecting to 192.168.90.126...
The authenticity of host '192.168.90.126 (192.168.90.126)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.1' (RSA) to the list of known hosts.

#!/bin/sh
#
# Usage: ./check_sftp_avail [ -p  ] -H 
#
# Paths to commands used in this script.  These
# may have to be modified to match your system setup.

SFTP="/usr/bin/sftp"
RM="/bin/rm"

PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="1.0.0"

. $PROGPATH/utils.sh

print_usage() {
    echo "Usage: $PROGNAME [ -p port ] -H host"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "sftp service availability plugin for Nagios"
    echo ""
    support
}

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

port=115  # Default for sftp
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --hostname)
            box=$2
        shift
            ;;
        -H)
            box=$2
        shift
            ;;
        --port)
            port=$2
        shift
            ;;
        -p)
            port=$2
        shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

# Make sure a hostname and port have been specified
if [ -z "$box" -o -z "$port" ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

tempfile=/tmp/check_sftp-$box.tmp
exitstatus=$STATE_UNKNOWN

$SFTP -b - $box &>$tempfile <<EOF




EOF

if [ ! -e "$tempfile" ]; then
        stdio="No sftp output found" && $RM -f $tempfile && echo $stdio && exit $STATE_UNKNOWN;
fi

status=`tail -2 $tempfile`


if [ "`grep -c 'Permission denied' $tempfile`" == "1" ]; then
    exit=$STATE_OK && stdio="OK: service available"
elif [ "`grep -c 'Host key verification failed' $tempfile`" == "1" ]; then
    exit=$STATE_WARNING && stdio="WARNING: Host key verification failed - unable to authenticate server"
else
    exit=$STATE_CRITICAL && stdio="CRITICAL: service unavailable"
fi

$RM -f $tempfile
echo $stdio
exit $exit

Leave [Shell Script] Check SFTP Availability With shell script to:

Written by

Read more #dev posts


Best Posts From capslock

We have not curated any of capslock's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From capslock