Created
November 9, 2012 13:30
-
-
Save tabletick/4045672 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Name: putSSHPubKey.sh | |
# Purpose: Adds SSH public Key to remote Host | |
# Author: Tabletick | |
# Date: 2012/11/06 | |
# | |
# Explanation: adds the public ssh key to the AUTHORIZED_KEYS file on a remote server | |
# Works only in this particular configuration to shows that it's possible. _Username_ and Keyfile | |
# is therefore hardcoded. | |
# Edit this | |
HOSTUSER="tabletick" # SSH user with password access on the remote server | |
HOSTIDFILE="<keyfile>" # Path to the keyfile to deploy (e.g ~/.ssh/id_rsa.pub) | |
# Defaults | |
APPSCP=/usr/bin/scp | |
APPSSH=/usr/bin/ssh | |
APPSSHPASS=/usr/bin/sshpass | |
APPSSHCOPYID=/usr/bin/ssh-copy-id | |
APPECHO=/bin/echo | |
# Redirect all output to a file instead of stdout, clean the file first | |
PATHOUTPUTFILE="`date +%Y%m%d%H%M%S`-putkey.sh.log" | |
${APPECHO} '' > "${PATHOUTPUTFILE}" | |
exec 1>>"${PATHOUTPUTFILE}" | |
exec 2>>"${PATHOUTPUTFILE}" | |
# Process parameters | |
for parameter in $@; do | |
case "${1}" in | |
'-l') # Process host list | |
shift | |
[ -e $1 ] && PATHHOSTS="${1}" || die "remote host list not found." | |
# Process the host list and run the script remotely if you haven't died yet | |
while read line; do | |
[[ "${line}" =~ "#" ]] && continue # Skip all comments in the host-file | |
HOSTIP=`echo $line | awk -F '|' '{print $5}'` | |
HOSTNAME=`echo $line | awk -F '|' '{print $1 }'` | |
# Drop the key on the remote-host | |
${APPECHO} "trying to connect to $HOSTNAME" | |
${APPSSHPASS} -f pass ${APPSSHCOPYID} -i ${HOSTIDFILE} ${HOSTUSER}@${HOSTNAME} | |
# Finish-message for this host | |
${APPECHO} -e "done with: $HOSTNAME\n- - - - - - -" | |
done < "${PATHHOSTS}" # Read from the Host file | |
shift | |
;; | |
# Other parameters might follow... | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment