Chinaunix首页 | 论坛 | 博客
  • 博客访问: 465537
  • 博文数量: 279
  • 博客积分: 4467
  • 博客等级: 上校
  • 技术积分: 2830
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-03 14:43
文章分类

全部博文(279)

文章存档

2013年(1)

2012年(39)

2011年(35)

2009年(29)

2008年(131)

2007年(44)

分类:

2008-04-22 09:55:06

[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" ...
, 被設置為0(自動調整,請看上面),然後是程序預定的語法格式的字符串( ). 我們沒有什麼描述,就用了一些無意義的東西(==). 變量ipslist中的數據看起來像這樣:


isp1 == isp2 == isp3 ==

用戶的選擇結果會被打印到標準錯誤. 命令"2> $tmpfile" 把這一結果寫到我們的臨時文件(tmpfile)裡. 這個菜單對話框有cancel可選, 所以我們必須檢查 $? (退出狀態) 以確定那一個鍵被按下.
阅读(683) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~