Search This Blog

Thursday, 14 January 2021

GPG Encryption for Python scripts

Install GPG:

pip3 install python3-gnupg

Generate key and provide name, email and passphrase:

gpg --gen-key

 Create a text file:

nano creds.txt

DEFAULT_USERNAME=cisco

DEFAULT_PASSWORD=cisco

(Ctrl+O to save, then Enter) 

Encrypt the file:

gpg --symmetric -o encrypted.env.gpg creds.txt

 To decrypt the file:

gpg --decrypt encrypted.env.gpg

To flush the password cache:

echo RELOADAGENT | gpg-connect-agebt

OK

When you decrypt the file again you will be prompted for the passphrase.

To use this file in Nornir 2.5 use these variables in the code:

nr.invetory.defaults.username = os.getenv("DEAFAULT_USERNAME")

nr.invetory.defaults.password = os.getenv("DEAFAULT_PASSWORD")

To execute the python script using the encrypted file use:

env $(gpg --decrypt encrypted.env.gpg) python3 encrypted-showipintbrief.py

No comments:

Post a Comment

Nornir Compliance Check