#!/bin/sh # Developed by Lubomir Host 'rajo' # Licensed under terms of GNU General Public License. # All rights reserved. full_bin="`which $0`" whichs="`which -a which`" keychain_bin="`which keychain`" keychain_bin=$HOME/bin/keychain realpath_bin="`which realpath`" if [ "${0##*/}" = "ssh-auto-add-key" ] then echo 'ssh-auto-add-key: This script should not be run like this, create symlink "ssh" instead' 1>&2 echo '' 1>&2 echo "cd ${PATH%%:*} && ln -s $full_bin ssh" 1>&2 echo '' 1>&2 exit 1 fi if [ -z "$keychain_bin" ] || [ ! -x $keychain_bin ]; then echo "keychain tool not found on your system" 1>&2 echo "keychain is required by ssh-auto-add-key" 1>&2 exit 1 fi if [ -z "$realpath_bin" ] || [ ! -x $realpath_bin ]; then echo "Warning: realpath tool not found on your system" 1>&2 fi ssh_bins="`which -a ssh`" for s in $ssh_bins; do [ "$s" = "$full_bin" ] && continue ssh_bin=$s; break; done #echo "ssh_bin=$ssh_bin"; skip_par=0 for p in $*; do case $p in -*) skip_par=1; continue; ;; *@*) server="${p##*@}" # user@server.com - remove "user@" prefix break ;; *) if [ $skip_par -eq 1 ]; then skip_par=0 ; continue; fi; server=$p break ;; esac; done #echo "server = $server"; keyfile="$HOME/.ssh/keys-by-host/$server" if [ -f "$keyfile" ]; then if [ ! -z "$realpath_bin" ] && [ -x "$realpath_bin" ]; then keyfile="`realpath $keyfile`" fi # execute keychain and then ssh binary "$keychain_bin" $keyfile && exec "$ssh_bin" $* else exec "$ssh_bin" $* fi