#!/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://codeberg.org/vaporup/ssh-tools                                                     |
# | Based On     : http://www.commandlinefu.com/commands/view/1809/get-the-version-of-sshd-on-a-remote-system |
# +-----------------------------------------------------------------------------------------------------------+

VERSION="ssh-version (ssh-tools) 1.9"

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

#
# Usage/Help message
#

function usage() {

cat << EOF

    Usage: ${0##*/} [OPTIONS] hostname

    EXAMPLES:

        ${0##*/} 127.0.0.1

        ${0##*/} -p 35007 127.0.0.1
EOF

}

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

if [[ $1 == "--version" ]]; then
    echo "${VERSION}"
    exit
fi

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