-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.sh
executable file
·66 lines (56 loc) · 1.96 KB
/
init.sh
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
#! /bin/bash
## # ssh-agent plugin
##
## Provides several functions and aliases to manage a single instance of a ssh-agent across
## multiple terminal instances.
##
## Requires: fzf and/or gum to provide fuzzy search
SSH_AGENT_DEFAULT_ALIAS='default'
SSH_AGENT_MUTEX_HOME=${HOME}
SSH_AGENT_MUTEX=${SSH_AGENT_MUTEX_HOME}/.current_ssh_agent
SSH_HOME=$HOME/.ssh
ssh_agent_list_identities() {
echo "> Loaded Identities"
ssh-add -l
}
ssh_agent_activate_current() {
### Activates the current ssh agent.
alias=${1:-${SSH_AGENT_DEFAULT_ALIAS}}
echo "Activating ${alias} ssh-agent: $SSH_AGENT_PID"
source $SSH_AGENT_MUTEX.${alias}
ssh_agent_list_identities
}
ssh_agent_list() {
### List existing agents
find "${SSH_AGENT_MUTEX_HOME}" -maxdepth 1 -name '.current_ssh_agent.*' | cut -d '.' -f 3
}
ssh_agent_create() {
### Creates a new ssh-agent and store its environment variables into $SSH_AGENT_MUTEX
alias=${1:-${SSH_AGENT_DEFAULT_ALIAS}}
ssh-agent >"$SSH_AGENT_MUTEX.${alias}"
ssh_agent_activate_current "${alias}"
echo "${alias} ssh-agent created: $SSH_AGENT_PID"
}
ssh_agent_kill_current() {
### Kills current ssh agent.
echo "Killing current ssh agent: $SSH_AGENT_PID"
kill -9 "$SSH_AGENT_PID"
grep -n -e "$SSH_AGENT_PID" ${SSH_AGENT_MUTEX}.* | cut -d ':' -f 1 | xargs -i{} echo rm {} \;
unset "$SSH_AGENT_PID"
}
ssh_agent_add_identities() {
ssh_agent_list_identities
echo "------------"
find "$SSH_HOME" -type f -not -name '*.pub' -not -name 'known_*' -printf '%f\n' | gum choose --no-limit | xargs -o -I {} ssh-add "$SSH_HOME"/{}
}
ssh_agent_add_identity() {
ssh_agent_list_identities
echo "------------"
find "$SSH_HOME"/ -type f -not -name '*.pub' -not -name 'known_*' -printf '%f\n' | fzf | xargs -o -I {} ssh-add "$SSH_HOME"/{}
}
alias ssh-agent-create=ssh_agent_create
alias sac=ssh_agent_activate_current
alias sal=ssh_agent_list_identities
alias saa=ssh_agent_add_identity
alias sas=ssh_agent_add_identities
alias sad=ssh_agent_remove_identity