Forked from hongkongkiwi/generate-dropbear-key
Last active
December 14, 2023 21:34
-
-
Save stokito/e43054c71572d77a665e2d8ec1096749 to your computer and use it in GitHub Desktop.
Revisions
-
stokito revised this gist
Dec 14, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -61,8 +61,8 @@ fi chmod 600 "$PRIVATE_KEY_FILE" # Output Public Key (override if exists) PUBLIC_KEY="$(dropbearkey -y -f "$PRIVATE_KEY_FILE" 2>/dev/null | grep "^ssh-" | cut -f1,2 -d ' ')" echo "${PUBLIC_KEY} ${KEY_COMMENT}" > "$PUBLIC_KEY_FILE" # Set permissions for public key chmod 644 "$PUBLIC_KEY_FILE" -
hongkongkiwi revised this gist
Jun 4, 2023 . No changes.There are no files selected for viewing
-
hongkongkiwi revised this gist
Jun 4, 2023 . 1 changed file with 39 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,30 @@ #!/bin/sh +ux # We set the sh +ux flags so that we error on undefined variables and error on bad commands help() { echo >&2 "$0 [-f] [-p] [-q] [<priv_key_file>] [<key_type>] [<key_comment>]" echo >&2 echo >&2 "-q / --quiet to silent all output (except -p if passed)" echo >&2 "-p / --pubkey to output public key after generation" echo >&2 "-f / --force to force replacing existing key" echo >&2 echo >&2 "<priv_key_file> can be any valid filename [default: '$HOME/.ssh/id_dropbear']" echo >&2 "<key_type> can be 'rsa', 'dss', 'ecdsa' or 'ed25519' [default: 'ed25519']" echo >&2 "<key_comment> can be be any valid ascii string [default: '$USER@$(hostname)'" echo >&2 } REPLACE_KEY="" OUTPUT_PUBKEY="" QUIET="" for ARG in "$@"; do case "$ARG" in '-h'|'-help'|'--help') help; exit 255;; '-f'|'-force'|'--force') REPLACE_KEY="Y"; shift;; '-p'|'-pubkey'|'--pubkey') OUTPUT_PUBKEY="Y"; shift;; '-q'|'-quiet'|'--quiet') QUIET="Y"; shift;; '-'*) echo >&2 "ERROR: unknown argument '$ARG'"; echo >&2; help; exit 255;; esac done # Ensure that dropbearkey is installed @@ -23,28 +38,38 @@ PUBLIC_KEY_FILE="${PRIVATE_KEY_FILE}.pub" KEY_TYPE="${2:-"ed25519"}" KEY_COMMENT="${3:-"$USER@$(hostname)"}" KEY_DIR="$(dirname "$PRIVATE_KEY_FILE")" # Ensure the directory exists mkdir -p "$KEY_DIR" # Set permissions for directory chmod 700 "$KEY_DIR" [ -f "$PRIVATE_KEY_FILE" -a -z "$REPLACE_KEY" ] && { echo >&2 "ERROR: $PRIVATE_KEY_FILE already exists. Pass -f/--force to override"; exit 1; } rm -f "$PRIVATE_KEY_FILE" # Generate an RSA key using dropbear if [ -z "$QUIET" ]; then dropbearkey -t "$KEY_TYPE" -f "$PRIVATE_KEY_FILE" >/dev/null || { echo >&2 "ERROR: failed generating private key"; exit 1; } echo >&2 "Key generation complete" else dropbearkey -t "$KEY_TYPE" -f "$PRIVATE_KEY_FILE" 2>/dev/null >/dev/null || { echo >&2 "ERROR: failed generating private key"; exit 1; } fi [ -f "$PRIVATE_KEY_FILE" ] || { echo >&2 "ERROR: private key file $PRIVATE_KEY_FILE does not exist"; exit 1; } # Set permissions for private key chmod 600 "$PRIVATE_KEY_FILE" # Output Public Key (override if exists) PUBLIC_KEY="$(dropbearkey -y -f "$PRIVATE_KEY_FILE" 2>/dev/null | grep "ssh-${KEY_TYPE} " | cut -f2 -d ' ')" echo "ssh-${KEY_TYPE} ${PUBLIC_KEY} ${KEY_COMMENT}" > "$PUBLIC_KEY_FILE" # Set permissions for public key chmod 644 "$PUBLIC_KEY_FILE" if [ -n "$OUTPUT_PUBKEY" ]; then # Show Public Key if [ -z "$QUIET" ]; then echo >&2 "Private Key:" fi cat "$PUBLIC_KEY_FILE" fi -
hongkongkiwi revised this gist
Jun 4, 2023 . 1 changed file with 39 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,50 @@ #!/bin/sh +ux # We set the sh +ux flags so that we error on undefined variables and error on bad commands for ARG in "$@"; do if [ "$ARG" = "-h" -o "$ARG" = "--help" ]; then echo >&2 "$0 [<priv_key_file>] [<key_type>] [<key_comment>]" echo >&2 echo >&2 "<priv_key_file> can be any valid filename [default: '$HOME/.ssh/id_dropbear']" echo >&2 "<key_type> can be 'rsa', 'dss', 'ecdsa' or 'ed25519' [default: 'ed25519']" echo >&2 "<key_comment> can be be any valid ascii string [default: '$USER@$(hostname)'" exit 255 fi done # Ensure that dropbearkey is installed command -v 'dropbearkey' >/dev/null 2>&1 || { echo >&2 "I require dropbearkey but it's not installed. Aborting."; exit 1; } # Will accept private key file name as first argument PRIVATE_KEY_FILE="${1:-"$HOME/.ssh/id_dropbear"}" PUBLIC_KEY_FILE="${PRIVATE_KEY_FILE}.pub" # Will accept key type as second argument KEY_TYPE="${2:-"ed25519"}" KEY_COMMENT="${3:-"$USER@$(hostname)"}" KEY_DIR="$(dirname "$PRIVATE_KEY_FILE")" # Ensure the directory exists mkdir -p "$KEY_DIR" # Set permissions for directory chmod 700 "$KEY_DIR" [ -f "$PRIVATE_KEY_FILE" ] && { echo >&2 "ERROR: $PRIVATE_KEY_FILE already exists"; exit 1; } # Generate an RSA key using dropbear dropbearkey -t "$KEY_TYPE" -f "$PRIVATE_KEY_FILE" || { echo >&2 "ERROR: failed generating private key"; exit 1; } [ -f "$PRIVATE_KEY_FILE" ] || { echo >&2 "ERROR: private key file $PRIVATE_KEY_FILE does not exist"; exit 1; } # Set permissions for private key chmod 600 "$PRIVATE_KEY_FILE" # Output Public Key (override if exists) PUBLIC_KEY="$(dropbearkey -y -f "$PRIVATE_KEY_FILE" | cut -f2 -d ' ')" echo "ssh-${KEY_TYPE} ${PUBLIC_KEY} ${KEY_COMMENT}" > "$PUBLIC_KEY_FILE" # Set permissions for public key chmod 644 "$PUBLIC_KEY_FILE" # Show Public Key cat "$PUBLIC_KEY_FILE" -
hongkongkiwi revised this gist
Apr 18, 2023 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,16 @@ #!/bin/sh +ux # Will accept key dir as first argument KEY_DIR="${1:-"/mnt/sda1/.ssh"}" # Will accept key type as first argument KEY_TYPE="${2:-"rsa"}" # Ensure the directory exists mkdir -p "$KEY_DIR" # Generate an RSA key using dropbear dropbearkey -t "$KEY_TYPE" -f "${KEY_DIR}/id_rsa" # Output Public Key dropbearkey -y -f "${KEY_DIR}/id_rsa" | grep "^ssh-rsa " > "${KEY_DIR}/id_rsa.pub" -
hongkongkiwi revised this gist
Aug 18, 2016 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,15 @@ #!/bin/bash KEY_DIR="/mnt/sda1/.ssh" # Make directories mkdir -p "$KEY_DIR" # Generate an RSA key using dropbear dropbearkey -t rsa -f "${KEY_DIR}/id_rsa" # Output Public Key dropbearkey -y -f "${KEY_DIR}/id_rsa" | grep "^ssh-rsa " > "${KEY_DIR}/id_rsa.pub" # Show Public Key cat "${KEY_DIR}/id_rsa.pub" -
hongkongkiwi revised this gist
Aug 18, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ # Make directories mkdir -p "/mnt/sda1/.ssh" # Generate an RSA key using dropbear dropbearkey -t rsa -f "/mnt/sda1/.ssh/id_rsa" -
hongkongkiwi revised this gist
Aug 18, 2016 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ # Generate an RSA key using dropbear dropbearkey -t rsa -f "/mnt/sda1/.ssh/id_rsa" # Output Public Key dropbearkey -y -f "/mnt/sda1/.ssh/id_rsa" | grep "^ssh-rsa " > "/mnt/sda1/.ssh/id_rsa.pub" # Show Public Key cat "/mnt/sda1/.ssh/id_rsa.pub" -
hongkongkiwi revised this gist
Aug 18, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,4 +2,4 @@ dropbearkey -t rsa -f "/mnt/sda1/.ssh/id_rsa" # Get Public Key dropbearkey -y -f "/mnt/sda1/.ssh/id_rsa" | grep "^ssh-rsa " -
hongkongkiwi created this gist
Aug 18, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ # Generate an RSA key using dropbear dropbearkey -t rsa -f "/mnt/sda1/.ssh/id_rsa" # Get Public Key dropbearkey -y -f "/mnt/sda1/.ssh/id_rsa" | grep “^ssh-rsa ”