Regular Expressions 101

Community Patterns

Parser for GPG search-key output

1

Regular Expression
PCRE2 (PHP >=7.3)

/
(?<entropy>\d+)\s*bit\s*(?<algo>\S+)\s*key\s*(?<pubkeyid>[^,]+)
/
gm

Description

Record the output of the batch gpg --search-keys commands into variables. For example, get the public key ids or those with *.amazon.com email addresses:

pubkeyids=$(gpg --batch --keyserver hkp://keyserver.ubuntu.com --search-keys amazon.com 2>&1 | grep -Po '\d+\s*bit\s*\S+\s*key\s*[^,]+' | cut -d' ' -f5)

We can automate searching for keys and adding them to the keyring using bash parsing that output. As an illustration, I created the following GitHub gist to host the code below.

Submitted by mavaddat - 2 years ago