What this is about

Setting up a computer to download packages via another computer that is running apt-cache-ng is relatively easy. However what happens when you have a computer such as a laptop, than is often connected to different networks? You may want to get updates when you are not on the network that has the cache. In order to do this, you need to remove the proxy setting. Later, when connected to the network with the cache you will want to re-enable it. This is how to automate this task.

The Script

#! /bin/bash
#

# Replace this with the hostname of the server that is running apt-cache-ng
SERVER=myserver

. /lib/lsb/init-functions

log_daemon_msg "Checking server $SERVER availiblity..."

ping -c 1 $SERVER &> /dev/null

if [ $? = "0" ]; then
    log_daemon_msg "Found apt-cache-ng server $SERVER."
    echo "Acquire::http { Proxy \"http://$SERVER:3142\"; };" > /etc/apt/apt.conf.d/01proxy
else
    log_daemon_msg "Can not reach apt-cache-ng server $SERVER."
    echo "" > /etc/apt/apt.conf.d/01proxy
fi

log_end_msg 0

exit 0

Basically this script pings the server once, and depending on if the server responded, the file /etc/apt/apt.conf.d/01proxy is either cleared of its contents or re-written.

You can put this script in /usr/local/bin.

Hooking It In

Ideally this script should run whenever a network is either connected or dis-connected. There are two directories just for this purpose, putting a link in each one will cause the script to be run whenever a network interface (e.g Wireless) comes up or goes down.

The following assumes you saved the script in /usr/local/bin and called it detectaptproxy

cd /etc/network/if-up.d
sudo ln -s /usr/local/bin/detectaptproxy
cd ../if-down.d
sudo ln -s /usr/local/bin/detectaptproxy


CategoryInternet

AutomateAptCacheNgProxySettings (last edited 2011-04-08 14:53:49 by D9784B24)