Skip to content

Instantly share code, notes, and snippets.

@ia
Created July 28, 2022 12:17
Show Gist options
  • Save ia/e888a7bb6b2679a0642efb861d5993d5 to your computer and use it in GitHub Desktop.
Save ia/e888a7bb6b2679a0642efb861d5993d5 to your computer and use it in GitHub Desktop.
Quick-&-dirty helper script to generate ssh key pair & related config section
#!/usr/bin/env bash
set -e
set -x
usage()
{
echo "Usage: ${0} localhostname username serverhostname [ssh_alias remotehostname]"
echo "Info:"
echo -e "\tlocalhostname - for convinience purpose in key config"
echo -e "\tusername - valid username at remote server"
echo -e "\tserverhostname - for convinience purpose in key config"
echo -e "\tssh_alias - for convinience purpose in ~/.ssh config"
echo -e "\tremotehostname - valid pingable hostname of remote server for ~/.ssh config"
echo ""
echo "Sample:"
echo -e "\t${0} myhomepc git github gh github.com"
exit 1
return 1
}
test ${#} -ge 3 || usage
test -n "${1}" || usage
test -n "${2}" || usage
test -n "${3}" || usage
host=${1}
user=${2}
target=${3}
ssh_dir="${HOME}/.ssh"
ssh_conf="${ssh_dir}/config"
ssh_keyfile_full="${ssh_dir}/${host}_${user}@${target}"
ssh_keyfile_home="~/.ssh/${host}_${user}@${target}"
cmd="ssh-keygen -t ed25519 -C ${host}:${user}@${target} -f ${ssh_keyfile_full}"
ssh-keygen -t ed25519 -C "${host}:${user}@${target}" -f "${ssh_keyfile_full}"
if [ -n "${4}" ] && [ -n "${5}" ]; then
test -f "${ssh_conf}" || touch "${ssh_conf}"
cat <<EOF >> "${ssh_conf}"
# generated at `date +"%Y.%m.%d %H:%M:%S"` using:
# \$ ${cmd}
Host ${4}
HostName ${5}
User ${user}
IdentityFile ${ssh_keyfile_home}
PubkeyAuthentication yes
PasswordAuthentication no
EOF
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment