Friday, January 18, 2008

Start/Stop openvpn script

Днес ми се наложи да направя VPN до Исландия с openvpn и дадения ми ключ (zlatozar.ovpn). Под линукс малко се озорих. То не бяха bridges то не беше чудо. Затова реших да сложа скрипта, който написах с много помощ от Google.
Първо си проверявам ip-то и routing таблицата:

$sudo ifconfig
$sudo route -n

След това стартирам скрипта (кръстил съм го open_vpn_script.sh):
#!/bin/bash

#################################
#
# Set up Ethernet bridge on Linux
# Requires: bridge-utils
#
#NOTE:
# iptables -A INPUT -i tap0 -j ACCEPT
# iptables -A INPUT -i br0 -j ACCEPT
# iptables -A FORWARD -i br0 -j ACCEPT
#
#################################

# Define Bridge Interface
br="br0"

# Define list of TAP interfaces to be bridged,
# for example tap="tap0 tap1 tap2".
tap="tap0"

# Define physical ethernet interface to be bridged
# with TAP interface(s) above.
eth="eth0"

eth_ip="your current ip"

eth_netmask="255.255.255.0"
eth_broadcast="xxx.xxx.xxx.255"

gw="your  getway"

case "$1" in

    start)
        for t in $tap; do
            openvpn --mktun --dev $t
        done

        brctl addbr $br
        brctl addif $br $eth

        for t in $tap; do
            brctl addif $br $t
        done

        ifconfig $eth 0.0.0.0 promisc up

        ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast
        route add default gw $gw
        ;;

    stop)
        ifconfig $br down
        brctl delbr $br

        for t in $tap; do
            openvpn --rmtun --dev $t
        done

        ifconfig $eth $eth_ip netmask $eth_netmask broadcast $eth_broadcast
        route add default gw $gw
        ;;

    *)
    echo "usage openvpn-bridge {start|stop}"

    exit 1
    ;;
esac

exit 0



И сега вече пускам смело:
# Start
sudo ./open_vpn_script.sh start
sudo openvpn zlatozar.ovpn

# Stop
sudo ./open_vpn_script.sh stop
sudo route -n


Сега ми изглежда лесно, но не мислех така сутринта.

No comments:

algorithms (1) cpp (3) cv (1) daily (4) emacs (2) freebsd (4) java (3) javascript (1) JSON (1) linux (2) Lisp (7) misc (8) programming (16) Python (4) SICP (1) source control (4) sql (1) думи (8)