Merge "Change PTL informatin in INFO"
[bottlenecks.git] / utils / infra_setup / passwordless_SSH / set_passwordless_ssh.sh
1 #! /bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 #this script is copied from https://github.com/guioconnor/Passwordless-SSH.
12 #the Bottlenecks project needs to set the machines login each other passwordless, this script is appropriate.
13
14 filename="id_rsa"
15 path="$HOME/.ssh"
16
17 if [ $1 ]
18 then
19     hostname=$1
20     if [ $2 ]
21     then
22         username=$2
23     else
24         username="$USER"
25     fi
26 else
27     # Read the host and username to store public key (the host/username accepting passwordless ssh from this computer)
28     echo "What host you want to grant passwordless SSH from this computer?"
29     read hostname
30     echo "What is your username on $hostname? ($USER?)"
31     read username
32
33     if [ ! $username ]
34     then
35         username="$USER"
36     fi
37 fi
38
39
40 # Generate rsa files
41 if [ -f $path/$filename ]
42 then
43     echo "RSA key exists on $path/$filename, using existing file"
44 else
45     ssh-keygen -t rsa -f "$path/$filename"
46     echo RSA key pair generated
47 fi
48
49 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)"
50 cat "$path/$filename.pub" | ssh "$hostname" -l "$username" '[ -d .ssh ] || mkdir .ssh; cat >> .ssh/authorized_keys; chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys'
51 status=$?
52
53 if [ $status -eq 0 ]
54 then
55     echo "Set up complete, try to ssh to $host now"
56     exit 0
57 else
58     echo "an error has occured"
59     exit 255
60 fi