3 # Modifed by: Michael Asher <asherlm [at] gmail dot com
4 # Last Modified: 06/07/2010
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.
13 .
/usr
/lib
/network
/globals
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"
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"
28 if [ -z "${WIRED_INTERFACE}" ]; then
31 chkwiredint
=`/sbin/ifconfig ${WIRED_INTERFACE} | grep -oi 'RUNNING'`
34 if [ -z "${WIRELESS_INTERFACE}" ]; then
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
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."
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"
69 checkrunninginterfaces
(){
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"
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
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
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.
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
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
119 interfaces
=$interfaces+1
125 if [[ "$donotrunint" == "0" ]]; then
126 if [[ "$interfaces" == "0" ||
$interfaces == "" ]]; then
127 stat_busy
"No interfaces found running. VPN not established."
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
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
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
170 echo "Usage: $0 {start|stop|restart|kill}"
175 # vim: ft=sh ts=4 et sw=4: