教育网直通车连接工具

出自Linux Wiki

在2007年3月15日 (四) 04:33由Chenxing (讨论 | 贡献)所做的修订版本

最新版本于2007.3.15日公布,版本号1.0.1。请使用最新版本。 本文给出一个连接教育网直通车的脚本。

为保证本文中的代码不在转载中走样,如果你不能保证准确地复制本文和下面代码中的每一个字符,请不要转载本文。谢谢合作!

目录

简介

目前下面的脚本在 GNU/Debian Testing ( Etch )中测试通过,我认为它可以在其它发行版中使用。

该脚本的功能

  1. 连接教育网直通车的指定服务器并设置路由
  2. 断开服务器并恢复路由

使用该脚本的条件

如果要连接服务器一,请按照教育网直通车服务器一的连接配置方法,如果还要连接服务器二或三,请按照教育网直通车服务器二、三的连接配置方法进行配置。配置完成后,方可使用此脚本进行连接。

安装说明

1.复制下面的代码到一个空文件中,将其保存,这里假设文件名为eduvipTools,下载direct.conf,存到适当目录下。

2.为其添加执行权限

#chmod +x eduvipTools

3.编辑这个文件,比如:

#vi eduvipTools

4.文件有一行file=/etc/init.d/directip.conf,修改此行,让file=你的direct.conf的路径,推荐使用绝对路径。

5.修改各连接的名称。在下面的nameList[0],nameList[1],nameList[2]后面是三个连接的名称,对于服务器一来说,它的值是你在/etc/ppp/peers中创建的那个文件的文件名。对于服务器二和三,它们是在/etc/l2tpd/l2tpd.conf中,[lns后的那个串。

6.保存所做的修改,退出。安装完成!

使用说明

以root身份进入存放eduvipTools的文件夹。

连接服务器一的指令为:

#./eduvipTools -s 1

类似地,连接服务器二的指令为:

#./eduvipTools -s 2

断开连接的方法:

#./eduvipTools -a d

如果执行时发生错误导致不能上网,请关掉、再打开你的网卡(假设是eth0):

ifconfig eth0 down
ifconfig eth0 up

如果是动态IP的,再执行

dhclient eth0

此时会回到连接服务器前的状态。

代码

<bash>

  1. !/bin/bash
  1. A file that stores a list of IP that should be directly connected.

file=/etc/init.d/directip.conf

  1. Connection Name List

nameList[0]="eduexp" # EduVip Server 1 (pptp) nameList[1]="eduvip2" # EduVip Server 2 (l2tp) nameList[2]="eduvip3" # EduVip Server 3 (l2tp)

  1. Gateway List, From server 0~2

gwlist[0]="58.207.255.114" gwlist[1]="58.207.254.74" gwlist[2]="58.207.2.0"

sid=0 act="0" while getopts s:a: opt ; do case $opt in s) sid=$OPTARG ;; a) act=$OPTARG ;; :) echo "$OPTARG requires additional info." ;; esac done

  1. Get the server id.

serverToConnect=0; if [ $sid = 2 ] || [ $sid = 3 ] ; then let serverToConnect=sid-1 fi

  1. Gateway and Connection name

gwip=${gwlist[$serverToConnect]} conName=${nameList[$serverToConnect]}

function disConnect { disconName=${nameList[$1]} disgwip=${gwlist[$1]} route -n | grep $disgwip > /dev/null if [ $? -gt 0 ]; then return fi if [ $1 -eq 0 ] ; then poff $disconName >/dev/null else echo "d $disconName" > /var/run/l2tp-control fi i=0 while read ip nouse mask; do if [ $i -eq 0 ]; then i=1 route -n | grep ^$ip | while read dest default_gw other; do route add -net default gw $default_gw done fi route del -net $ip netmask $mask 2>/dev/null done < $file rm /tmp/eduvipVPNstatus -f 2>/dev/null }

function DoConnect { disConnect 0 disConnect 1 disConnect 2 echo "Making Connection" # Establish the VPN connection if [ $serverToConnect -eq 0 ] ; then poff $conName >/dev/null pon $conName else /etc/init.d/l2tpd start 2>/dev/null >/dev/null echo "c $conName" > /var/run/l2tp-control fi

# 等待连接完成 declare -i i=0 route -n | grep ppp0$ > /dev/null until [ $? -eq 0 ] || [ $i -gt 100 ]; do let i=i+1 sleep 0.1 route -n | grep ppp0$ > /dev/null done

# 测试100次后仍未测试到路由,则认为连接失败 if [ $i -gt 100 ]; then echo "Connection Failed." exit 1 fi

echo "Connection Established." }

