Skip to content

Instantly share code, notes, and snippets.

@jacquarg
Created December 26, 2013 21:28

Revisions

  1. jacquarg created this gist Dec 26, 2013.
    35 changes: 35 additions & 0 deletions addkey
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/sh


    # Read in the SSH key
    echo "Input the key to be added:"
    read key

    # Place the key in a temporary file (it's hard to get ssh-keygen
    # to read from stdin; <<< works for bash, but is non-posix)
    keyfile=$(tempfile) &&\
    echo "$key" > $keyfile

    # Generate a fingerprint
    fingerprint=$(ssh-keygen -lf $keyfile)

    # Check for errors
    if [ $(echo "$fingerprint" | egrep -c '(R|D)SA') -eq 0 ]
    then

    # Display the fingerprint error and clean up
    echo "Error: $fingerprint"
    rm $keyfile
    exit 1

    fi

    # Add the key to the authorised keys file and clean up
    mkdir -p .ssh &&\
    echo -n "no-agent-forwarding,no-port-forwarding,no-X11-forwarding " >> .ssh/authorized_keys &&\
    cat $keyfile >> .ssh/authorized_keys
    rm $keyfile

    # Display the fingerprint for reference
    echo "Success! Added a key with the following fingerprint:"
    echo $fingerprint