Skip to content

Instantly share code, notes, and snippets.

@BirknerAlex
Created June 12, 2019 15:58
Show Gist options
  • Save BirknerAlex/1a3a0f6464c5985f1efcd9e81cb59087 to your computer and use it in GitHub Desktop.
Save BirknerAlex/1a3a0f6464c5985f1efcd9e81cb59087 to your computer and use it in GitHub Desktop.
Yubikey GPG info script
import subprocess
import re
import sys
class GPGInfo:
@staticmethod
def get_id():
output = subprocess.check_output("/usr/bin/gpg2 --card-status --batch", shell=True)
results = re.search('^Signature key.*: ([A-Z0-9 ]+)', output.decode(), re.MULTILINE)
if results:
return results.group(1).replace(' ', '')
@staticmethod
def export_public_key(_gpg_id):
return subprocess.check_output("/usr/bin/gpg2 --armor --export " + _gpg_id, shell=True)
if __name__ == '__main__':
gpg_id = GPGInfo.get_id()
# Only output GPG key ID
if len(sys.argv) == 2 and sys.argv[1] == 'id':
print(id)
sys.exit(0)
# Output full information
print("-----------------------")
print("Yubikey GPG Information")
print("-----------------------")
print("")
print("Key ID: " + gpg_id)
print("Git ID: " + gpg_id[-16:])
print("")
print("Public Key:")
print("")
public_key = GPGInfo.export_public_key(gpg_id).decode()
for line in public_key.split('\n'):
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment