morganestes
9/2/2015 - 5:46 PM

Create an SSH shortcut and key shell script.

Create an SSH shortcut and key shell script.

#!/bin/bash

echo "Please enter username on server: "
read USER

echo "Please enter server host or IP address (ex: opticllc.com)"
read SERVER_HOST

echo "Please enter server name (aka ssh shortcut)"
read SERVER_NAME

# USER="rtvenge"
# SERVER_NAME="ryantvenge.com"

# add shortcut to ssh config file
echo -e "Host $USER \n    HostName $SERVER_HOST \n    Port 22 \n    User $USER \n    IdentityFile ~/.ssh/$USER" >> ~/.ssh/config

# Try to create an ssh directory and create authorized_keys file on server
ssh $SERVER_NAME 'mkdir ~/.ssh && touch ~/.ssh/authorized_keys'

# create ssh key
ssh-keygen -f ~/.ssh/$USER -C "$SERVER_NAME" |

# wait for ssh-keygen to complete
wait

# ssh into server and copy ssh key up
cat ~/.ssh/$USER.pub | ssh $USER 'cat >> .ssh/authorized_keys'