Fedora下连接浙大vpn
Monday, June 21, 2010
首先要装好xl2tpd
yum install xl2tpd
然后修改/etc/xl2tpd/xl2tpd.conf
vi /etc/xl2tpd/xl2tpd.conf
[global] ; Global parameters:
port = 1701 ; * Bind to port 1701
[lac zju] ; Example VPN LAC definition
lns = lns.zju.edu.cn ; * Who is our LNS?
refuse pap = yes ; * Refuse PAP authentication
require authentication = no ; * Require peer to authenticate
name = 20910027@a ; * Report this as our hostname
ppp debug = yes ; * Turn on PPP debugging
pppoptfile = /etc/xl2tpd/zju.options ; * ppp options file for this lac
创建/etc/xl2tpd/zju.options
vi /etc/xl2tpd/zju.options
asyncmap 0
noauth
crtscts
lock
hide-password
modem
netmask 255.255.255.0
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
ipcp-accept-local
ipcp-accept-remote
noipx
修改/etc/ppp/chap-secrets
vi /etc/ppp/chap-secrets
username * "password" *
然后创建连接脚本
vi vpn.sh
/*********************************************/
#!/bin/bash
########################################
# Shell script to (re)connect to VPN
# pluskid
# wg.wang
########################################
####################
# Variables
####################
##Set the environment variables of PATH
PATH=/bin:/usr/bin:/sbin:/usr/sbin
## How many seconds to wait for the ppp0 to come up each try
TIMEOUT=20
## How many times to try to bring up ppp0 until fail
N_RETRY=3
## Interface default to use
INTERFACE=eth0
## How many seconds to wait for l2tpd to restart
L2TPD_TIMEOUT=10
## For xl2tpd: /etc/init.d/xl2tpd
## For l2tpd: /etc/init.d/l2tpd
L2TPD_SCRIPT=/etc/init.d/xl2tpd #由于我使用的是xl2tpd,
#如果是l2tpd,就改成l2tpd.下面相同。
## For xl2tpd: /var/run/xl2tpd/l2tp-control
## For l2tpd: /var/run/l2tp-control
L2TPD_PIPE=/var/run/xl2tpd/l2tp-control
####################
# Commands
####################
function usage
{
echo "$1 login to the school VPN."
echo "$1 [-h] [-r]"
echo " Default to connect to VPN."
echo " -r reconnect."
echo " -h help, i.e. show this information."
}
function connect
{
if ppp0_alive ; then
echo "Already connected."
else
bring_up_ppp0 && setup_route
fi
}
function reconnect
{
restart_l2tpd && bring_up_ppp0 && setup_route
}
####################
# Internal utility functions
####################
function super_user
{
if [ "$UID" = "0" ]; then
return 0 # Yes, super user
else
return 1
fi
}
function ppp0_alive
{
if ifconfig | grep -s 'ppp0' > /dev/null ; then
return 0 # Yes, connected
else
return 1
fi
}
function bring_up_ppp0
{
for i in $(seq $N_RETRY)
do
echo -n "Trying to bring up ppp0."
$L2TPD_SCRIPT start &
echo "c zju" > $L2TPD_PIPE
for j in $(seq $TIMEOUT)
do
if ppp0_alive; then
echo " Done!"
return 0 # Yes, brought up!
fi
echo -n "."
sleep 1
done
echo
done
echo "ERROR: Failed to bring up ppp0!"
return 1
}
function setup_route
{
echo "Setting up route..."
STATIC=$(ip route | grep '^default' | cut -d" " -f3)
# If already set the route before, we cannot find
# the original default route any more. But fortunately,
# the route previously setted often won't change and
# we can just safely ignore this task
if [ ! -z $STATIC ]; then
ip route add 10.0.0.0/8 via $STATIC dev $INTERFACE
ip route add 222.205.0.0/16 via $STATIC dev $INTERFACE
ip route add 210.32.0.0/16 via $STATIC dev $INTERFACE
ip route add 239.43.1.1 via $STATIC dev $INTERFACE
ip route del default
fi
PPP=$(ifconfig | sed -n '/P-t-P/s/^.*P-t-P:\([0-9.]*\).*$/\1/p')
ip route add default via $PPP dev ppp0
echo "Done!"
}
function restart_l2tpd
{
echo "Restarting xl2tpd..."
$L2TPD_SCRIPT restart
for i in $(seq $L2TPD_TIMEOUT)
do
if [ -e $L2TPD_PIPE ]; then
echo "Done!"
return 0 # Successfully restarted!
fi
sleep 1
done
echo "ERROR: Failed to restart xl2tpd!"
return 1 # Failed to restart xl2tpd
}
####################
# Main
####################
if ! super_user ; then
echo "ERROR: You must be super user to run this utility!"
exit 1
fi
if [ $# -lt 1 ]; then
connect
elif [ "$1" = "-h" ]; then
usage
else
reconnect
fi
#!/bin/bash
########################################
# Shell script to (re)connect to VPN
# pluskid
# wg.wang
########################################
####################
# Variables
####################
##Set the environment variables of PATH
PATH=/bin:/usr/bin:/sbin:/usr/sbin
## How many seconds to wait for the ppp0 to come up each try
TIMEOUT=20
## How many times to try to bring up ppp0 until fail
N_RETRY=3
## Interface default to use
INTERFACE=eth0
## How many seconds to wait for l2tpd to restart
L2TPD_TIMEOUT=10
## For xl2tpd: /etc/init.d/xl2tpd
## For l2tpd: /etc/init.d/l2tpd
L2TPD_SCRIPT=/etc/init.d/xl2tpd #由于我使用的是xl2tpd,
#如果是l2tpd,就改成l2tpd.下面相同。
## For xl2tpd: /var/run/xl2tpd/l2tp-control
## For l2tpd: /var/run/l2tp-control
L2TPD_PIPE=/var/run/xl2tpd/l2tp-control
####################
# Commands
####################
function usage
{
echo "$1 login to the school VPN."
echo "$1 [-h] [-r]"
echo " Default to connect to VPN."
echo " -r reconnect."
echo " -h help, i.e. show this information."
}
function connect
{
if ppp0_alive ; then
echo "Already connected."
else
bring_up_ppp0 && setup_route
fi
}
function reconnect
{
restart_l2tpd && bring_up_ppp0 && setup_route
}
####################
# Internal utility functions
####################
function super_user
{
if [ "$UID" = "0" ]; then
return 0 # Yes, super user
else
return 1
fi
}
function ppp0_alive
{
if ifconfig | grep -s 'ppp0' > /dev/null ; then
return 0 # Yes, connected
else
return 1
fi
}
function bring_up_ppp0
{
for i in $(seq $N_RETRY)
do
echo -n "Trying to bring up ppp0."
$L2TPD_SCRIPT start &
echo "c zju" > $L2TPD_PIPE
for j in $(seq $TIMEOUT)
do
if ppp0_alive; then
echo " Done!"
return 0 # Yes, brought up!
fi
echo -n "."
sleep 1
done
echo
done
echo "ERROR: Failed to bring up ppp0!"
return 1
}
function setup_route
{
echo "Setting up route..."
STATIC=$(ip route | grep '^default' | cut -d" " -f3)
# If already set the route before, we cannot find
# the original default route any more. But fortunately,
# the route previously setted often won't change and
# we can just safely ignore this task
if [ ! -z $STATIC ]; then
ip route add 10.0.0.0/8 via $STATIC dev $INTERFACE
ip route add 222.205.0.0/16 via $STATIC dev $INTERFACE
ip route add 210.32.0.0/16 via $STATIC dev $INTERFACE
ip route add 239.43.1.1 via $STATIC dev $INTERFACE
ip route del default
fi
PPP=$(ifconfig | sed -n '/P-t-P/s/^.*P-t-P:\([0-9.]*\).*$/\1/p')
ip route add default via $PPP dev ppp0
echo "Done!"
}
function restart_l2tpd
{
echo "Restarting xl2tpd..."
$L2TPD_SCRIPT restart
for i in $(seq $L2TPD_TIMEOUT)
do
if [ -e $L2TPD_PIPE ]; then
echo "Done!"
return 0 # Successfully restarted!
fi
sleep 1
done
echo "ERROR: Failed to restart xl2tpd!"
return 1 # Failed to restart xl2tpd
}
####################
# Main
####################
if ! super_user ; then
echo "ERROR: You must be super user to run this utility!"
exit 1
fi
if [ $# -lt 1 ]; then
connect
elif [ "$1" = "-h" ]; then
usage
else
reconnect
fi
添加执行权限
chmod +x vpn.sh
运行,即可连接vpn
./vpn.sh
断开vpn的脚本
#!/bin/bash
#
#wg.wang
#
L2TPD_PIPE=/var/run/xl2tpd/l2tp-control
echo "c zju" > $L2TPD_PIPE
killall xl2tpd
posted by Fan Bai @ 14:21,