function setRoute { route -n | while read dest default_gw other; do if [ $dest = "0.0.0.0" ]; then # 清除原默认网关 route del -net default

if [ ! $default_gw = "0.0.0.0" ]; then # 逐个加入需要直接连接的ip的路由 while read ip nouse mask; do route add -net $ip netmask $mask gw $default_gw done < $file fi

# 加入直通车的网关 i=0 route add -net default gw $gwip until [ $? -eq 0 ] || [ $i -gt 3 ]; do let i=i+1 sleep 1 echo "Try again..." route add -net default gw $gwip done if [ $i -gt 3 ]; then echo "Connect Failed." exit 1 fi exit 1 fi done

# 若不存在默认网关或者路由已设置 if [ $? -eq 0 ]; then i=0 route add -net default gw $gwip until [ $? -eq 0 ] || [ $i -gt 3 ]; do let i=i+1 sleep 1 echo "Try again..." route add -net default gw $gwip done if [ $i -gt 3 ]; then echo "Connect Failed." exit 1 fi fi }

if [ $act = "d" ] ; then echo "Disconnecting" disConnect 0 disConnect 1 disConnect 2 exit 0 fi

DoConnect setRoute

  1. !/bin/bash
  1. A file that stores a list of IP that should be directly connected.

file=/etc/init.d/directip.conf

  1. Connection Name List

nameList[0]="eduexp" # EduVip Server 1 (pptp) nameList[1]="eduvip2" # EduVip Server 2 (l2tp) nameList[2]="eduvip3" # EduVip Server 3 (l2tp)

  1. Gateway List, From server 0~2

gwlist[0]="58.207.255.114" gwlist[1]="58.207.254.74" gwlist[2]="58.207.2.0"

sid=0 act="0" while getopts s:a: opt ; do case $opt in s) sid=$OPTARG ;; a) act=$OPTARG ;; :) echo "$OPTARG requires additional info." ;; esac done

  1. Get the server id.

serverToConnect=0; if [ $sid = 2 ] || [ $sid = 3 ] ; then let serverToConnect=sid-1 fi

  1. Gateway and Connection name

gwip=${gwlist[$serverToConnect]} conName=${nameList[$serverToConnect]}

function disConnect { disconName=${nameList[$1]} disgwip=${gwlist[$1]} route -n | grep $disgwip > /dev/null if [ $? -gt 0 ]; then return fi if [ $1 -eq 0 ] ; then poff $disconName >/dev/null else echo "d $disconName" > /var/run/l2tp-control fi i=0 while read ip nouse mask; do if [ $i -eq 0 ]; then i=1 route -n | grep ^$ip | while read dest default_gw other; do route add -net default gw $default_gw done fi route del -net $ip netmask $mask 2>/dev/null done < $file rm /tmp/eduvipVPNstatus -f 2>/dev/null }

function DoConnect { disConnect 0 disConnect 1 disConnect 2 echo "Making Connection" # Establish the VPN connection if [ $serverToConnect -eq 0 ] ; then poff $conName >/dev/null pon $conName else /etc/init.d/l2tpd start 2>/dev/null >/dev/null echo "c $conName" > /var/run/l2tp-control fi

# 等待连接完成 declare -i i=0 route -n | grep ppp0$ > /dev/null until [ $? -eq 0 ] || [ $i -gt 1000 ]; do let i=i+1 route -n | grep ppp0$ > /dev/null done

# 测试1000次后仍未测试到路由,则认为连接失败 if [ $i -gt 2000 ]; then echo "Connection Failed." exit 1 fi

echo "Connection Established." }

function setRoute { route -n | while read dest default_gw other; do if [ $dest = "0.0.0.0" ]; then # 清除原默认网关 route del -net default

if [ ! $default_gw = "0.0.0.0" ]; then # 逐个加入需要直接连接的ip的路由 while read ip nouse mask; do route add -net $ip netmask $mask gw $default_gw done < $file fi

# 加入直通车的网关 i=0 route add -net default gw $gwip until [ $? -eq 0 ] || [ $i -gt 3 ]; do let i=i+1 sleep 1 echo "Try again..." route add -net default gw $gwip done exit 1 fi done

# 若不存在默认网关或者路由已设置 if [ $? -eq 0 ]; then i=0 route add -net default gw $gwip until [ $? -eq 0 ] || [ $i -gt 3 ]; do let i = i + 1 sleep 1 echo "Try again..." route add -net default gw $gwip done fi }

if [ $act = "d" ] ; then echo "Disconnecting" disConnect 0 disConnect 1 disConnect 2 exit 0 fi

DoConnect setRoute echo "Connected." echo $serverToConnect > /tmp/eduvipVPNstatus </bash>

Chenxing 17:56 2007年3月9日 (CST)

个人工具
简体繁体转换