Skip to content

Instantly share code, notes, and snippets.

@lmlsna
Last active January 21, 2022 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmlsna/6f726eb1d772802378cbc1dbc4353ec3 to your computer and use it in GitHub Desktop.
Save lmlsna/6f726eb1d772802378cbc1dbc4353ec3 to your computer and use it in GitHub Desktop.
Download a PGP key and parse key, fingerprint, and email into variables in BASH
#!/bin/bash
# First argument passed to script is pgp key url
# Download key to memory with curl or wget (fallback)
key="$(curl -sSL "$1" 2>/dev/null || wget --quiet -O - "$1" 2>/dev/null)"
# Parse just the fingerprint and email, then separate into their own variables
both="$(gpg --show-keys --keyid-format=long --list-options show-only-fpr-mbox=yes <<< "$key")"
fingerprint="$(echo "$both" | cut -d' ' -f1)"
email="$(echo "$both" | cut -d' ' -f2)"
#!/bin/bash
# One liner to get key at url "$1" and output fingerprint and email space separated
curl -sSL "$1" 2>/dev/null || wget --quiet -O - "$1" 2>/dev/null | gpg --show-keys --keyid-format=long --list-options show-only-fpr-mbox=yes
#!/bin/bash
# Download a PGP key and parse key, fingerprint, and email into variables
# Requires: gpg and (curl or wget)
# Usage: ./dl-key.sh "https://example.com/key-file.gpg.asc"
# ==== Error checking (you can omit this) =====
if [ $# -eq 0 ]; then
echo -e "\e[31mScript requires a url to a PGP keyfile as the first argument.\e[39m"; exit 1
elif [ ! $(command -v gpg 2>/dev/null) ]; then
echo -e "\e[31mScript requires gpg binary.\e[39m\nTry installing 'gpg' or 'gnupg' with your package manager."; exit 2
elif [ ! $(command -v curl 2>/dev/null) ] && [ ! $(command -v wget 2>/dev/null) ]; then
echo -e "\e[31mScript requires curl or wget.\e[39m\nTry installing 'curl' or 'wget' with your package manager."; exit 3
fi
# ===== Bash Script =====
# Download key to memory with curl or wget (fallback)
key="$(curl -sSL "$1" 2>/dev/null || wget --quiet -O - "$1" 2>/dev/null)"
# Parse just the fingerprint and email, then separate into their own variables
both="$(gpg --show-keys --keyid-format=long --list-options show-only-fpr-mbox=yes <<< "$key")"
fingerprint="$(echo "$both" | cut -d' ' -f1)"
email="$(echo "$both" | cut -d' ' -f2)"
# Output example
echo -e "key:\n $key \n"
echo -e "fingerprint: $fingerprint"
echo -e "email: $email"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment