Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have several network locations set up on my laptop: Work, Home, etc. The work one(s) all have a proxy server set up, while the others don't. This works fine for OSX applications -- Safari, Mail, even Firefox and Thunderbird with the System Proxy plugin.

For terminal applications such as git, svn, gem, and curl I have the following in my .bash_profile:

export HTTP_PROXY='http://proxy.mycompany.com:80'
export http_proxy=$HTTP_PROXY

My question is this: is there some way to make the exported variable look up the value from my system's current location? I'd prefer it to be done dynamically (so if I change locations during a terminal session it will change), but I'd be happy with just one that set it when .bash_profile ran (meaning I'd have to start a new terminal session when I changed locations).

Thanks!

share|improve this question
add comment

3 Answers 3

What version of Mac OS X? I'm not positive the tool is included with Mac OS X 10.4 or earlier.

networksetup should be what you're looking for, namely sudo networksetup -getwebproxy NAME_OF_NETWORK_DEVICE (eg. sudo networksetup -getwebproxy Airport)

The output comes out as so:

Enabled: Yes
Server: SERVER_ADDRESS
Port: 123
Authenticated Proxy Enabled: 0 for false, 1 for true

So you will need to convert the output to something usable.

A really crude example using awk a couple times (my awk skills are rather basic) would be:

sudo networksetup -getwebproxy Airport | awk {'print $2'} | awk {'getline l2; getline l3; print "http://"l2":"l3'} | head -n 1

Results in an output http://SERVER_ADDRESS:123

share|improve this answer
2  
A list of all services: networksetup -listallnetworkservices –  Arjan Sep 29 '09 at 15:52
1  
I love the idea, but it doesn't work for me. If I just do sudo networksetup -getwebproxy Ethernet, I get the printout and then a segfault. I imagine the segfault causes the pipe to break, so awk never gets anything to parse. –  James A. Rosen Sep 29 '09 at 16:06
    
Is there any information output? And does it still segfault without the awk commands? –  Chealion Sep 29 '09 at 16:23
    
I think I actually saw that segmentation fault on my own Macs once. I don't have the issue on 10.5.8 or 10.6.1 now (both Intel). Maybe you're on some other version? –  Arjan Sep 29 '09 at 17:38
    
I'm on 10.5.8, and it happens without the awk. Adding the awk makes the segfault message disappear . . . along with all other output. –  James A. Rosen Sep 29 '09 at 19:48
show 4 more comments
up vote 1 down vote accepted

The following seems to work, but I'm not sure how robust it is:

system_profiler SPNetworkDataType|grep "HTTP Proxy Server"|awk {'sub(/^.*:[ \t]*/, "", $0); print $0;'}

It doesn't work if your Location uses a proxy.pac instead of a hard-coded proxy location.

share|improve this answer
    
Unfortunately this doesn't seem to include the port number. –  tlrobinson Apr 28 '11 at 0:25
add comment

In the answer above, there is a comment from tlrobinson about how it doesn't include the port number.

You can do that by switching out HTTP Proxy Server with HTTP Proxy Port

I have also seen this done like this:

system_profiler SPNetworkDataType | grep "HTTP Proxy Server" | awk '{print $4}' | head -1
system_profiler SPNetworkDataType | grep "HTTP Proxy Port" | awk '{print $4}' | head -1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.