diff options
author | alex <alex@FreeBSD.org> | 2001-05-25 12:27:00 +0000 |
---|---|---|
committer | alex <alex@FreeBSD.org> | 2001-05-25 12:27:00 +0000 |
commit | 8dcea40c52b0d3421319a5844c2877a5552ee990 (patch) | |
tree | a92c21400b2ac1734e506e04e9aa5fd82c304c9c /share/examples | |
parent | 4a1406d53e6833a16dfbb8d338d868196ae23d99 (diff) | |
download | FreeBSD-src-8dcea40c52b0d3421319a5844c2877a5552ee990.zip FreeBSD-src-8dcea40c52b0d3421319a5844c2877a5552ee990.tar.gz |
This script can control the state of your ISDN line. It counts
how many scripts/users currently use the ISDN line and uses
"ifconfig down" if noone uses it any more.
Not objected by: hm
Diffstat (limited to 'share/examples')
-rw-r--r-- | share/examples/isdn/isdnctl | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/share/examples/isdn/isdnctl b/share/examples/isdn/isdnctl new file mode 100644 index 0000000..f9de44f --- /dev/null +++ b/share/examples/isdn/isdnctl @@ -0,0 +1,111 @@ +#!/bin/sh + +# isdnctl +# Control the ISDN line based on usage +# +# This script can control the state of your ISDN line. It counts +# how many scripts/users currently use the ISDN line and uses +# "ifconfig down" if noone uses it any more. +# I use this script for cronjobs that fetch mail and news and run cvsup. +# If I'm still using the line, the script won't close the connection, +# but if not, it saves a lot of phone costs. +# +# ---------------------------------------------------------------------------- +# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp): +# Alexander Langer <alex@big.endian.de> wrote this file. As long as you retain +# this notice you can do whatever you want with this stuff. If we meet some +# day, and you think this stuff is worth it, you can buy me a beer in return. +# +# Alexander Langer +# ---------------------------------------------------------------------------- +# +# $FreeBSD$ +# + + +usage () { + echo "Usage: $0 [-i interface] [-f /path/to/users.file] [up|down|show]" +} + +# Defaults +INTERFACE=isp0 +USERSFILE= + +# Getopt stuff +args=`getopt i:f: $*` +if [ $? != 0 ]; then + usage + exit 2 + fi +set -- $args +for i; do + case "$i" in + -i) + INTERFACE="$2" + shift; shift + ;; + -f) + USERSFILE="$2" + shift; shift + ;; + --) + shift + break + ;; + esac +done + +if [ -z $USERSFILE ]; then + USERSFILE=/var/run/isdn.users.$INTERFACE +fi + +if [ -z $1 ]; then + usage + exit 2 +fi + +# Does Usersfile exist? +if [ ! -f $USERSFILE ]; then + # Try to create it + if ! /sbin/ifconfig $INTERFACE | grep "^$INTERFACE.*<UP," > /dev/null; then + echo "0" > $USERSFILE || exit 1 + else + echo "1" > $USERSFILE || exit 1 + fi +elif [ ! -w $USERSFILE ]; then + echo "Error: $USERSFILE not writeable!" + exit 1 +fi + +if ! /sbin/ifconfig $INTERFACE | grep "^$INTERFACE.*<UP," > /dev/null; then + if ! cat $USERSFILE | grep '^0$' > /dev/null ; then + echo "Interface down, removing number from file"; + echo "0" > $USERSFILE + fi; +fi; + +case "$1" in + show) + echo "`cat $USERSFILE` users online" + ;; + up) + expr `cat $USERSFILE` + 1 > $USERSFILE + /sbin/ifconfig $INTERFACE up + echo "`cat $USERSFILE` users online" + ;; + down) + if cat $USERSFILE | grep '^0$' > /dev/null ; then + echo "Already down" + exit 0 + fi + expr `cat $USERSFILE` - 1 > $USERSFILE + if cat $USERSFILE | grep '^0$' > /dev/null ; then + echo "`cat $USERSFILE` users online, interface down" + /sbin/ifconfig $INTERFACE down + exit 0 + fi + echo "`cat $USERSFILE` users online" + ;; +esac + +exit 0 |