Entries classified under unix


Unattended, Encrypted, Incremental Network Backups: Part 1
- Cool.
To links linux unix tips ... on Sat 08/13/05 at 04:18 AM
Patent absurdity
- Stallman on the EU software patent mess.
Is Linux For Losers?
- Worse is better.
To links bsd linux unix coding funny ... on Fri 06/17/05 at 03:03 PM
DarwinPorts Guide
- Alright, it looks like I'm going to have to break down and learn how to package ports since none of this crap is working on Tiger.
To links osx unix apple bsd tools ... on Wed 06/15/05 at 06:15 PM

OS X Network Location support from the command line

I move between three different network configurations with my powerbook in an average day. Two of these configurations have proxy servers and one does not. Mac OS X has really excellent network location support that lets me configure this stuff once so that switching locations is as simple as invoking QuickSilver, typing the first few letters of the network location and BAM.

Most applications automatically pick up the new proxy configuration but some do not, like Firefox (which is one of three big reasons I still use Safari). I do a lot of work from the command line with network based tools such as curl, wget, port, http_ping, links, svn, etc. None of these use the system proxy settings but most support specifying a proxy server via the http_proxy environment variable.

I've searched high and low for a mechanism that would handle setting the http_proxy variable based on my current network location but have come up with nothing.

First, you need to create a file /etc/http_proxy that specifies the proxy servers for each Network Location you have setup in your Network Preferences (If anyone can figure out how to get the proxy information directly please let me know. I can get the current network location but not information about it). The file might look something like this:

Work = http://proxy.example.com:80
Library = http://proxy.library.com:80

The keys are the names of your network locations and the values are in http://proxy-host:proxy-port form.

Next, you'll need to put the following script somewhere along your $PATH named proxy-config and give it a chmod +x too.

 #!/bin/bash 

 # source this into your current to have the proxy 
 # environment variables needed by many command line 
 # apps setup correctly.

 # get the current network location name
 netloc=$(/usr/sbin/scselect 2>&1 | egrep '^ * ' | \
          sed 's:.*((.*)):\1:')

 # find the proxy in /etc/http_proxy based on the 
 # current location
 http_proxy=$(egrep "$netloc[ \t]*=" /etc/http_proxy | \
              sed 's/.*=[ \t]*(.*)/\1/')

 if [ -n "$http_proxy" ]; then
   export http_proxy
   export HTTP_PROXY="$http_proxy"
 else
   unset http_proxy
   unset HTTP_PROXY
 fi

 # the rest of this is used for symlink commands
 bn=$(basename $0)
 if [ "$bn" != "proxy-config" ]; then
     dn=$(cd $(dirname $0); pwd)
     for p in $(echo $PATH | sed 's/:/ /g'); do
       [ "$p" != "$dn" ] && [ -x "$p/$bn" ] && exec "$p/$bn" "$@"
     done
 fi

This script has two usage scenarios. You can source this into your current shell to have the http_proxy set correctly based on your current network location:

$ . proxy-config
$ echo $http_proxy
http://proxy.example.com:80

Alternatively, you can create symlinks to the proxy-config script using the names of commands that require http_proxy and the script will automatically set the variable and exec the real command.

Got that? No? Okay, let's move on.

Pretend you have /usr/bin/curl and you put the script from above at /usr/local/bin/proxy-config. You can get curl to use the approriate proxy settings by doing something like this:

# mkdir /usr/local/proxybin
# cd /usr/local/proxybin
# ln -s ../bin/proxy-config curl
# ls -l
total 4
lrwxr-xr-x  1 root wheel 19 May 11 13:30 curl -> ../bin/proxy-config

As long as /usr/local/proxybin is on your $PATH before /usr/bin, executing curl will actuall call proxy-config. proxy-config will then setup the proxy settings and exec /usr/bin/curl.

Now just create a symlink just like the one made for curl for anything else that requires proxy settings and enjoy network location support from the command line.

To weblog osx tools bash unix ... on Wed 05/11/05 at 05:50 PM

Using Bash's History Effectively
- Need to move away from `history | grep -i`
To links bash tools unix linux reference ... on Thu 04/07/05 at 04:14 PM
Master Foo and the Ten Thousand Lines
- Word!
To links coding theory unix ... on Sun 03/27/05 at 06:45 PM
How I learned to stop worrying and love the command line, part 1.
- Introduction to being a complete bad-ass.
To links coding linux unix tools ... on Fri 02/18/05 at 03:10 AM
GNU make Manual
- All on one page :)
Sam's Teach Yourself Emacs in 24 Hours
- yeah whatever... I've been trying to learn emacs for years.
To links emacs linux tools unix ... on Tue 10/19/04 at 08:40 PM
The Hole Hawg
- Neal Stephenson on UNIX.
To links funny linux theory unix ... on Sat 10/16/04 at 03:30 PM
Mac Takes Honors as Best Unix Desktop
- "KDE and GNOME have both gotten much better, but let's get real. They're not even in the same ballpark." -- Ouch. True though...
To links linux osx unix ... on Thu 10/14/04 at 06:55 AM
A Tao of Regular Expressions
To links linux reference regex tools unix ... on Wed 10/13/04 at 04:57 PM
128 bit storage: are you high?
- What do Moore's law and boiling oceans have in common? Sun's Jeff Bonwick explains in three easy paragraphs. Really brilliant stuff.
To links cool diversions unix ... on Wed 10/13/04 at 06:37 AM
Secure programmer: Prevent race conditions
- Some really good info on various methods of dealing with synchronization between processes on *NIX based systems.
To links coding linux unix ... on Tue 10/12/04 at 09:08 AM
Ten Commands Every Linux Developer Should Know
- ctags/etags, strace, fuser, ps, time, nm, strings, od/xxd, file, objdump
To links linux tips unix ... on Tue 10/05/04 at 06:46 PM
Shell (sh,ksh,bash) scripting in 20 pages
- "A guide to writing shell scripts for C/C++/Java and unix programmers"
To links coding unix ... on Sun 10/03/04 at 05:07 AM
TRAMP User Manual
- A remote file editing package for Emacs. Uses ssh/scp.
To links emacs tools unix ... on Thu 09/30/04 at 07:29 PM
EmacsNewbie
- Super useful tips on diving into Emacs.
To links emacs tips tools unix ... on Tue 09/21/04 at 02:55 PM
Emacs Notepad
- A bunch of extremely useful notes on hacking emacs. (Ftrain.com)
To links coding emacs tools unix ... on Sat 09/18/04 at 06:22 AM
The Rise of "Worse is Better" - Richard Gabriel
- Old and still very valid. What's the best mix of Simplicity, Correctness, Consistency, and Completeness in software design? Describes MIT and "NewJersey" approaches.
To links coding theory unix ... on Fri 08/27/04 at 03:51 PM
Emacs reference card
- Single page printable version available.
To links emacs reference unix ... on Wed 08/25/04 at 01:53 PM
The Joel on Software Forum - Explain why emacs is popular? (Not a troll)
- After using Emacs for three years, I think I finally need to learn how to use it. This has some good pointers.
To links emacs linux tools unix ... on Fri 08/20/04 at 07:16 PM
The seder's grab bag
- This much sed will eat your brains!
To links tools unix ... on Tue 08/10/04 at 07:18 PM
Frequently-Asked Questions about sed, the stream editor
To links unix ... on Mon 07/26/04 at 06:31 PM

Disable horizontal wrapping in various textmode tools

Notes on a bunch of different text based utilities.
To unix linux tips weblog ... on Mon 07/19/04 at 01:53 AM

"Screen"

How not to name a software application.
To unix linux tools weblog ... on Sat 07/10/04 at 06:09 AM