How do I get passwords from the keyring in the terminal for usage in scripts?

You can use secret-tools to store and retrieve the password from the keyring.

To store a new password:

secret-tool store --label='Password for mydrive' drive mydrive

I let you check in the keyring how it appears. To look it up (this command can easily be inserted in your script):

secret-tool lookup drive mydrive

I think the only answer is through python, but there are two bugs that make things hard.

  1. You need to manually give your keys names (Seahorse: Descriptions) because identifying details that other applications use are not available in the python version. I have created a bug report here: https://bugs.launchpad.net/ubuntu/+source/gnome-python-desktop/+bug/1144781
  2. These descriptions are empty in Seahorse in the specific case of LUKS keys, but changing the empty description does actually change the key name so you can look for it in python. I have created a bug report here: https://bugs.launchpad.net/ubuntu/+source/seahorse/+bug/1144703

If you are working with scripts and keyrings, please mention that these bugs affect you too.

  • Bug 1144781 affects me too
  • Bug 1144703 affects me too

As for the python part, here is an example:

#!/usr/bin/env python

import gnomekeyring as gk

keyring = 'login'
keyItems = gk.list_item_ids_sync(keyring)

for keyItem in keyItems:
    key = gk.item_get_info_sync(keyring, keyItem)
    if  key.get_display_name() == 'KeyName you are looking for':
        # Your script here using key.get_secret()
        print "Password:", key.get_secret()

If you know of any other way, e.g. through simple bash commands, please let us know.


Use Python Keyring Lib

It has a convenient CLI for use in shell scripts.

Installation

pip install keyring

Setting and getting keys

$ keyring set system username
Password for 'username' in 'system':
$ keyring get system username
password