Last active
October 4, 2017 09:22
-
-
Save yugoslavskiy/538a473222923a4b9ec1 to your computer and use it in GitHub Desktop.
Script to create encrypted rsa keys and server profile (alias) in your ssh_config.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Script to create rsa keys and server profile (alias) in your ssh_config. | |
# | |
# created date: 20.03.2016 | |
# last update date: 11.05.2017 | |
# author: @yugoslavskiy | |
# | |
usage () { | |
echo " | |
Description: | |
Script to create rsa keys and server profile (alias) in your ssh_config. | |
Usage: | |
$(basename "$0") -s <server name> -i <ip address / domain name> [-u user_name] [-p port] [-c /path/to/ssh_config] | |
Where: | |
-h show this help message | |
-s set ssh server name (just for alias) | |
-u set user name for ssh connection (default: ${USER}) | |
-i set ip address / domain name of ssh server | |
-p set ssh port (default: 22) | |
-c set full path to ssh_config (default: ${ssh_congig_file})" | |
} | |
# default parametrs | |
#[[ -e /etc/ssh/ssh_config ]] && ssh_congig_file=/etc/ssh/ssh_config | |
[[ -e /etc/ssh/ssh_config ]] && ssh_congig_file="${HOME}/.ssh/config" | |
[[ -n ${ssh_congig_file} ]] || ssh_congig_file=None | |
port=22 | |
user_name=${USER} | |
while getopts ':hs:u::i:p::c::' option; do | |
case "$option" in | |
h) usage; echo '' ; exit ;; | |
c) ssh_congig_file=$OPTARG ;; | |
p) port=$OPTARG ;; | |
s) name_of_server=$OPTARG ;; | |
u) user_name=$OPTARG ;; | |
i) ip_address=$OPTARG ;; | |
:) printf "missing argument for -%s\n" "$OPTARG" >&2; usage; exit 1 ;; | |
\?) printf "illegal option: -%s\n" "$OPTARG" >&2; usage; exit 1 ;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [[ ! "${name_of_server}" ]] && [[ ! "${ip_address}" ]]; then | |
echo -e "\nYou have to give me more parametrs!\n" | |
usage | |
exit 1 | |
elif [[ "${ssh_congig_file}" == "None" ]]; then | |
echo -e "\nYou have to give me full path to ssh_config file (${ssh_congig_file} doesn't exists)\n" | |
usage | |
exit 1 | |
fi | |
# generate keys | |
new_ssh_key=$HOME/.ssh/id_rsa_${name_of_server}_$(date +%Y-%m-%d) | |
ssh-keygen -t rsa -b 4096 -N '' -f ${new_ssh_key} -C "key for ${name_of_server}" | |
# encrypt private key with openssl | |
mv ${new_ssh_key} ${new_ssh_key}.old | |
openssl pkcs8 -topk8 -v2 des3 -in ${new_ssh_key}.old -out ${new_ssh_key} | |
rm -f ${new_ssh_key}.old | |
chmod 0600 $new_ssh_key* | |
# Ask for the administrator password if needed | |
[[ -w ${ssh_congig_file} ]] || { | |
printf "\nI need root password for access to ${ssh_congig_file}:\n" | |
} | |
echo -e " | |
############################################### | |
# generetad with generate_ssh_rsa_keys script # | |
############################################### | |
Host ${name_of_server} | |
HostName ${ip_address} | |
Port ${port} | |
User ${user_name} | |
IdentityFile ${new_ssh_key}" | tee -a ${ssh_congig_file} >& /dev/null | |
printf "\nSo, what new in ${ssh_congig_file}:\n\n" | |
tail -n 8 ${ssh_congig_file} | |
printf "\nNow you can use ssh-copy-id tool to copy your new public key to ${name_of_server} server:\n" | |
printf "\$ ssh-copy-id ${name_of_server}\n" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment