macOS keychain - get a password

The macOS keychain may be queried from the command line. This is useful for scripting - for instance, it is useful for pulling back a personal access token for Github stored in the keychain.

sudo security find-internet-password -gws github.com

where

-g ==> Display the password for the item found -s ==> Match “server” string -w ==> Display only the password on stdout


To get help:

# overall help
sudo security -h 

# subcommand help
sudo security find-internet-password -h


To list keychains:

sudo security list-keychains


To list items in a keychain:

# lists items in the default keychain, which is typically the user keychain
sudo security find-internet-password