Created
February 24, 2017 16:46
-
-
Save kenbarbour/8ac74448cf671048a517332babb16392 to your computer and use it in GitHub Desktop.
Placed in my ~/.ssh directory and run when a new .pub file is added. Creates an authorized_keys file with the contents of every .pub file in the directory
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 | |
KEYFILES=~/.ssh/*.pub #All public key files in git/.ssh | |
AUTHFILE=~/.ssh/authorized_keys #File to store authorized keys | |
echo 'Building authorized keys file' | |
# Clear current authorized keys file | |
if [ -e $AUTHFILE ]; then | |
# Avoid overwriting old bakup files | |
if [ -e $AUTHFILE.bak ]; then #If backup already exists | |
BAKINC=1 #Counter for backup files | |
while [ -e $AUTHFILE$BAKINC.bak ]; do | |
((BAKINC++)) | |
done | |
touch $AUTHFILE$BAKINC.bak | |
cat $AUTHFILE >> $AUTHFILE$BAKINC.bak | |
echo Backed up $AUTHFILE to $AUTHFILE$BAKINC.bak | |
else #If backup file doesnt exist | |
touch $AUTHFILE.bak | |
cat $AUTHFILE >> $AUTHFILE.bak | |
echo Backed up $AUTHFILE to $AUTHFILE.bak | |
fi | |
cat /dev/null > $AUTHFILE | |
else | |
touch $AUTHFILE | |
chmod 740 $AUTHFILE | |
fi | |
echo "" | |
# Add keys to authorized keys file | |
shopt -s nullglob | |
for f in $KEYFILES | |
do | |
cat $f >> $AUTHFILE | |
echo Added key: $f | |
done | |
echo Keys built successfully | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment