aboutsummaryrefslogtreecommitdiff
path: root/ssh-agent-loadkey-menu
blob: 29a8e6dd0d65b9449f98ad09830285550f1d8e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
#
# Load a key from a menu.
#

# Parameters
BASENAME="`basename $0`"
KEYS="$HOME/.ssh/keys"

# Check
if [ ! -d "$KEYS" ]; then
  echo "$BASENAME: folder not found: $KEYS"
  exit 1
fi

# Get available keys
function __query {
  ( cd $KEYS && find -name '*.pub' | sed -e 's/.pub$//' | grep -v decomissioned )
}

# 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 {
  echo "Current loaded keys:"
  echo ""

  ssh-add -L | cut -d ' ' -f 3 | sed -e 's/^/\t/'
}

# Key chooser mennu
function __chooser {
  echo "Available keys"
  echo ""
  __list | sed -e 's/^/\t/'
  echo ""
  __loaded
  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 {
  KEY="$KEYS/$1"

  # 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
__chooser