[code]
#!/bin/sh
#
# pppdialout
#
#DIALOG=Xdialog
DIALOG=dialog
#
# 你的默認ISP的名字:
defaultisp=maxnet
#
error()
{
echo "$1"
exit 2
}
help()
{
cat <
pppdialout -- select an ISP and dial out.
All available ISPs must have a config file in /etc/ppp/peers
pppdialout executes the ppp-on/ppp-off scripts as described
in http://linuxfocus.org/English/March2001/article192.shtml
pppdialout, copyright gpl, http://linuxfocus.org/English/November2002
HELP
exit 0
}
# 分析命令行參數:
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
--) shift;break;; # end of options
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done
tempfile=/tmp/pppdialout.$$
trap "rm -f $tempfile" 1 2 5 15
# 檢查是否已經連接internet
if /sbin/ifconfig | grep '^ppp' > /dev/null; then
# 我們已經在線
$DIALOG --title "go offline" --yesno "Click YES to \
terminate the ppp connection" 0 0
rval="$?"
clear
if [ "$rval" = "0" ]; then
echo "running /etc/ppp/scripts/ppp-off ..."
/etc/ppp/scripts/ppp-off
fi
else
# 沒發現internet連接, 進行連接
# 從 /etc/ppp/peers 取得可用的ISP列表
for f in `ls /etc/ppp/peers`; do
if [ -f "/etc/ppp/peers/$f" ]; then
isplist="$isplist $f =="
fi
done
[ -z "$isplist" ]&&error "No isp def found in /etc/ppp/peers"
#
$DIALOG --default-item "$defaultisp" --title "pppdialout" \
--menu "Please select one of\
the following ISPs for dialout" 0 0 0 $isplist 2> $tempfile
rval="$?" # return status, isp name will be in $tempfile
clear
if [ "$rval" = "0" ]; then
isp=`cat $tempfile`
echo "running /etc/ppp/scripts/ppp-on $isp..."
/etc/ppp/scripts/ppp-on "$isp"
else
echo "Cancel..."
fi
rm -f $tempfile
fi
# pppdialout 結束
[/code]
這個腳本是如何工作的:
開始,我們定義了一些函數:error和help, 下一步我們檢查命令行參數,然後定義了一相臨時文件(/tmp/pppdialout.$$).
$$是當前進行的名字,它在每一個計算機中都是獨立的. 陷阱語句在程序被強制中斷(如用戶按下Ctrl+C)是執行並且刪除我們定義的臨時文件.
然後我檢查我們是否已經在線(命令:/sbin/ifconfig | grep '^ppp'). 如果我們已經在線則打開一個對話框,
就像上面你所看到的那樣, 詢問用戶是否要斷連接. 如果我們不在線就打開一個菜單對話框. 我們從 /etc/ppp/peers (ls
/etc/ppp/peers)中取得所有可用的ISPs. 菜單對話框的語法是:
dialog --menu "text"
阅读(729) | 评论(0) | 转发(0) |