freebsd-dev/share/examples/isdn/contrib/isdnctl

112 lines
2.4 KiB
Plaintext
Raw Normal View History

#!/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