diff options
Diffstat (limited to 'ssh-agent-loadkey')
-rwxr-xr-x | ssh-agent-loadkey | 96 |
1 files changed, 79 insertions, 17 deletions
diff --git a/ssh-agent-loadkey b/ssh-agent-loadkey index 1207a05..a335561 100755 --- a/ssh-agent-loadkey +++ b/ssh-agent-loadkey @@ -1,31 +1,93 @@ #!/bin/bash # -# Load a key into the ssh-agent +# Load a key from a menu. # # Parameters BASENAME="`basename $0`" KEYS="$HOME/.ssh/keys" -TYPE="$1" -HANDLE="$2" -KEY="$KEYS/$TYPE/$HANDLE" # Check -if [ -z "$HANDLE" ]; then - echo "usage: $BASENAME <keytype> <handle>" - echo "available keys:" - echo "" - ( cd $HOME/.ssh/keys && find -name '*.pub' ) | grep -v decomissioned | sed -e 's/^/\t/' - exit 1 -elif [ ! -e "$KEY" ]; then - echo "$BASENAME: file not found: $KEY" +if [ ! -d "$KEYS" ]; then + echo "$BASENAME: folder not found: $KEYS" exit 1 fi -# Check if the selected option has a custom procedure (monkeysphere, keyringer, etc) -if [ -x "$KEY.askpass" ]; then - # SSH-ADD(1) says: "Note that on some machines it may be necessary to redirect the input from /dev/null to make this work". - SSH_ASKPASS="$KEYS.askpass" ssh-add $KEY < /dev/null +# Get available keys +function __query { + ( + cd $KEYS && find -name '*.pub' | sed -e 's/.pub$//' | grep -v decomissioned | while read line; do + handle="`echo $line | cut -d '/' -f 3`" + type="`echo $line | cut -d '/' -f 2`" + echo "$handle ($type)" + done + ) +} + +# List available keys +function __list { + n="0" + __query | sort | uniq | while read key; do + echo -en "$n. $key" + echo "" + let ++n + done | column -t -c 6 +} + +# Display the keys available in the agent +function __loaded { + #ssh-add -L | cut -d ' ' -f 3 | sed -e 's/^/\t/' + + ssh-add -L | while read line; do + handle="$(basename `echo $line | cut -d ' ' -f 3`)" + type="`echo $line | cut -d ' ' -f 1 | sed -e 's/^ssh-//'`" + echo "$handle ($type)" + done | column -t -c 6 +} + +# Key chooser mennu +function __chooser { + echo "Usage: $BASENAME <keytype> <handle>" + echo "" + echo "Available keys" + echo "" + __list | sed -e 's/^/\t/' + echo "" + echo "Current loaded keys:" + echo "" + __loaded | sed -e 's/^/\t/' + echo "" + + read -rep "Choose key: " n + + # Check the selected option + if [ ! -z "$n" ]; then + key="$(__list | grep -E "(^$n.| $n:)" | sed -e "s/^[0-9]*. //" | cut -d : -f 1)" + + if [ ! -z "$key" ]; then + __load $key + fi + fi +} + +# Load a key +function __load { + HANDLE="$1" + TYPE="`echo $2 | sed -e 's/(//' -e 's/)//'`" + KEY="$KEYS/$TYPE/$HANDLE" + + # Check if the selected option has a custom procedure (monkeysphere, keyringer, etc) + if [ -x "$KEY.askpass" ]; then + # SSH-ADD(1) says: "Note that on some machines it may be necessary to redirect the input from /dev/null to make this work". + SSH_ASKPASS="$KEY.askpass" ssh-add $KEY < /dev/null + else + ssh-add $KEY + fi +} + +# Dispatch +if [ ! -z "$2" ]; then + __load $* else - ssh-add $KEY + __chooser fi |