osx - Command-line access to OS X's keychain - How to get e-mail address associated with account -
osx - Command-line access to OS X's keychain - How to get e-mail address associated with account -
i'm writing bash command-line tool accesses keychain emulating web browser communicate web pages.
it's quite straightforward password stored in keychain there:
password=`security find-internet-password -gs accounts.google.com -w`
but it's bit more tricky extract email address, specific command returns lot of information:
$security find-internet-password -gs accounts.google.com /users/me/library/keychains/login.keychain" class: "inet" attributes: 0x00000007 <blob>="accounts.google.com" 0x00000008 <blob>=<null> "acct"<blob>="my-email-address@gmail.com" "atyp"<blob>="form" "cdat"<timedate>=0x32303135303333303134333533315a00 "20150330143531z\000" "crtr"<uint32>="rimz" "cusi"<sint32>=<null> "desc"<blob>=<null> "icmt"<blob>=<null> "invi"<sint32>=<null> "mdat"<timedate>=0x32303135303333303134333533315a00 "20150330143531z\000" "nega"<sint32>=<null> "path"<blob>="/servicelogin" "port"<uint32>=0x00000000 "prot"<blob>=<null> "ptcl"<uint32>="htps" "scrp"<sint32>=<null> "sdmn"<blob>=<null> "srvr"<blob>="accounts.google.com" "type"<uint32>=<null> password: "my-password"
how extract business relationship e-mail address line starting "acct"<blob>=
, store it, say, variable called email
?
if you're using multiple grep
, cut
, sed
, , awk
statements, can replace them single awk
.
password=$(security find-internet-password -gs accounts.google.com -w) email=$(awk -f\" '/acct"<blob>/ {print $4}'<<<$password)
this may easier on single line, couldn't security
command print out output yours in order test it. plus, it's bit long show on stackoverflow:
email=$(security find-internet-password -gs accounts.google.com -w | awk -f\" '/acct"<blob>/ {print $4}')
the /acct"<blob>/
regular expression. particular awk
command line filter out lines match regular expression. -f\"
divides output field given. in line, fields become:
acct
<blob>
my-email-address@gmail.com
a null field the {print $4}
says print out 4th field.
by way, it's improve utilize $(....)
instead of ticks in shell scripts. $( ... )
easier see, , can enclose subcommands execute before main command:
foo=$(ls $(find . -name "*.txt"))
osx bash keychain
Comments
Post a Comment