diff --git a/ssh-checker.sh b/ssh-checker.sh new file mode 100755 index 0000000..4fc7bb7 --- /dev/null +++ b/ssh-checker.sh @@ -0,0 +1,55 @@ +#!/bin/bash +######################################################################## +# Copyright (C) 2017 Max Mehl +######################################################################## +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +######################################################################## +# +# Reads hosts file and checks SSH access. If not possible with public +# key, this script tries to place the system's public key on the host +# via a normal (password-based) SSH access attempt. +# +######################################################################## + +CURDIR=$(dirname "$(readlink -f "$0")") +if [ ! -e "$CURDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi +source "$CURDIR"/config.cfg + +if [ ! -e "$HOSTS" ]; then echo "Missing hosts file. Please set a correct value of HOSTS= in your config file. Current value: $HOSTS"; exit 1; fi + +function trim { + sed -r -e 's/^\s*//g' -e 's/\s*$//g' +} + +while read line; do + if $(echo "$line" | grep -qE "^\s*#"); then continue; fi + + RHOST=$(echo "$line" | cut -d";" -f1 | trim) + + echo "[INFO] Trying ${RHOST}" + + STATUS=$(ssh -n -o BatchMode=yes -o ConnectTimeout=5 ${RHOST} "echo -n"; echo $?) + + if [ $STATUS != 0 ]; then + echo "[ERROR] No SSH login possible for ${RHOST}. Adding public key with password:" + cat ~/.ssh/id_rsa.pub | ssh ${RHOST} 'cat >> ~/.ssh/authorized_keys' + else + echo "[SUCCESS] SSH login possible for ${RHOST}." + fi + + echo + +done < "$HOSTS"