Automating GnuPG

      No Comments on Automating GnuPG

If you want to use GnuPG in a script and don’t want to be prompted for the passphrase, put the passphrase in a file called passphrase.txt and use this to encrypt:

gpg --passphrase-fd 3 -c 3< passphrase.txt < filename > filename.gpg

Of course, you can also use echo to specify your passphrase

gpg --passphrase-fd 3 -c 3< <(echo "secret") < filename > filename.gpg

or you can pipe a tarball into gpg

tar -cf - dir/ | gpg --passphrase-fd 3 -c 3< <(echo "pass") > backup.tar.gpg

or even send a gpg encrypted tarball via e-mail

tar c dir/ | gpg --force-mdc -ac -o - --passphrase-fd 3 -c 3< <(echo "pass") | mail user@domain.tld

Note that you have to use the --batch flag if you want to run gpg from a cron script. (Otherwise gpg tries to read from /dev/tty that doesn’t exist for cron jobs)1.

More GnuPG hacks can be found here: http://www.linuxjournal.com/article/8732



[1] http://stackoverflow.com/questions/39867/how-to-run-gpg-from-a-script-run-by-cron


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.