Search code examples
pythongnupg

gpg commands to extract key information inside python script


I have a python script to check for gpg public key and gpg private keys expiration. So far I was managed to get the keys expiration information using gpg commands inside os.system('gpg ...') commands.

This works fine for both public and private keys but when I run that python script from nrpe server the gpg command unable to get the private key information

Public key:

os.system('gpg -n key.gpg > key_info.txt')

Private key:

os.system('gpg --batch --import key.gpg > /dev/null 2>&1')
os.system('gpg --list-secret-keys > key_info.txt')

Is there any way to get the private key information using any other commands.

Also any way to use gnupg module to get the keys information, I see we can use gnup module only to do some operations such as encrypt/decrypt etc.


Solution

  • Finally I was able to run the script and get the private key information from another nrpe server using this gpg command

    gpg -n --with-fingerprint --keyid-format=long --show-keys keys.gpg > keys_info.txt