forked from LukeSmithxyz/LARBS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_key
42 lines (35 loc) · 1.18 KB
/
ssh_key
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
#!/bin/sh
# Usage: ssh_key without GH_TOKEN creates ssh key and shows public key
# ssh_key with GH_TOKEN creates ssh key and sets directly into Github
# Refer: https://unix.stackexchange.com/a/136898
# Main entry point
main() {
if [[ -e ~/.ssh ]] && [[ -d ~/.ssh ]]; then
echo ''
else
rm -rf ~/.ssh
ssh-keygen -t rsa -b 4096 -P '' -f ~/.ssh/id_rsa
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
fi
if ! ssh -T [email protected]; then
echo 'Add the public key to your git'
[[ -z $GH_TOKEN ]] && cat ~/.ssh/id_rsa.pub || add_ssh_gh
exit 1
fi
}
# function to configure SSH key in GitHub thru API
add_ssh_gh() {
set +e
curl --fail -H "Authorization: token $GH_TOKEN" https://api.github.com/user >/dev/null 2>&1
[[ $? -gt 0 ]] && echo 'Invalid TOKEN!' && exit 1
set -e
echo 'GitHub API works!'
title="$(cat /etc/hostname)_ssh"
ssh_pub_key=$(cat ~/.ssh/id_rsa.pub)
set +e
curl -H "Authorization: token $GH_TOKEN" --data "{\"title\":\"$title\",\"key\":\"$ssh_pub_key\"}" https://api.github.com/user/keys
[[ $? -gt 0 ]] && echo 'Provide Public Key write key access' && exit 1
set -e
echo 'Successfully added the key to GitHub'
}
main