updated on Fri Jan 13 00:14:41 UTC 2012
[aur-mirror.git] / netcfg-vpnc / net-auto-vpnc
blob653f949763b7ef3fc023b786c7f8ad52c6dbe62a
1 #!/bin/bash
3 # Modifed by: Michael Asher <asherlm [at] gmail dot com
4 # Last Modified: 06/07/2010
5 #
6 # This script is heavily inspired from the net-auto-wireless script
7 # This will allow you to connect at boot time to the VPN.
8 # You will need to define a netcfg entry (/etc/networks/examples/vpnc.example)
9 # In order to use the script.
11 . /etc/rc.conf
12 . /etc/rc.d/functions
13 . /usr/lib/network/globals
15 # Variables
16 whichroute=`/usr/sbin/ip route | grep default | grep -o "dev\ [a-z0-9]\{3,5\}"|awk '{print $2}'` # Get default route interface
17 # Is VPNC even installed?
18 if [[ ! -x /usr/sbin/vpnc ]]; then
19 echo "Please install 'vpnc' to use net-auto-vpnc"
20 exit 0
21 fi
22 # Check to see if a VPNC Connection has been defined
23 if [ -z "${VPN_CONNECTION}" ]; then
24 report_fail "No VPN_CONNECTION name set. Set it by adding VPN_CONNECTION=\"NETCFG VPNC configuration\" to rc.conf"
25 exit 0
28 if [ -z "${WIRED_INTERFACE}" ]; then
29 chkwiredint="NULL"
30 else
31 chkwiredint=`/sbin/ifconfig ${WIRED_INTERFACE} | grep -oi 'RUNNING'`
34 if [ -z "${WIRELESS_INTERFACE}" ]; then
35 chkwirelessint="NULL"
36 else
37 chkwirelessint=`/sbin/ifconfig ${WIRELESS_INTERFACE} | grep -oi 'RUNNING'`
40 if [ -z "${VPN_ROUTE_WAITTIME}" ]; then
41 VPN_ROUTE_WAITTIME="NULL"
44 if [ -z "${VPN_ROUTE_WAITTIME}" ]; then
45 VPN_INTERFACE_WAITTIME="NULL"
47 # Additional Variables
48 whichroute=`/usr/sbin/ip route | grep default | grep -o "dev\ [a-z0-9]\{3,5\}"|awk '{print $2}'` # Get default route interface
50 checkroutes(){
51 if [ "$whichroute" == "" ]; then
52 if [[ "${VPN_ROUTE_WAITTIME}" == "" || "${VPN_ROUTE_WAITTIME}" == "NULL" ]]; then
53 stat_busy "Default route is not defined. Not running VPN."
54 stat_fail
55 exit 1
56 else
57 stat_busy "Default route is not defined. Will try again in ${VPN_ROUTE_WAITTIME} seconds" # This is needed to give my wireless time to show up with an IP.
58 sleep ${VPN_ROUTE_WAITTIME}
59 whichroute=`/usr/sbin/ip route | grep default | grep -o "dev\ [a-z0-9]\{3,5\}"|awk '{print $2}'`
60 if [ "$whichroute" == "" ]; then
61 stat_busy "Default route is currently not defined. Not running VPN"
62 stat_fail
63 exit 1
69 checkrunninginterfaces(){
70 interfaces=0
71 donotrunint=0
72 if [[ "$chkwiredint" == "" || "$chkwiredint" == "NULL" ]]; then
73 if [ "$chkwirelessint" == "" ]; then
74 if [[ "${VPN_INTERFACE_WAITTIME}" == "" || "${VPN_INTERFACE_WAITTIME}" == "NULL" ]]; then
75 stat_busy "VPN_INTERFACE_WAITTIME not defined in rc.conf"
76 stat_busy "No interfaces are currently available. VPN not established"
77 stat_fail
78 exit 0
79 else
80 stat_busy "No interfaces are currently available. Trying again in ${VPN_INTERFACE_WAITTIME} seconds."
81 sleep ${VPN_INTERFACE_WAITTIME}
83 elif [ "$chkwirelessint" == "RUNNING" ]; then
84 if [ "${VPN_DONOTRUNONTHISINTERFACE}" == "${WIRELESS_INTERFACE}" ]; then
85 stat_busy "${WIRELESS_INTERFACE} has been configured to not auto-start VPN"
86 donotrunint=$donotrunint+1
87 else
88 interfaces=$interfaces+1
91 elif [ "$chkwiredint" == "RUNNING" ]; then
92 if [ "${VPN_DONOTRUNONTHISINTERFACE}" == "${WIRED_INTERFACE}" ]; then
93 stat_busy "${WIRED_INTERFACE} has been configured to not auto-start VPN"
94 donotrunint=$donotrunint+1
95 else
96 interfaces=$interfaces+1
99 # Run it again - Assuming items above failed
100 chkwiredint=`/sbin/ifconfig ${WIRED_INTERFACE} | grep -oi 'RUNNING'`
101 chkwirelessint=`/sbin/ifconfig ${WIRELESS_INTERFACE} | grep -oi 'RUNNING'`
102 if [[ "$chkwiredint" == "" || "$chkwiredint" == "NULL" ]]; then
103 if [ "$chkwirelessint" == "" ]; then
104 stat_busy "No interfaces found running. VPN not established." # No need to try again. Get through to finish booting.
105 stat_fail
106 exit 0
107 elif [ "$chkwirelessint" == "RUNNING" ]; then
108 if [ "${VPN_DONOTRUNONTHISINTERFACE}" == "${WIRELESS_INTERFACE}" ]; then
109 stat_busy "${WIRED_INTERFACE} has been configured to not auto-start VPN"
110 donotrunint=$donotrunint+1
111 else
112 interfaces=$interfaces+1
114 elif [ "$chkwiredint" == "RUNNING" ]; then
115 if [ "${VPN_DONOTRUNONTHISINTERFACE}" == "${WIRED_INTERFACE}" ]; then
116 stat_busy "${WIRED_INTERFACE} has been configured to not auto-start VPN"
117 donotrunint=$donotrunint+1
118 else
119 interfaces=$interfaces+1
124 # Final check
125 if [[ "$donotrunint" == "0" ]]; then
126 if [[ "$interfaces" == "0" || $interfaces == "" ]]; then
127 stat_busy "No interfaces found running. VPN not established."
128 stat_fail
129 exit 0
131 else
132 exit 0
136 case "$1" in
137 start)
138 if ! ck_daemon net-auto-vpnc; then
139 exit_stderr "net-auto-vpnc has already been started: try \"/etc/rc.d/net-auto-vpnc restart\""
141 checkrunninginterfaces
142 checkroutes
143 stat_busy "Starting netcfg auto-vpnc connection ${VPN_CONNECTION}"
144 /usr/bin/netcfg up "${VPN_CONNECTION}"
145 if [ $? -eq 0 ]; then
146 add_daemon net-auto-vpnc
147 stat_done
148 else
149 stat_fail
152 stop)
153 if ! ck_daemon net-auto-vpnc; then
154 stat_busy "Stopping netcfg auto-vpnc connection ${VPN_CONNECTION}"
155 /usr/bin/netcfg down "${VPN_CONNECTION}" >>/dev/null 2>&1
156 if [ $? -eq 0 ]; then
157 rm_daemon net-auto-vpnc
158 stat_done
159 else
160 stat_fail
164 restart)
165 "$0" stop
166 sleep 1
167 "$0" start
170 echo "Usage: $0 {start|stop|restart|kill}"
171 exit 1
173 esac
174 exit 0
175 # vim: ft=sh ts=4 et sw=4: