-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_ssh_config.sh
executable file
·57 lines (48 loc) · 1.43 KB
/
update_ssh_config.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
#!/usr/bin/env bash
set -e
function print_green() {
echo -e "\033[32m$1\033[39m"
}
REPO='[email protected]/magnet-cl/keygen.git'
SSH_CONFIG_FILE='ssh_config'
IDENTITY_FILE_PATH=''
print_usage() {
printf "Usage:
\t-i FILE
\t\tSpecify identity file for ssh hosts.\n
"
}
while getopts 'i:' flag; do
case "${flag}" in
i) IDENTITY_FILE_PATH="${OPTARG}" ;;
*)
print_usage
exit 1
;;
esac
done
print_green "Obtaining ssh config from keygen repository (read access required)"
git archive --remote=ssh://$REPO master $SSH_CONFIG_FILE | tar -x
mkdir -p $HOME/.ssh/config.d
mv -f $SSH_CONFIG_FILE $HOME/.ssh/config.d/magnet
if [[ "$OSTYPE" == "darwin"* ]]; then
print_green "Adding MacOS configuration to hosts"
perl -pi -e 's/(.*)user .*/$&\n\1AddKeysToAgent yes\n\1UseKeychain yes/g' $HOME/.ssh/config.d/magnet
fi
if [ ! -z "$IDENTITY_FILE_PATH" ]; then
print_green "Adding identity file to host configuration"
perl -pi -e "s|(.*)user magnet|\$&\n\1IdentityFile $IDENTITY_FILE_PATH\n\1IdentitiesOnly yes|g" $HOME/.ssh/config.d/magnet
fi
if [ ! -f "$HOME/.ssh/config" ]; then
print_green "Creating ssh config"
cat >$HOME/.ssh/config <<EOF
Include config.d/*
EOF
elif ! grep -Fxq "Include config.d/*" $HOME/.ssh/config; then
# code if not found
print_green "Including config.d/* in ssh config"
echo -e "Include config.d/*\n$(cat $HOME/.ssh/config)" >$HOME/.ssh/config
else
# code if found
print_green "config.d/* already included in ssh config"
fi