Created
September 8, 2014 20:03
-
-
Save jbreams/f9f5d6a7aafd26072cd3 to your computer and use it in GitHub Desktop.
AWS SSH Key Init Script
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 | |
# | |
# Init file for AWS Authorized Keyfile | |
# | |
# chkconfig: 2345 11 25 | |
# description: AWS Authorized Keyfile | |
case $1 in | |
start) | |
touch /var/lock/subsys/awssshkey | |
;; | |
stop) | |
rm /var/lock/subsys/awssshkey | |
exit 0 | |
;; | |
esac | |
if [ ! -d /root/.ssh ]; then | |
mkdir -m 0700 -p /root/.ssh | |
restorecon /root/.ssh | |
fi | |
# Get the root ssh key setup | |
ReTry=0 | |
rm -f /root/.ssh/authorized_keys | |
while [ ! -f /root/.ssh/authorized_keys ] && [ $ReTry -lt 5 ]; do | |
sleep 2 | |
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key \ | |
> /root/.ssh/authorized_keys | |
[ $? -eq 0 ] && echo "Added ssh keys $(< /root/.ssh/authorized_keys)" | |
ReTry=$[Retry+1] | |
done | |
chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You really want -s not -f, since the first curl will write a zero byte file and then the if while will exit. Also, you probably want this to loop forever until it gets keys.