Skip to content

Instantly share code, notes, and snippets.

@kenbarbour
Created February 24, 2017 16:46
Show Gist options
  • Save kenbarbour/8ac74448cf671048a517332babb16392 to your computer and use it in GitHub Desktop.
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
#!/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