giuliomoro@73
|
1 #!/bin/bash
|
giuliomoro@73
|
2 IP_ADDRESS="192.168.7.2"
|
giuliomoro@73
|
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
|
giuliomoro@73
|
4 # ssh -i $HOME/.ssh/bbb_rsa root@192.168.7.2
|
giuliomoro@73
|
5 # in order to log in
|
giuliomoro@73
|
6 CONFIG_FILENAME=$HOME/.ssh/config
|
giuliomoro@73
|
7 CLEAN=0
|
giuliomoro@73
|
8 if [ $# -gt 0 ]; then
|
giuliomoro@73
|
9 while (( "$#" )); do
|
giuliomoro@73
|
10 if [ $1 = "-k" ]; then
|
giuliomoro@73
|
11 if [[ $2 == "/"* ]]; then
|
giuliomoro@73
|
12 #it's full path
|
giuliomoro@73
|
13 PRIVATE_KEY_FILENAME=$2
|
giuliomoro@73
|
14 else
|
giuliomoro@73
|
15 #assume it's a key in .ssh
|
giuliomoro@73
|
16 PRIVATE_KEY_FILENAME=$HOME'/.ssh/'$2
|
giuliomoro@73
|
17 fi
|
giuliomoro@73
|
18 shift
|
giuliomoro@73
|
19 shift
|
giuliomoro@73
|
20 continue
|
giuliomoro@73
|
21 fi
|
giuliomoro@73
|
22 if [ $1 = "-i" ]; then
|
giuliomoro@73
|
23 IP_ADDRESS=$2
|
giuliomoro@73
|
24 shift
|
giuliomoro@73
|
25 shift
|
giuliomoro@73
|
26 continue
|
giuliomoro@73
|
27 fi
|
giuliomoro@73
|
28 if [ $1 = "clean" ] ; then
|
giuliomoro@73
|
29 CLEAN=1
|
giuliomoro@73
|
30 shift
|
giuliomoro@73
|
31 continue
|
giuliomoro@73
|
32 fi
|
giuliomoro@73
|
33 #if we get here, then a wrong number or type of parameters have been passed
|
giuliomoro@73
|
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
|
giuliomoro@73
|
35 Usage:
|
giuliomoro@73
|
36 -i [ipAddress] sets the host to authenticate to (default: 192.168.7.1)
|
giuliomoro@73
|
37 -k [filename] sets the key file to use for autentication
|
giuliomoro@73
|
38 (default: ~/.ssh/id_rsa)"
|
giuliomoro@73
|
39 exit 1;
|
giuliomoro@73
|
40 done;
|
giuliomoro@73
|
41 fi
|
giuliomoro@73
|
42
|
giuliomoro@73
|
43 if [ $CLEAN -eq 1 ] ; then
|
giuliomoro@73
|
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) "
|
giuliomoro@73
|
45 read sure
|
giuliomoro@73
|
46 if [ $sure = "y" ] ; then
|
giuliomoro@73
|
47 ssh root@192.168.7.2 rm -rf .ssh &&\
|
giuliomoro@73
|
48 rm $PRIVATE_KEY_FILENAME ${PRIVATE_KEY_FILENAME}.pub $CONFIG_FILENAME
|
giuliomoro@73
|
49 if [ $? -ne 0 ] ; then
|
giuliomoro@73
|
50 printf "ERROR: error while cleaning"
|
giuliomoro@73
|
51 exit 4
|
giuliomoro@73
|
52 fi
|
giuliomoro@73
|
53 printf "Cleaning succesful"
|
giuliomoro@73
|
54 exit 0
|
giuliomoro@73
|
55 fi
|
giuliomoro@73
|
56 fi
|
giuliomoro@73
|
57 #printf "Pinging the Beaglebone on $IP_ADDRESS\n"
|
giuliomoro@73
|
58 #ping $IP_ADDRESS -w 1000 -c &>/dev/null 1 #returns 1 if ping is unsuccesful
|
giuliomoro@73
|
59 #if [ $? -ne 0 ] ; then # $? is the return value of the last command
|
giuliomoro@73
|
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" ;
|
giuliomoro@73
|
61 # exit 1
|
giuliomoro@73
|
62 #fi;
|
giuliomoro@73
|
63 #printf "...done\n"
|
giuliomoro@73
|
64
|
giuliomoro@73
|
65 mkdir -p $HOME/.ssh # create the ssh folder if it does not exist
|
giuliomoro@73
|
66 printf '\nHost bbb\nHostname '$IP_ADDRESS'\nUser root\nIdentityFile '$PRIVATE_KEY_FILENAME'\n' >> $HOME/.ssh/config
|
giuliomoro@73
|
67
|
giuliomoro@73
|
68 printf "Generating key $PRIVATE_KEY_FILENAME if it does not exist"
|
giuliomoro@73
|
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)
|
giuliomoro@73
|
70 if [ $? -ne 0 ] ; then
|
giuliomoro@73
|
71 printf "\nERROR: an error occurred while creating key pair $PRIVATE_KEY_FILENAME\n"
|
giuliomoro@73
|
72 exit 2
|
giuliomoro@73
|
73 fi
|
giuliomoro@73
|
74 printf "...Done\n"
|
giuliomoro@73
|
75
|
giuliomoro@73
|
76 printf "Type 'a' (without quotes) when prompted for a password: \n"
|
giuliomoro@73
|
77
|
giuliomoro@73
|
78 # StrictHostKeyChecking=no below will prevent the following message upon the first connection:
|
giuliomoro@73
|
79 # "The authenticity of host '192.168.1.2' can't be established."
|
giuliomoro@73
|
80 # which would require the user to type 'yes'
|
giuliomoro@99
|
81 cat ${PRIVATE_KEY_FILENAME}.pub | (ssh -q -o StrictHostKeyChecking=no bbb 'mkdir -p .ssh; cat >> .ssh/authorized_keys')
|
giuliomoro@73
|
82 if [ $? -ne 0 ] ; then
|
giuliomoro@73
|
83 printf "ERROR: An error occurred while copying the public key to the BBB\n"
|
giuliomoro@73
|
84 exit 3
|
giuliomoro@73
|
85 fi
|
giuliomoro@73
|
86 printf "SSH setup complete. You can now ssh into the beaglebone with command: ssh bbb\n"
|
giuliomoro@73
|
87 exit 0
|