summaryrefslogtreecommitdiffstats
path: root/share/examples/isdn/contrib/isdnctl
blob: f9de44fbb83446cf317a5243d844a6c0a64fad39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
OpenPOWER on IntegriCloud