Skip to content

Instantly share code, notes, and snippets.

@kaji-bikash
Last active January 17, 2018 23:42

Revisions

  1. kaji-bikash revised this gist Jan 17, 2018. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions simple_pubkey_appender.sh
    Original file line number Diff line number Diff line change
    @@ -9,14 +9,20 @@ fi

    #Assumptions
    #Default user deploy whose SSH authorized_keys is to modify
    #SSH authorized_keys is in the format ssh-rsa key
    #SSH authorized_keys is in the format ssh-rsa key
    home=${home:-deploy}
    username=$1
    name=`curl --silent https://api.github.com/users/kajisaap | grep "name" | awk -F: '{ print $2}' | cut -d "," -f1`
    name=`curl --silent https://api.github.com/users/$username | grep "name" | awk -F: '{ print $2}' | cut -d "," -f1`

    prompt() {
    echo ""
    echo "You are adding $username - $name"
    read -p "To avoid unintended pubkey additions, press ENTER/RETURN to continue: "

    read -n1 -r -p "To avoid unintended additions, press any key to continue..." key

    #if ! [ "$key" = "y" ]; then
    # exit 1;
    #fi
    }

    prompt;
    @@ -30,7 +36,7 @@ ssh_auth_file=`eval echo ~${home}/.ssh/authorized_keys`
    ##Append found keys to authorized_keys file
    while read line
    do
    echo $line >> ${ssh_auth_file}
    echo "$line ${username}" >> ${ssh_auth_file}
    done <"/tmp/${username}.pub"

    #Simulate idempotence/de-duplication
    @@ -40,7 +46,8 @@ mv ${ssh_auth_file}.uniq ${ssh_auth_file}


    if [ $? -eq "0" ]; then
    echo ""
    echo "SUCCESS : $name - $username added"; exit 0;
    fi

    echo ""
    echo "FAILED: $name - $username not added"; exit 1;
  2. kaji-bikash revised this gist Jan 17, 2018. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion simple_pubkey_appender.sh
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,14 @@ fi
    #SSH authorized_keys is in the format ssh-rsa key
    home=${home:-deploy}
    username=$1
    name=`curl --silent https://api.github.com/users/kajisaap | grep "name" | awk -F: '{ print $2}' | cut -d "," -f1`

    prompt() {
    echo "You are adding $username - $name"
    read -p "To avoid unintended pubkey additions, press ENTER/RETURN to continue: "
    }

    prompt;

    #get public key from a github
    KEY="https://github.com/$username.keys"
    @@ -27,4 +35,12 @@ done <"/tmp/${username}.pub"

    #Simulate idempotence/de-duplication
    cat $ssh_auth_file | sort -u -k1,2 > ${ssh_auth_file}.uniq
    mv ${ssh_auth_file}.uniq ${ssh_auth_file}
    mv ${ssh_auth_file}.uniq ${ssh_auth_file}



    if [ $? -eq "0" ]; then
    echo "SUCCESS : $name - $username added"; exit 0;
    fi

    echo "FAILED: $name - $username not added"; exit 1;
  3. kaji-bikash revised this gist Jan 17, 2018. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion simple_pubkey_appender.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    #! /bin/bash
    #!/bin/bash

    usage() { echo "Usage: $0 github-username" 1>&2; exit 1; }

    if [ $# -lt "1" ]; then
    usage;
    fi


    #Assumptions
    #Default user deploy whose SSH authorized_keys is to modify
    #SSH authorized_keys is in the format ssh-rsa key
  4. kaji-bikash created this gist Jan 15, 2018.
    22 changes: 22 additions & 0 deletions simple_pubkey_appender.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #! /bin/bash
    #Assumptions
    #Default user deploy whose SSH authorized_keys is to modify
    #SSH authorized_keys is in the format ssh-rsa key
    home=${home:-deploy}
    username=$1

    #get public key from a github
    KEY="https://github.com/$username.keys"
    curl --silent $KEY -o /tmp/$username.pub

    ssh_auth_file=`eval echo ~${home}/.ssh/authorized_keys`

    ##Append found keys to authorized_keys file
    while read line
    do
    echo $line >> ${ssh_auth_file}
    done <"/tmp/${username}.pub"

    #Simulate idempotence/de-duplication
    cat $ssh_auth_file | sort -u -k1,2 > ${ssh_auth_file}.uniq
    mv ${ssh_auth_file}.uniq ${ssh_auth_file}