Bashscript: Easy ssh-keygen to ssh server

Bashscript: Easy ssh-keygen to ssh server

To create a Private/Public key on your server with ssh and connect to a remote one you can just run the script below on your client pc with:

./ssh-keygen-now-script.sh ssh.your-ssh-server.com 2233
#!/bin/bash
#

# Depending on ssh version maybe its file: 
# .ssh/authorized_keys2
# Change the permissions of .ssh to 700
# Change the permissions of .ssh/authorized_keys2 to 640
#
# User should be added on remote site with adduser <CUSTOMUSER>
#

# Infos about the remote server:
HOST=$1
PORT=$2
USER=root

# Just enter through, 
# def dir to home dir of atm user
ssh-keygen -t rsa

#if not available create dir
echo; echo "Create .ssh dir on $HOST ..."
ssh -p $PORT -l $USER $HOST 'mkdir -p .ssh'

sleep 1
echo; echo "Copy file to $HOST ..."
# ssh-copy-id -i ~/.ssh/id_rsa.pub -p$PORT $USER@$HOST
cat ~/.ssh/id_rsa.pub | ssh -p $PORT -l $USER $HOST 'cat >> .ssh/authorized_keys'

echo; echo "Finished and copied. success."

echo; echo -n "Test now? (y/n): "; read yn
echo; echo; sleep 1

if [ $yn == "y" ];then
   ssh -p $PORT -l $USER $HOST
else
   echo "bye"
fi

exit 0;