Skip to content

Instantly share code, notes, and snippets.

@vtvh
Last active Nov 24, 2020
Embed
What would you like to do?
Create ssh key, copy to clipboard, add config. Usage: create-ssh-key <keyname> <hostname> <username> <port if not 22>
create-ssh-key() {
if [ "$#" -eq 0 ]; then
cat <<EOF
Create ssh key, copy to clipboard, add config.
Usage:
create-ssh-key <keyname> <hostname> <username> <port if not 22>
EOF
return 1
fi
local keyname=$1
local hostname=$2
local username=$3
local port=$4
: "${keyname:=keyname}"
: "${hostname:=hostname}"
: "${username:=username}"
: "${port:=22}"
local private_key_path=~/.ssh/$keyname.rsa
local public_key_path=$private_key_path.pub
# No comment on public key for privacy
# Exit imidiately if choose not to overwrite exist key
ssh-keygen -t rsa -b 4096 -C '' -f $private_key_path || return 1
# copy public key to clipboard
if [[ $(command -v clipcopy) ]]; then
clipcopy $public_key_path && echo 'Public key copied to clipboard'
else
echo 'Public key content' && cat $public_key_path
fi
# add to config
cat <<EOF >> ~/.ssh/config
Host $keyname
User $username
Hostname $hostname
Port $port
IdentityFile $private_key_path
EOF
echo 'Config added, go edit the ssh config file.'
vi + ~/.ssh/config
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment