ssh login passwordless script
[bottlenecks.git] / utils / infra_setup / passwordless_SSH / set_passwordless_ssh.sh
1 #! /bin/bash
2
3 #this script is copied from https://github.com/guioconnor/Passwordless-SSH.
4 #the Bottlenecks project needs to set the machines login each other passwordless, this script is appropriate.
5
6 filename="id_rsa"
7 path="$HOME/.ssh"
8
9 if [ $1 ]
10 then
11     hostname=$1
12     if [ $2 ]
13     then
14         username=$2
15     else
16         username="$USER"
17     fi
18 else
19     # Read the host and username to store public key (the host/username accepting passwordless ssh from this computer)
20     echo "What host you want to grant passwordless SSH from this computer?"
21     read hostname
22     echo "What is your username on $hostname? ($USER?)"
23     read username
24
25     if [ ! $username ]
26     then
27         username="$USER"
28     fi
29 fi
30
31
32 # Generate rsa files
33 if [ -f $path/$filename ]
34 then
35     echo "RSA key exists on $path/$filename, using existing file"
36 else
37     ssh-keygen -t rsa -f "$path/$filename"
38     echo RSA key pair generated
39 fi
40
41 echo "We need to log into $hostname as $username to set up your public key (hopefully last time you'll use password from this computer)"
42 cat "$path/$filename.pub" | ssh "$hostname" -l "$username" '[ -d .ssh ] || mkdir .ssh; cat >> .ssh/authorized_keys; chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys'
43 status=$?
44
45 if [ $status -eq 0 ]
46 then
47     echo "Set up complete, try to ssh to $host now"
48     exit 0
49 else
50     echo "an error has occured"
51     exit 255
52 fi