add import_ssh_key.sh
This commit is contained in:
parent
9ad3bd1714
commit
c8737dab6a
|
@ -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=$(</dev/stdin)
|
||||
else
|
||||
local private_key=$(<$file_path)
|
||||
fi
|
||||
|
||||
import "$private_key" "$name"
|
||||
}
|
||||
|
||||
set -e
|
||||
main "$@"
|
Loading…
Reference in New Issue