giuliomoro@73: #!/bin/bash giuliomoro@73: IP_ADDRESS="192.168.7.2" giuliomoro@73: 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: # ssh -i $HOME/.ssh/bbb_rsa root@192.168.7.2 giuliomoro@73: # in order to log in giuliomoro@73: CONFIG_FILENAME=$HOME/.ssh/config giuliomoro@73: CLEAN=0 giuliomoro@73: if [ $# -gt 0 ]; then giuliomoro@73: while (( "$#" )); do giuliomoro@73: if [ $1 = "-k" ]; then giuliomoro@73: if [[ $2 == "/"* ]]; then giuliomoro@73: #it's full path giuliomoro@73: PRIVATE_KEY_FILENAME=$2 giuliomoro@73: else giuliomoro@73: #assume it's a key in .ssh giuliomoro@73: PRIVATE_KEY_FILENAME=$HOME'/.ssh/'$2 giuliomoro@73: fi giuliomoro@73: shift giuliomoro@73: shift giuliomoro@73: continue giuliomoro@73: fi giuliomoro@73: if [ $1 = "-i" ]; then giuliomoro@73: IP_ADDRESS=$2 giuliomoro@73: shift giuliomoro@73: shift giuliomoro@73: continue giuliomoro@73: fi giuliomoro@73: if [ $1 = "clean" ] ; then giuliomoro@73: CLEAN=1 giuliomoro@73: shift giuliomoro@73: continue giuliomoro@73: fi giuliomoro@73: #if we get here, then a wrong number or type of parameters have been passed giuliomoro@73: 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: Usage: giuliomoro@73: -i [ipAddress] sets the host to authenticate to (default: 192.168.7.1) giuliomoro@73: -k [filename] sets the key file to use for autentication giuliomoro@73: (default: ~/.ssh/id_rsa)" giuliomoro@73: exit 1; giuliomoro@73: done; giuliomoro@73: fi giuliomoro@73: giuliomoro@73: if [ $CLEAN -eq 1 ] ; then giuliomoro@73: 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: read sure giuliomoro@73: if [ $sure = "y" ] ; then giuliomoro@73: ssh root@192.168.7.2 rm -rf .ssh &&\ giuliomoro@73: rm $PRIVATE_KEY_FILENAME ${PRIVATE_KEY_FILENAME}.pub $CONFIG_FILENAME giuliomoro@73: if [ $? -ne 0 ] ; then giuliomoro@73: printf "ERROR: error while cleaning" giuliomoro@73: exit 4 giuliomoro@73: fi giuliomoro@73: printf "Cleaning succesful" giuliomoro@73: exit 0 giuliomoro@73: fi giuliomoro@73: fi giuliomoro@73: #printf "Pinging the Beaglebone on $IP_ADDRESS\n" giuliomoro@73: #ping $IP_ADDRESS -w 1000 -c &>/dev/null 1 #returns 1 if ping is unsuccesful giuliomoro@73: #if [ $? -ne 0 ] ; then # $? is the return value of the last command giuliomoro@73: # 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: # exit 1 giuliomoro@73: #fi; giuliomoro@73: #printf "...done\n" giuliomoro@73: giuliomoro@73: mkdir -p $HOME/.ssh # create the ssh folder if it does not exist giuliomoro@73: printf '\nHost bbb\nHostname '$IP_ADDRESS'\nUser root\nIdentityFile '$PRIVATE_KEY_FILENAME'\n' >> $HOME/.ssh/config giuliomoro@73: giuliomoro@73: printf "Generating key $PRIVATE_KEY_FILENAME if it does not exist" giuliomoro@73: 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: if [ $? -ne 0 ] ; then giuliomoro@73: printf "\nERROR: an error occurred while creating key pair $PRIVATE_KEY_FILENAME\n" giuliomoro@73: exit 2 giuliomoro@73: fi giuliomoro@73: printf "...Done\n" giuliomoro@73: giuliomoro@73: printf "Type 'a' (without quotes) when prompted for a password: \n" giuliomoro@73: giuliomoro@73: # StrictHostKeyChecking=no below will prevent the following message upon the first connection: giuliomoro@73: # "The authenticity of host '192.168.1.2' can't be established." giuliomoro@73: # which would require the user to type 'yes' giuliomoro@99: cat ${PRIVATE_KEY_FILENAME}.pub | (ssh -q -o StrictHostKeyChecking=no bbb 'mkdir -p .ssh; cat >> .ssh/authorized_keys') giuliomoro@73: if [ $? -ne 0 ] ; then giuliomoro@73: printf "ERROR: An error occurred while copying the public key to the BBB\n" giuliomoro@73: exit 3 giuliomoro@73: fi giuliomoro@73: printf "SSH setup complete. You can now ssh into the beaglebone with command: ssh bbb\n" giuliomoro@73: exit 0