Last active
November 24, 2020 11:16
-
-
Save vtvh/402eadbee87cac94e934e64b1341fe51 to your computer and use it in GitHub Desktop.
Create ssh key, copy to clipboard, add config. Usage: create-ssh-key <keyname> <hostname> <username> <port if not 22>
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
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