- 论坛徽章:
- 0
|
- #!/bin/sh
- #
- # pppdialout
- #
- #DIALOG=Xdialog
- DIALOG=dialog
- #
- # 你的默认ISP的名字:
- defaultisp=maxnet
- #
- error()
- {
- echo "$1"
- exit 2
- }
- help()
- {
- cat <<HELP
- 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 结束
复制代码
这个脚本是如何工作的:
开始,我们定义了一些函数:error和help, 下一步我们检查命令行参数,然后定义了一相临时文件(/tmp/pppdialout.$$). $$是当前进行的名字,它在每一个计算机中都是独立的. 陷井语句在程序被强制中断(如用户按下Ctrl+C)是执行并且删除我们定义的临时文件. 然后我检查我们是否已经在线(命令:/sbin/ifconfig | grep '^ppp'). 如果我们已经在线则打开一个对话框, 就像上面你所看到的那样, 询问用户是否要断连接. 如果我们不在线就打开一个菜单对话框. 我们从 /etc/ppp/peers (ls /etc/ppp/peers)中取得所有可用的ISPs. 菜单对话框的语法是:
dialog --menu "text" <height> <width> <menu height> <tag1> <description> ...
<height>, <width> 和<menu height> 被设置为0(自动调整,请看上面),然后是程序预定的语法格式的字符串(<tag1> <description> . 我们没有什么描述,就用了一些无意义的东西(==). 变量ipslist中的数据看起来像这样:
isp1 == isp2 == isp3 ==
用户的选择结果会被打印到标准错误. 命令"2> $tmpfile" 把这一结果写到我们的临时文件(tmpfile)里. 这个菜单对话框有cancel可选, 所以我们必须检查 $? (退出状态) 以确定那一个键被按下.
好啦, 理论已经足够. 结果看起来是这样:
![]()
![]()
from: http://www.linuxfocus.org/ChineseGB/November2002/article267.shtml |
|