Mercurial > hg > beaglert
comparison scripts/setup-ssh.sh @ 108:3068421c0737 ultra-staging
Merged default into ultra-staging
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 18 Aug 2015 00:35:15 +0100 |
parents | f03d68f18d7f |
children |
comparison
equal
deleted
inserted
replaced
54:d3f869b98147 | 108:3068421c0737 |
---|---|
1 #!/bin/bash | |
2 IP_ADDRESS="192.168.7.2" | |
3 PRIVATE_KEY_FILENAME=$HOME/.ssh/id_rsa #if you change this to something like bbb_rsa it will be safer when cleaning but if you don't have the config file you will have to | |
4 # ssh -i $HOME/.ssh/bbb_rsa root@192.168.7.2 | |
5 # in order to log in | |
6 CONFIG_FILENAME=$HOME/.ssh/config | |
7 CLEAN=0 | |
8 if [ $# -gt 0 ]; then | |
9 while (( "$#" )); do | |
10 if [ $1 = "-k" ]; then | |
11 if [[ $2 == "/"* ]]; then | |
12 #it's full path | |
13 PRIVATE_KEY_FILENAME=$2 | |
14 else | |
15 #assume it's a key in .ssh | |
16 PRIVATE_KEY_FILENAME=$HOME'/.ssh/'$2 | |
17 fi | |
18 shift | |
19 shift | |
20 continue | |
21 fi | |
22 if [ $1 = "-i" ]; then | |
23 IP_ADDRESS=$2 | |
24 shift | |
25 shift | |
26 continue | |
27 fi | |
28 if [ $1 = "clean" ] ; then | |
29 CLEAN=1 | |
30 shift | |
31 continue | |
32 fi | |
33 #if we get here, then a wrong number or type of parameters have been passed | |
34 echo "Generates a key pair (if does not exist) and copies it over to the host, saving the settings in the /.ssh/config file | |
35 Usage: | |
36 -i [ipAddress] sets the host to authenticate to (default: 192.168.7.1) | |
37 -k [filename] sets the key file to use for autentication | |
38 (default: ~/.ssh/id_rsa)" | |
39 exit 1; | |
40 done; | |
41 fi | |
42 | |
43 if [ $CLEAN -eq 1 ] ; then | |
44 printf "Cleaning ${PRIVATE_KEY_FILENAME}, ${PRIVATE_KEY_FILENAME}.pub, ${CONFIG_FILENAME} and bbb:~/.ssh/ . Are you sure? This might prevent you from accessing other services that use the same key or config files (y/n) " | |
45 read sure | |
46 if [ $sure = "y" ] ; then | |
47 ssh root@192.168.7.2 rm -rf .ssh &&\ | |
48 rm $PRIVATE_KEY_FILENAME ${PRIVATE_KEY_FILENAME}.pub $CONFIG_FILENAME | |
49 if [ $? -ne 0 ] ; then | |
50 printf "ERROR: error while cleaning" | |
51 exit 4 | |
52 fi | |
53 printf "Cleaning succesful" | |
54 exit 0 | |
55 fi | |
56 fi | |
57 #printf "Pinging the Beaglebone on $IP_ADDRESS\n" | |
58 #ping $IP_ADDRESS -w 1000 -c &>/dev/null 1 #returns 1 if ping is unsuccesful | |
59 #if [ $? -ne 0 ] ; then # $? is the return value of the last command | |
60 # echo "Error: the Beaglebone is not alive, make sure it is connected and drivers are installed (MacOs and Windows only) or try again later" ; | |
61 # exit 1 | |
62 #fi; | |
63 #printf "...done\n" | |
64 | |
65 mkdir -p $HOME/.ssh # create the ssh folder if it does not exist | |
66 printf '\nHost bbb\nHostname '$IP_ADDRESS'\nUser root\nIdentityFile '$PRIVATE_KEY_FILENAME'\n' >> $HOME/.ssh/config | |
67 | |
68 printf "Generating key $PRIVATE_KEY_FILENAME if it does not exist" | |
69 ls $PRIVATE_KEY_FILENAME &>/dev/null || ssh-keygen -t rsa -f $PRIVATE_KEY_FILENAME -q -P "" # the command after || will be executed only if the previous command fails (i.e. if id_rsa does not exist) | |
70 if [ $? -ne 0 ] ; then | |
71 printf "\nERROR: an error occurred while creating key pair $PRIVATE_KEY_FILENAME\n" | |
72 exit 2 | |
73 fi | |
74 printf "...Done\n" | |
75 | |
76 printf "Type 'a' (without quotes) when prompted for a password: \n" | |
77 | |
78 # StrictHostKeyChecking=no below will prevent the following message upon the first connection: | |
79 # "The authenticity of host '192.168.1.2' can't be established." | |
80 # which would require the user to type 'yes' | |
81 cat ${PRIVATE_KEY_FILENAME}.pub | (ssh -q -o StrictHostKeyChecking=no bbb 'mkdir -p .ssh; cat >> .ssh/authorized_keys') | |
82 if [ $? -ne 0 ] ; then | |
83 printf "ERROR: An error occurred while copying the public key to the BBB\n" | |
84 exit 3 | |
85 fi | |
86 printf "SSH setup complete. You can now ssh into the beaglebone with command: ssh bbb\n" | |
87 exit 0 |