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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#! /bin/bash
##
## Usage: %PROG% [OPTIONS] [PROVIDER_LIST]
##
## A simple ISP connection manager for a Debian machine with one or more
## dial-up accounts handled by pppconfig, pon, and poff. (This script is
## especially useful as a window manager menu item or a GNOME launcher.)
##
## Options:
## -f COLOUR set text foreground to COLOUR
## -b COLOUR set text background to COLOUR
## -h show this usage message, then exit
##
## PROVIDER_LIST is a comma-separated list of provider names and screen
## names. For example, if /etc/ppp/peers/ contains the files 'provider',
## 'clear', and 'telecom', you might invoke %PROG% with:
##
## %PROG% '_IHUG=provider,Clear_NET=clear,_XTRA=telecom'
##
# 2003-09-25 Tim Musson <trmusson@ihug.co.nz>
# I, the Copyright holder of this work, hereby release it into the Public
# Domain. This applies worldwide. In case this is not legally possible, I
# grant anyone the right to use this work for any purpose, without any
# conditions, unless such conditions are required by law.
#
# 2009 Timothy Richard Musson <trmusson@gmail.com>
PROG=$(basename $0)
EX_USAGE=64
XMESSAGE=${XMESSAGE:-$(which gxmessage)} || XMESSAGE=xmessage
TIMESTAMP=$HOME/.gxdialup.tmp
PROVIDER_LIST='_default provider=provider'
TIMEOUT=40
MSG_TITLE='ISP Connection Status'
MSG_FONT='sans 14'
MSG_FG=
MSG_BG=
MSG_GEOM=400x90
MSG_POS=-center
[ "$XMESSAGE" = xmessage ] && MSG_FONT=
showUsage ()
{
sed -n '/^##/s/^## //p' $0 | sed -e "s#%PROG%#${PROG}#g"
exit
}
invocationError ()
{
echo "Try '$PROG -h'" >&2
exit $EX_USAGE
}
gxPon ()
{
# Dial a named ISP
/usr/bin/pon $1
}
gxPoff ()
{
# Disconnect
/usr/bin/poff >/dev/null
}
alreadyConnected ()
{
# Return 0 if currently connected, otherwise 1
[ -e /var/run/ppp0.pid ]
}
while getopts 'hf:b:' Option
do
case $Option in
h) showUsage ;;
f) MSG_FG=$OPTARG ;;
b) MSG_BG=$OPTARG ;;
*) invocationError ;;
esac
done
shift $(($OPTIND - 1))
[ "$#" -gt 1 ] && invocationError
[ -n "$1" ] && PROVIDER_LIST=$1
if alreadyConnected; then
if [ -f "$TIMESTAMP" ]; then
message=$(< "$TIMESTAMP")
else
message='Connected.'
fi
buttons='_Disconnect now:101'
else
message='Not connected.'
buttons=''
num=0
IFS_tmp=$IFS
IFS=','
for provider in $PROVIDER_LIST; do
[ -n "$buttons" ] && buttons="$buttons,"
showname[$num]=${provider%%=*}
realname[$num]=${provider##*=}
buttons="${buttons}Dial ${showname[$num]}:$(( 102 + num ))"
(( num++ ))
done
IFS=$IFS_tmp
fi
buttons=$buttons,GTK_STOCK_CLOSE:100
$XMESSAGE $MSG_POS \
${MSG_GEOM:+-geometry "$MSG_GEOM"} \
-title "$MSG_TITLE" \
${MSG_FONT:+-fn "$MSG_FONT"} \
${MSG_FG:+-fg "$MSG_FG"} \
${MSG_BG:+-bg "$MSG_BG"} \
-buttons "$buttons" \
-default GTK_STOCK_CLOSE \
"$message"
response=$?
if [ "$response" -eq 101 ]; then
gxPoff
elif [ "$response" -ge 102 ]; then
num=$(( response - 102 ))
gxPon ${realname[$num]} || exit 1
echo "Connected to ${showname[num]/_/} since $(date +%T)" > "$TIMESTAMP"
if [ "$TIMEOUT" -gt 0 ]; then
$XMESSAGE $MSG_POS \
${MSG_GEOM:+-geometry "$MSG_GEOM"} \
-title "$MSG_TITLE" \
${MSG_FONT:+-fn "$MSG_FONT"} \
${MSG_FG:+-fg "$MSG_FG"} \
${MSG_BG:+-bg "$MSG_BG"} \
-timeout $TIMEOUT \
-buttons '' \
"Dialing ${showname[$num]/_/}, please wait..." &
fi
fi
exit 0
|