From c8737dab6a84e089d4ae3c94d8630c8749218212 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Mon, 20 Apr 2020 20:22:54 +0200 Subject: [PATCH] add import_ssh_key.sh --- encryption/import_ssh_key.sh | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 encryption/import_ssh_key.sh diff --git a/encryption/import_ssh_key.sh b/encryption/import_ssh_key.sh new file mode 100755 index 0000000..bab8607 --- /dev/null +++ b/encryption/import_ssh_key.sh @@ -0,0 +1,88 @@ +#!/bin/sh + +function help { + echo "NAME" + echo " $(basename $0) - import an OpenSSH key from a file or from stdin" + echo "" + echo "SYNOPSYS" + echo " $(basename $0) -n NAME [-f FILE]" + echo "" + echo "OPTIONS" + echo " -n, --name=KEYNAME" + echo " The key name, e.g. \"id\", which would result in a private key file ~/.ssh/id_rsa," + echo " ~/.ssh/id_dsa, ~/.ssh/id_ecdsa or .ssh/id_ed25519 (depending on the key type)." + echo "" + echo " -f, --file=FILE" + echo " The file containing the private key that will be imported. If this option is not" + echo " specified, the private key will be read from stdin." + echo "" + echo " -h, --help" + echo " Display this help message." + echo "" + echo "AUTHOR" + echo " Written by Christian Ulrich, christian@ulrich.earth" +} + +function import { + local private_key="$1" + local name="$2" + + local public_key=$(ssh-keygen -yf /dev/stdin <<< $private_key) + local type=$( + ssh-keygen -lf /dev/stdin <<< $public_key | + rev | + cut -d" " -f1 | + rev | + tr -d '()' | + tr '[:upper:]' '[:lower:]') + local private_key_file=~/.ssh/${name}_${type} + local public_key_file=~/.ssh/${name}_${type}.pub + echo "$private_key" > $private_key_file + chmod 0600 $private_key_file + echo "$public_key" > $public_key_file + chmod 0644 $public_key_file + echo "Successfully imported key." + echo "Private key stored at ${private_key_file}" + echo "Public key stored at ${public_key_file}" +} + +function main { + while [[ $# -gt 0 ]]; do + local opt="$1" + case $opt in + -n|--name) + local name="$2" + shift + shift + ;; + -f|--file) + local file_path="$2" + shift + shift + ;; + -h|--help) + help + return 0 + ;; + *) + help + return 1 + esac + done + + if [ -z "$name" ]; then + help + return 1 + fi + + if [ -z "$file_path" ]; then + local private_key=$(