Skip to content

Instantly share code, notes, and snippets.

@knksmith57
Created May 12, 2017 17:42
Show Gist options
  • Save knksmith57/5576678a22a570a955beb433cdcfa868 to your computer and use it in GitHub Desktop.
Save knksmith57/5576678a22a570a955beb433cdcfa868 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eo pipefail
##
## extract .netrc creds inspired by https://bugs.launchpad.net/python-jenkins/+bug/941400
##
## usage:
## netrc_credentials example.com # // 'myuser mypassword'
## netrc_credentials example.com username # // 'myuser'
## netrc_credentials example.com password # // 'mypassword'
##
: ${1:?'domain must be provided via positional argument #1'}
username_password="$(echo -e "import netrc\nusername, _, password = netrc.netrc().authenticators('$1')\nprint '{} {}'.format(username, password)" | python 2>/dev/null)"
if [[ $2 == u* ]]; then
awk '{print $1}' <<< "${username_password}"
elif [[ $2 == p* ]]; then
awk '{print $2}' <<< "${username_password}"
else
echo "${username_password}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment