-
Notifications
You must be signed in to change notification settings - Fork 0
/
sam
executable file
·110 lines (92 loc) · 2.7 KB
/
sam
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
function help() {
echo -e " ___ _ __ __
/ __| /_\ | \/ |
\__ \/ _ \| |\/| |
|___/_/ \_\_| |_|
${GREEN}SSH Agent Manager${NC}
${YELLOW}Usage:${NC}
sam <command> [<args>]
${YELLOW}Some useful sam commands are:${NC}
${GREEN}ls${NC} lists fingerprints of all keys added
${GREEN}add${NC} add <key> (private key) to agent
${GREEN}rm${NC} delete <key> from the agent
${GREEN}all${NC} load all available keys to agent
${GREEN}clean${NC} remove all keys from the agent
${GREEN}copy${NC} copy <key> (public key) to clipboard
${GREEN}start${NC} start ssh-agent service
${GREEN}stop${NC} stop ssh-agent service
${GREEN}fix${NC} fix ~/.ssh permissions
------
${GREEN}gen${NC} generate ssh key pair
${GREEN}deploy${NC} upload pub <key> to a remote server"
exit 1
}
function err() { echo -e "${RED}$* ${NC}"; }
function yes() { echo -e "${GREEN}$* ${NC}"; }
function all() {
keys=$(find "$HOME"/.ssh -maxdepth 1 -name '*.pub' -exec basename {} \;)
for k in $keys; do
$add_cmd "${k//.pub/}"
done
}
function usual() { ssh-add -t "$lifetime" "$HOME/.ssh/$1"; }
function passwordstore() {
! [ -x "$(command -v pass)" ] && err '=> pass not installed' && exit 1
! [ -x "$(command -v expect)" ] && err '=> expect not installed' && exit 1
passw=$(pass "ssh/$1")
expect <<EOF
log_user 0
spawn ssh-add -t $lifetime $HOME/.ssh/$1
expect "Enter passphrase"
send "$passw\r"
expect eof
EOF
}
function copy() {
if [ -f "/etc/os-release" ]; then
! [ -x "$(command -v xclip)" ] && err '=> xclip not installed' && exit 1
xclip -sel clip "$HOME/.ssh/$keyfile.pub" | yes "Public key '$2' copied to clipboard!"
else
cat "$HOME/.ssh/$keyfile.pub" | pbcopy |yes "Public key '$2' copied to clipboard!"
fi
}
function fix() {
find "$HOME/.ssh" -type f -exec chmod 600 {} \;
find "$HOME/.ssh" -name '*.pub' -type f -exec chmod 644 {} \;
chmod 644 "$HOME/.ssh/known_hosts"
chmod 644 "$HOME/.ssh/config"
chmod 700 "$HOME/.ssh"
}
function deploy() {
pubkey="$HOME/.ssh/${prefix}$2.pub"
ssh-copy-id -f -i "$pubkey" "$3"
}
function main() {
[[ -z "$1" ]] && help
keyfile="${prefix}$2"
case "$1" in
ls) ssh-add -l ;;
add) $add_cmd "$keyfile" ;;
rm) ssh-add -d "$HOME/.ssh/$keyfile" ;;
all) all ;;
clean) ssh-add -D ;;
copy) copy "$keyfile" "$2" ;;
fix) fix ;;
gen) ssh-keygen -t rsa -b 4096 -C "$2" ;;
deploy) deploy "$@" ;;
*)
err "ERROR: Unknown command '$1'"
exit 2
;;
esac
}
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
#add_cmd='usual'
add_cmd=passwordstore
prefix=''
lifetime='1d'
main "$@"