-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy-ssh-git.sh
41 lines (32 loc) · 1.25 KB
/
lazy-ssh-git.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
#!/bin/bash
while true; do
read -rp "Your account email address ([email protected]): " email
if [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]] ; then break; fi
echo "Incorrect email format"
done
while true; do
read -rp "Hostname for your account (my-github): " hostname
if [[ "$hostname" =~ ^[^\s]+$ ]]; then break; fi
echo "Spaces are not allowed, choose another hostname"
done
while true; do
read -rp "Domain of the git service (github.com): " domain
if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then break; fi
echo "Incorrect domain format"
done
echo "Generating key pair..."
ssh-keygen -t rsa -b 4096 -C "$email" -f ~/.ssh/"$hostname"_key -N '' > /dev/null
echo "Copy SSH public key and add it to SSH keys settings of your account:"
cat ~/.ssh/"$hostname"_key.pub
echo "As soon as it's ready, press any key to continue... "
read -n 1 -s -r
echo "# Created by lazy-ssh-git script
Host $hostname
HostName $domain
IdentityFile ~/.ssh/${hostname}_key
IdentitiesOnly yes
" >> ~/.ssh/config
if [ -z "$SSH_AGENT_PID" ] || ! ps -p "$SSH_AGENT_PID" > /dev/null; then eval "$(ssh-agent)"; fi
echo "Connecting to git@$hostname..."
ssh-keyscan "$hostname" 2>/dev/null >> ~/.ssh/known_hosts
ssh -T git@"$hostname"