Keep It Simple Stupid

Configuring proxy in terminal in OS X

| comments

Sometimes you need to go to the internets through a proxy with authorization. And even in terminal on OS X.

First, you configure the proxy information in System Preferences / Network / Advanced / Proxies. There are Web Proxy Server, Username, and Password fields you fill in. It saves the credentials in the login keychain, so it makes sense to use them instead of typing them plaintext in terminal. To access the keychain, security program is used.

Having combined the command to set up proxy and get the credentials, here’s what I got:

export http_proxy=\
$(printf $(security find-internet-password -s ${PROXY} | grep 'acct' | cut -d'"' -f4))':'\
$(security 2>&1 >/dev/null find-internet-password -gs ${PROXY} | cut -d'"' -f2)"@${PROXY}:8080" &&\
export https_proxy="$http_proxy" ftp_proxy="$http_proxy" HTTP_PROXY="$http_proxy"\
HTTPS_PROXY="$http_proxy" FTP_PROXY="$http_proxy"; unset PROXY

In the script above I added backslashes to split the command into multiple lines. It should work this way, but if it doesn’t remove the backslashes and merge the lines.

The script assumes the name of the item with the saved password is, which is the address of your proxy and you should change it.

Note also the $(printf $(security …)) construction. Because in my case the username has the format domain\user, the security app returns domain\134user, where \134 is backslash escaping of \. printf converts it to the character itself. It may not be secure in general case!

NB! If you issue a command like echo "$https_proxy", it’ll show your username and password in plaintext. I haven’t figured out a way to avoid that yet.


Don't hesitate to leave a comment below. NB! If you don't see a comment form under the post, it's most likely that an extension (such as Ghostery, NoScript, or AdBlock) of your browser blocks the scripts from, and you can unblock that.

« Unmount flash drive in OS X Jenkins and Qt »