因为学校的校园网用DrCOM进行认证,当在Linux下工作时,上网很不方便,主要的原因是帐号是共用的,经常会碰到想用帐号的时候别人在用。在
Windows下,这不是什么大问题,可以让登录程序自己尝试自动登录。可是,用于Linux的drcom-client并没有提供这项功能。那怎么办
呢,我打算写一个bash脚本来凑合着对付这个问题,于是就有了本文要给出的这个东西。我的bash编程经验等于x趋近于0时sin(x)的值,所以脚本
写得很烂,看不过眼也只能将就了。
鉴于DrCOM在教育网内广泛使用,那么,这个脚本应该是适用于那些需要在Linux下通过DrCOM认证上网的童鞋的。要使用此脚本,你需要已经
安装好了drcom-client,如果你还没有这么做,那么你可以去这里下载:http:
//sourceforge.net/projects/drcom-client
并且还要已经配置好了/etc/drcom.conf,具体的配置参数见drcom-client的文档。
我写的这个脚本叫drcomcbot.sh,脚本本身接受一个可选参数:重试次数,默认的重试次数为0,也就是无限次重试。每次尝试之后脚本会“休息”一秒钟,然后再次尝试登录。如此循环往复,直到登录成功或者你失去耐心CTRL-C为止^_^
懒得传附件了,直接把脚本源代码贴上来了事:
#! /bin/sh
usage="
\n
try to login DrCOM repeatly\n
\n
you should have already installed drcom-1.3.7, if you haven't,\n
you can get latest drcom-client for linux from sourceforge.\n
\n
the only argument you can provide is how many times to retry \n
before giving up. e.g, if you wanna try 10 times, then you \n
should input as follows:\n
./drcomc.sh 10\n
\n
if retry is 0, then this script will try to login infinitely.\n
default retry is 0.\n
\n
Rockins Chen \n
Mon Apr 6 00:12:07 CST 2009\n"
#==================================================================
# REPEAT can be changed by command line argument
REPEAT=0
if [[ $# == 1 ]]
then
REPEAT=$1;
fi
#==================================================================
# following parameters should *NOT* be changed
SUCCEED="no"
LOG="./drcomc.log"
function check_login_status()
{
exp='/^Login\W\w+$/{print $2;}';
login_result=`awk "$exp" drcomc.log`;
#echo "CHECK:$login_result";
if [[ $login_result == "failed" ]]
then
SUCCEED="no";
elif [[ $login_result == "succeeded" ]]
then
SUCCEED="yes";
fi
}
function info()
{
echo -e $usage;
}
function mainloop()
{
# clear log
echo -n -e > $LOG
touch $LOG
# print prompts
info;
echo "try login to DrCOM...";
if [[ $REPEAT == 0 ]]
then
RETRY="*INFINITE*";
else
RETRY=$REPEAT;
fi
echo -e "retry $RETRY times\n\n";
# loop for ever ?
loopforever=0;
if [[ $REPEAT == 0 ]]
then
loopforever=1;
fi
for (( i=0; i < $REPEAT || $loopforever ; ))
do
drcomc login 2>&1 > $LOG;
check_login_status;
if [[ $SUCCEED == "yes" ]]
then
echo "drcom login success";
break
else
echo "faild, try again...";
fi
sleep 1s;
if [[ !$loopforever ]]
then
let i++
fi
done
unlink $LOG
}
mainloop;
|
阅读(2254) | 评论(0) | 转发(0) |