#!/usr/bin/env bash

#  +-----------------------------------------------------------------------------------------------------------+
#  | Title        : ssh-version                                                                                |
#  |                                                                                                           |
#  | Description  : Shows version of the SSH server you are connecting to                                      |
#  |                                                                                                           |
#  | Author       : Sven Wick <sven.wick@gmx.de>                                                               |
#  | Contributors : Denis Meiswinkel                                                                           |
#  | URL          : https://github.com/vaporup/ssh-tools                                                       |
#  |                                                                                                           |
#  | Based On     : http://www.commandlinefu.com/commands/view/1809/get-the-version-of-sshd-on-a-remote-system |
#  +-----------------------------------------------------------------------------------------------------------+

ssh_opts=(
    -o BatchMode=yes
    -o CheckHostIP=no
    -o StrictHostKeyChecking=no
    -o HashKnownHosts=no
    -o ConnectTimeout=16
    -o PasswordAuthentication=no
    -o PubkeyAuthentication=no
)

#
# Usage/Help message
#

function usage() {

cat << EOF

    Usage: ${0##*/} hostname

EOF

}

if [[ -z $1 || $1 == "--help" ]]; then
    usage
    exit 1
fi

if [[ $1 == *"@"* ]]; then
    SERVER=$( echo ${1##*@} )
else
    SERVER=${1}
fi

SSH_VERSION=$(ssh -vN "${ssh_opts[@]}" "$@" -l ForceLogin2Fail 2>&1 | grep "remote software version")
echo ${SSH_VERSION#debug1: }
