Skip to content

Instantly share code, notes, and snippets.

@kjunggithub
Forked from ryantology/install-keys.sh
Created August 5, 2014 14:06

Revisions

  1. Ryan White renamed this gist Jul 20, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Ryan White revised this gist Jul 20, 2014. 1 changed file with 57 additions and 51 deletions.
    108 changes: 57 additions & 51 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,51 +1,57 @@
    #!/bin/bash
    # vim:set ts=4 sw=4 et ai:
    # Retrieve the SSH public key and install it for subsequent login attempts.

    AUTHORIZED_KEYS=/root/.ssh/authorized_keys

    TMP_KEY=/tmp/openssh_id.pub
    CURL=/usr/bin/curl
    CURLOPTS="--retry 3 --retry-delay 2 --silent --fail -o $TMP_KEY"

    KEY_URL=http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
    KEY_FILE=/mnt/openssh_id.pub

    SUCCESS=0
    ATTEMPT=0
    MAX_ATTEMPTS=10

    if [ ! -d `dirname $AUTHORIZED_KEYS` ]; then
    mkdir -p -m 700 `dirname $AUTHORIZED_KEYS`
    fi

    while [ $SUCCESS -eq 0 -a $ATTEMPT -lt $MAX_ATTEMPTS ] ; do
    # attempt to retrieve the SSH public key and install it
    if [ -f $KEY_FILE ] ; then
    cat $KEY_FILE > $AUTHORIZED_KEYS
    echo "SSH key added to $AUTHORIZED_KEYS from $KEY_FILE"
    SUCCESS=1
    else
    $CURL $CURLOPTS $KEY_URL
    if [ $? -eq 0 -a -f $TMP_KEY ]; then
    cat $TMP_KEY > $AUTHORIZED_KEYS
    echo "SSH key added to $AUTHORIZED_KEYS from $KEY_URL"
    rm -f $TMP_KEY
    SUCCESS=1
    fi
    fi

    # print out status and wait for a bit if we failed
    ATTEMPT=$(($ATTEMPT + 1))
    if [ $SUCCESS -eq 1 ]; then
    echo "SSH key retrieval attempt $ATTEMPT failed"
    sleep 5
    fi
    done

    # either we got it or we just gave up
    if [ -f $AUTHORIZED_KEYS ]; then
    chmod 600 $AUTHORIZED_KEYS
    else
    echo "-=[ FATAL ]=- SSH key could not be retrieved!!!"
    fi
    #!/bin/bash
    # vim:set ts=4 sw=4 et ai:
    # Retrieve SSH public keys from GitHub and install to authorized_keys.

    GITHUB_USERS=(USER1 USER2)
    INSTALL_FILE=/home/USER/.ssh/authorized_keys
    TMP_KEY=/tmp/ssh.key

    CURL=/usr/bin/curl
    CURLOPTS="--retry 3 --retry-delay 2 --silent --fail -o $TMP_KEY"

    if [ ! -d `dirname $INSTALL_FILE` ]; then
    mkdir -p -m 700 `dirname $INSTALL_FILE`
    fi

    # Backup existing key
    if [ -f $INSTALL_FILE ]; then
    mv $INSTALL_FILE $INSTALL_FILE.bak
    fi


    for GITHUB_USER in ${GITHUB_USERS[@]}; do
    SUCCESS=0
    ATTEMPT=0
    MAX_ATTEMPTS=10
    while [ $SUCCESS -eq 0 -a $ATTEMPT -lt $MAX_ATTEMPTS ] ; do
    # attempt to retrieve the SSH public key and install it
    KEY_URL="https://github.com/$GITHUB_USER.keys"

    $CURL $CURLOPTS $KEY_URL
    if [ $? -eq 0 -a -f $TMP_KEY ]; then
    cat $TMP_KEY >> $INSTALL_FILE
    echo "" >> $INSTALL_FILE
    echo "SSH key added to $INSTALL_FILE from $KEY_URL"
    rm -f $TMP_KEY
    SUCCESS=1
    fi

    # print out status and wait for a bit if we failed
    ATTEMPT=$(($ATTEMPT + 1))
    if [ $SUCCESS -eq 0 ]; then
    echo "SSH key retrieval attempt $ATTEMPT failed"
    sleep 5
    fi
    done
    done

    # either we got it or we just gave up
    if [ -f $INSTALL_FILE ]; then
    chmod 600 $INSTALL_FILE
    echo "SSH Keys Installed"
    else
    echo "-=[ FATAL ]=- SSH key could not be retrieved!!!"
    if [ -f $INSTALL_FILE.bak ]; then
    mv $INSTALL_FILE.bak $INSTALL_FILE
    fi
    fi
  3. @codeslinger codeslinger created this gist Jul 31, 2008.
    51 changes: 51 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/bin/bash
    # vim:set ts=4 sw=4 et ai:
    # Retrieve the SSH public key and install it for subsequent login attempts.

    AUTHORIZED_KEYS=/root/.ssh/authorized_keys

    TMP_KEY=/tmp/openssh_id.pub
    CURL=/usr/bin/curl
    CURLOPTS="--retry 3 --retry-delay 2 --silent --fail -o $TMP_KEY"

    KEY_URL=http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
    KEY_FILE=/mnt/openssh_id.pub

    SUCCESS=0
    ATTEMPT=0
    MAX_ATTEMPTS=10

    if [ ! -d `dirname $AUTHORIZED_KEYS` ]; then
    mkdir -p -m 700 `dirname $AUTHORIZED_KEYS`
    fi

    while [ $SUCCESS -eq 0 -a $ATTEMPT -lt $MAX_ATTEMPTS ] ; do
    # attempt to retrieve the SSH public key and install it
    if [ -f $KEY_FILE ] ; then
    cat $KEY_FILE > $AUTHORIZED_KEYS
    echo "SSH key added to $AUTHORIZED_KEYS from $KEY_FILE"
    SUCCESS=1
    else
    $CURL $CURLOPTS $KEY_URL
    if [ $? -eq 0 -a -f $TMP_KEY ]; then
    cat $TMP_KEY > $AUTHORIZED_KEYS
    echo "SSH key added to $AUTHORIZED_KEYS from $KEY_URL"
    rm -f $TMP_KEY
    SUCCESS=1
    fi
    fi

    # print out status and wait for a bit if we failed
    ATTEMPT=$(($ATTEMPT + 1))
    if [ $SUCCESS -eq 1 ]; then
    echo "SSH key retrieval attempt $ATTEMPT failed"
    sleep 5
    fi
    done

    # either we got it or we just gave up
    if [ -f $AUTHORIZED_KEYS ]; then
    chmod 600 $AUTHORIZED_KEYS
    else
    echo "-=[ FATAL ]=- SSH key could not be retrieved!!!"
    fi