Weblog entry #1 for Notwerk
#1
Apt-Cacher for Mobile Users
Posted by Notwerk on Sat 19 Apr 2008 at 13:06
I use an apt-cacher server on a network to limit bandwidth usage. Unfortunately, laptop users who connect to both the mentioned network as well as others find it a hassle to keep switching back and forth between two sets of settings for apt. So here's a nifty little script to automate this task on the client side.<cut>
#!/bin/bash
#
# INFO:
# This script will test to see if the computer is behind an apt-cacher server,
# then creates/modifies a directive in /etc/apt/apt.conf.d/01proxy accordingly.
#
# INSTALLATION:
# Save this script in /etc/network/if-up.d/ using an appropriate name and make
# it executable (chown root:root and chmod 755).
#
# Version 0.9.6
# Written by: Munther W. Dajani
# Email: mdajani@gmail.com
#
###################################################
### Define variables here
CACHE_NAME='repo' # apt-cacher hostname or ip
CACHE_PORT='3142' # apt-cacher listen port
###################################################
### Preset Variables. Do NOT modify
FILE='/etc/apt/apt.conf.d/01proxy'
V1='Acquire::http::Proxy'
V2='http://';
###################################################
### Define Functions
function make_file () {
rm -f $1
touch $1
chmod 644 $1
chown root:root $1
echo "// Maintained by $0" > $1
echo '// Do not edit' >> $1
echo '//' >> $1
echo "$V1 \"$V2\";" >> $1
}
###
function get_mode () {
local RESULT=`cat $3 |grep -v ^// |awk '{print $2}'`
case $RESULT in
\"$V2\"\;) MODE='direct';;
\"$V2$1:$2\"\;) MODE='cached';;
*) make_file $3
get_mode $1 $2 $3
;;
esac
}
###
function find_proxy () {
local RESULT=`echo "open $1 $2" |telnet 2>/dev/null |grep -c Connected`
case $RESULT in
0) STATUS='closed';;
1) STATUS='open';;
*) exit 1;;
esac
}
###
function switch () {
case $MODE in
'direct') sed --in-place -e "s/http:\/\//&$1:$2/" $3;;
'cached') sed --in-place -e "s/$1:$2//" $3;;
*) exit 1;;
esac
}
###
function main () {
find_proxy $1 $2
get_mode $1 $2 $3
if ([ $STATUS = 'open' ] && [ $MODE = 'direct' ]) \
|| ([ $STATUS = 'closed' ] && [ $MODE = 'cached' ]); then
switch $1 $2 $3
fi
}
###################################################
### Start
sleep 2
main $CACHE_NAME $CACHE_PORT $FILE
exit 0
#EOF
Please feel free to modify/redistribute as per GPLv3.
Any comments or suggestions on improving this script are very welcome.
#!/bin/bash
#
# INFO:
# This script will test to see if the computer is behind an apt-cacher server,
# then creates/modifies a directive in /etc/apt/apt.conf.d/01proxy accordingly.
#
# INSTALLATION:
# Save this script in /etc/network/if-up.d/ using an appropriate name and make
# it executable (chown root:root and chmod 755).
#
# Version 0.9.6
# Written by: Munther W. Dajani
# Email: mdajani@gmail.com
#
###################################################
### Define variables here
CACHE_NAME='repo' # apt-cacher hostname or ip
CACHE_PORT='3142' # apt-cacher listen port
###################################################
### Preset Variables. Do NOT modify
FILE='/etc/apt/apt.conf.d/01proxy'
V1='Acquire::http::Proxy'
V2='http://';
###################################################
### Define Functions
function make_file () {
rm -f $1
touch $1
chmod 644 $1
chown root:root $1
echo "// Maintained by $0" > $1
echo '// Do not edit' >> $1
echo '//' >> $1
echo "$V1 \"$V2\";" >> $1
}
###
function get_mode () {
local RESULT=`cat $3 |grep -v ^// |awk '{print $2}'`
case $RESULT in
\"$V2\"\;) MODE='direct';;
\"$V2$1:$2\"\;) MODE='cached';;
*) make_file $3
get_mode $1 $2 $3
;;
esac
}
###
function find_proxy () {
local RESULT=`echo "open $1 $2" |telnet 2>/dev/null |grep -c Connected`
case $RESULT in
0) STATUS='closed';;
1) STATUS='open';;
*) exit 1;;
esac
}
###
function switch () {
case $MODE in
'direct') sed --in-place -e "s/http:\/\//&$1:$2/" $3;;
'cached') sed --in-place -e "s/$1:$2//" $3;;
*) exit 1;;
esac
}
###
function main () {
find_proxy $1 $2
get_mode $1 $2 $3
if ([ $STATUS = 'open' ] && [ $MODE = 'direct' ]) \
|| ([ $STATUS = 'closed' ] && [ $MODE = 'cached' ]); then
switch $1 $2 $3
fi
}
###################################################
### Start
sleep 2
main $CACHE_NAME $CACHE_PORT $FILE
exit 0
#EOF
Please feel free to modify/redistribute as per GPLv3.
Any comments or suggestions on improving this script are very welcome.