-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh-keys.sh
executable file
·62 lines (55 loc) · 1.44 KB
/
ssh-keys.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
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
## options:
## -l, --list list available private keys
## -n, --name <name> add the given key
## -a, --all add ALL the keys
# CLInt GENERATED_CODE: start
# No-arguments is not allowed
[ $# -eq 0 ] && sed -ne 's/^## \(.*\)/\1/p' $0 && exit 1
# Converting long-options into short ones
for arg in "$@"; do
shift
case "$arg" in
"--list") set -- "$@" "-l";;
"--name") set -- "$@" "-n";;
"--all") set -- "$@" "-a";;
*) set -- "$@" "$arg"
esac
done
function print_illegal() {
echo Unexpected flag in command line \"$@\"
}
# Parsing flags and arguments
while getopts 'hlan:' OPT; do
case $OPT in
h) sed -ne 's/^## \(.*\)/\1/p' $0
exit 1 ;;
l) _list=1 ;;
a) _all=1 ;;
n) _name=$OPTARG ;;
\?) print_illegal $@ >&2;
echo "---"
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
esac
done
# CLInt GENERATED_CODE: end
if [[ $_list ]]; then
ls ~/.ssh | grep -iv -e known_hosts -e pub
exit 0
fi
if [[ $_name ]]; then
fullname=$(find ~/.ssh -name "*$_name*" | grep -iv -e pub)
[[ ! -f "$fullname" ]] && echo "Could not find any file with keyword $_name" && exit 1
echo adding $fullname
ssh-add $fullname
exit 0
fi
if [[ $_all ]]; then
for file in $(ls ~/.ssh/ | grep -iv -e pub -e known_hosts); do
echo adding ~/.ssh/$file
ssh-add ~/.ssh/$file
done
fi