Auto Proxy config

I had a little problem. When I am at work I have to go through a proxy server for all internet access. When I am at home I don’t have a proxy server so nothing special is needed there. So this meant that when at work to get aptitude to work I had to do a

export http_proxy="http://name.of.proxy:80"

This really is not that big of an issue but I had to do it every time I opened the terminal. My solution (with the help of some on irc) was this:

ifconfig0=`ifconfig eth0 | grep addr: | awk '{ print $2 }' | cut -d : -f2 | cut -d . -f1 | grep .`
ifconfig1=`ifconfig eth1 | grep addr: | awk ‘{ print $2 }’ | cut -d : -f2 | cut -d . -f1 | grep .`

if [[ "$ifconfig0" == "157" ]] || [[ "$ifconfig1" == "157" ]]
then
export http_proxy=”http://name.of.proxy:80″
else
export http_proxy=”"
fi

I have both a wireless and wired connection and the first part of my IP at work is a 157. The idea here is that if I get a 157 then that means that I am at work and need the proxy. If not them I must be somewhere else and it is not needed.

I added this code to the bottom of my ~/.bashrc and it seems to be working well.

In what ways can I make this better? Are there any other solutions that you can think of?

Advertisements
  1. #1 by Johan on August 29, 2007 - 10:19 pm

    Use the guessnet package. It’s made for this sort of thing. With guessnet, you usually use the MAC of your default gateway to tell where you are.

  2. #2 by Gabriel on August 29, 2007 - 11:43 pm

    OOhh thanks a lot for this tip!! Will export http_proxy work for all KDE or Gnome settings? If not, is there a command to set KDE proxy to add to this snippet?
    I feel you, I hate to be switching the kde proxy manually every time between home and school.

  3. #3 by auburn on August 30, 2007 - 12:21 am

    guessnet…cool…that should be submitted to Debian Package of the day. Do either of you want to do that?
    http://debaday.debian.net/

  4. #4 by Michael on August 30, 2007 - 6:17 am

    I think it would be excellent if Network Manager could handle this automatically. Depending on the environment NM could set proxy, default printer, default e-mail-account etc. This would be great.

  5. #5 by tictactoe on August 30, 2007 - 7:08 am

    I think it will be better if your search for “adr:”. So here is my version :

    ifconfig eth0 | grep adr: | awk ‘{ print $2 }’ | cut -d “:” -f2 | cut -d “.” -f1

  6. #6 by Stoffe on August 30, 2007 - 8:18 am

    Don’t know about better, but…

    I wrote a plugin (Locations) for the Deluge torrent client, that checks for MAC address of the Gateway and automatically remembers all relevant network settings per connected MAC address. This has the advantage of being a more generic solution that will work everywhere, as long as you have set the appropriate config.

    I was toying with the idea of having something like this system- or at least desktop-wide. Need a proxy at one location? Just set it once and it’s done. Need to throttle bandwidth when calling with your cell phone? Want a different wallpaper at work? (Maybe the last one is over the top… ;-))

    That would be quite a bit more work than just a small script, but it would be very useful in our days of laptops and networks. 🙂

    Didn’t know about guessnet, although I probably didn’t want an external dependency anyways, as the plugin is in the main distribution now.

  7. #7 by Nathaniel on August 30, 2007 - 8:43 am

    Help me code libwpad! I’ve been absent a bit lately on it with having a baby and all, but I’m going to start working on it again real soon now.

    Linux needs a standard way to set system and session proxy settings. Part of the problem with the current way that it is done is that proxies never change during the life of the process. That is simply not realistic in today’s world of suspend/resume.

    Anyway, if you are interested, you know where to find me.

  8. #8 by etank on August 30, 2007 - 9:50 am

    @tictactoe

    I looked at the output of ifconfig and there is no “adr:” so I don’t think that will work on my system. I use Ubuntu but it could be different on a different distro I guess.

    @Stoffe

    I like the ideas that you have there.

    @Nathaniel

    I would love to help you with that.

  9. #9 by etank on August 30, 2007 - 9:52 am

    @Gabriel

    In my situation I was just needing something that would work for apt-get or aptitude. You should be able to use http_proxy in all versions of Ubuntu, Debian and their off shoots.

  10. #10 by Stoffe on August 30, 2007 - 1:50 pm

    Not to hijack this post but… 🙂 I’ve thought about it some more. This is approximately how I think it could be quite easily implemented to be dropped seamlessly into a GNOME desktop:

    Start a small daemon (gnome-locations, gnome-roaming?) with the session. This daemon has a list of gconf keys that are decidedly location specific, such as proxy.

    It keeps track of what the current Gateway MAC is, by either polling every minute (like Locations do every 60 seconds), or if NetworkManager is available, listening for a D-Bus signal from that (if it has one such).

    For each MAC, the daemon keeps a copy of those keys somewhere. It also listens for changes to those keys in gconf, and updates its own list accordingly. If the MAC changes, it checks for an available list and overwrites those keys in gconf.

    It also exposes CurrentMAC, as well as a ChangedMAC signal via D-Bus for non-gconf applications to use. This way other apps don’t need to poll themselves if they wish to do the same but don’t use gconf. The Deluge plugin could use it when available, for instance, as could Firefox or Pidgin. The community could make sure they do for interested distributions.

    Add some sane defaults, like resetting proxy to empty on an unknown Gateway, while perhaps keeping last value for some others.

    In theory, that should be all that is needed for an automatically roaming GNOME, with no extra settings needed. Just set whatever is appropriate once, and it will continue to work. This works well for me in the smaller case of Deluge, but there is probably some bits that needs more testing and thinking.

    Something I missed? 🙂

  1. Auto Proxy config | Used Car Nassau

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: