Skip to content

Instantly share code, notes, and snippets.

@stokito
Forked from hongkongkiwi/generate-dropbear-key
Last active December 14, 2023 21:34
Show Gist options
  • Save stokito/e43054c71572d77a665e2d8ec1096749 to your computer and use it in GitHub Desktop.
Save stokito/e43054c71572d77a665e2d8ec1096749 to your computer and use it in GitHub Desktop.

Revisions

  1. stokito revised this gist Dec 14, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions generate-dropbear-key
    Original 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-${KEY_TYPE} " | cut -f2 -d ' ')"
    echo "ssh-${KEY_TYPE} ${PUBLIC_KEY} ${KEY_COMMENT}" > "$PUBLIC_KEY_FILE"
    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"

  2. @hongkongkiwi hongkongkiwi revised this gist Jun 4, 2023. No changes.
  3. @hongkongkiwi hongkongkiwi revised this gist Jun 4, 2023. 1 changed file with 39 additions and 14 deletions.
    53 changes: 39 additions & 14 deletions generate-dropbear-key
    Original 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
    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
    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" ] && { echo >&2 "ERROR: $PRIVATE_KEY_FILE already exists"; exit 1; }
    [ -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
    dropbearkey -t "$KEY_TYPE" -f "$PRIVATE_KEY_FILE" || { echo >&2 "ERROR: failed generating private key"; exit 1; }
    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" | cut -f2 -d ' ')"
    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"

    # Show Public Key
    cat "$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
  4. @hongkongkiwi hongkongkiwi revised this gist Jun 4, 2023. 1 changed file with 39 additions and 8 deletions.
    47 changes: 39 additions & 8 deletions generate-dropbear-key
    Original 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

    # Will accept key dir as first argument
    KEY_DIR="${1:-"/mnt/sda1/.ssh"}"
    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

    # Will accept key type as first argument
    KEY_TYPE="${2:-"rsa"}"
    # 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 "${KEY_DIR}/id_rsa"
    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
    dropbearkey -y -f "${KEY_DIR}/id_rsa" | grep "^ssh-rsa " > "${KEY_DIR}/id_rsa.pub"
    # 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 "${KEY_DIR}/id_rsa.pub"
    cat "$PUBLIC_KEY_FILE"
  5. @hongkongkiwi hongkongkiwi revised this gist Apr 18, 2023. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions generate-dropbear-key
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,16 @@
    #!/bin/bash
    #!/bin/sh +ux

    KEY_DIR="/mnt/sda1/.ssh"
    # Will accept key dir as first argument
    KEY_DIR="${1:-"/mnt/sda1/.ssh"}"

    # Make directories
    # 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 rsa -f "${KEY_DIR}/id_rsa"
    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"
  6. @hongkongkiwi hongkongkiwi revised this gist Aug 18, 2016. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions generate-dropbear-key
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,15 @@
    #!/bin/bash

    KEY_DIR="/mnt/sda1/.ssh"

    # Make directories
    mkdir -p "/mnt/sda1/.ssh"
    mkdir -p "$KEY_DIR"

    # Generate an RSA key using dropbear
    dropbearkey -t rsa -f "/mnt/sda1/.ssh/id_rsa"
    dropbearkey -t rsa -f "${KEY_DIR}/id_rsa"

    # Output Public Key
    dropbearkey -y -f "/mnt/sda1/.ssh/id_rsa" | grep "^ssh-rsa " > "/mnt/sda1/.ssh/id_rsa.pub"
    dropbearkey -y -f "${KEY_DIR}/id_rsa" | grep "^ssh-rsa " > "${KEY_DIR}/id_rsa.pub"

    # Show Public Key
    cat "/mnt/sda1/.ssh/id_rsa.pub"
    cat "${KEY_DIR}/id_rsa.pub"
  7. @hongkongkiwi hongkongkiwi revised this gist Aug 18, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions generate-dropbear-key
    Original 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"

  8. @hongkongkiwi hongkongkiwi revised this gist Aug 18, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions generate-dropbear-key
    Original 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"

    # Get Public Key
    dropbearkey -y -f "/mnt/sda1/.ssh/id_rsa" | grep "^ssh-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"
  9. @hongkongkiwi hongkongkiwi revised this gist Aug 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generate-dropbear-key
    Original 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
    dropbearkey -y -f "/mnt/sda1/.ssh/id_rsa" | grep "^ssh-rsa "
  10. @hongkongkiwi hongkongkiwi created this gist Aug 18, 2016.
    5 changes: 5 additions & 0 deletions generate-dropbear-key
    Original 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 ”