Advertisement
scramblevpn

Auto start openvpn

Sep 21st, 2013
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.96 KB | None | 0 0
  1. #!/bin/sh -e
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides: openvpn
  5. # Required-Start: $network $remote_fs $syslog
  6. # Required-Stop: $network $remote_fs $syslog
  7. # Should-Start: network-manager
  8. # Should-Stop: network-manager
  9. # X-Start-Before: $x-display-manager gdm kdm xdm wdm ldm sdm nodm
  10. #========================================================
  11. # Script to start openvpn after reboot
  12. # There is be a better, easier way, installing openvpn as a service
  13. # with “update-rc.d openvpn defaults”, but I didn’t get it working
  14. # If you can, please post instructions in the comments.
  15. # Until then, we use a script to start openvpn after reboot
  16. #
  17. # Openvpn auto startup script taken from https://forums.openvpn.net/topic10028.html
  18. # I edited for raspberry PI and change paths as follows
  19. # DAEMON=/usr/local/sbin/openvpn
  20. # CONFIG_DIR=/home/pi/openvpn-release-2.3/
  21. #========================================================
  22.  
  23. # X-Interactive: true
  24. # Default-Start: 2 3 4 5
  25. # Default-Stop: 0 1 6
  26. # Short-Description: Openvpn VPN service
  27. ### END INIT INFO
  28.  
  29. # Original version by Robert Leslie
  30. # , edited by iwj and cs
  31. # Modified for openvpn by Alberto Gonzalez Iniesta
  32. # Modified for restarting / starting / stopping single tunnels by Richard Mueller
  33.  
  34. . /lib/lsb/init-functions
  35.  
  36. test $DEBIAN_SCRIPT_DEBUG && set -v -x
  37.  
  38. DAEMON=/usr/local/sbin/openvpn
  39. DESC="virtual private network daemon"
  40. CONFIG_DIR=/home/pi/openvpn-release-2.3/
  41. test -x $DAEMON || exit 0
  42. test -d $CONFIG_DIR || exit 0
  43.  
  44. # Source defaults file; edit that file to configure this script.
  45. AUTOSTART="all"
  46. STATUSREFRESH=10
  47. if test -e /etc/default/openvpn ; then
  48. . /etc/default/openvpn
  49. fi
  50.  
  51. start_vpn () {
  52. if grep -q '^[ ]*daemon' $CONFIG_DIR/$NAME.conf ; then
  53. # daemon already given in config file
  54. DAEMONARG=
  55. else
  56. # need to daemonize
  57. DAEMONARG="--daemon ovpn-$NAME"
  58. fi
  59.  
  60. if grep -q '^[ ]*status ' $CONFIG_DIR/$NAME.conf ; then
  61. # status file already given in config file
  62. STATUSARG=""
  63. elif test $STATUSREFRESH -eq 0 ; then
  64. # default status file disabled in /etc/default/openvpn
  65. STATUSARG=""
  66. else
  67. # prepare default status file
  68. STATUSARG="--status /var/run/openvpn.$NAME.status $STATUSREFRESH"
  69. fi
  70.  
  71. log_progress_msg "$NAME"
  72. STATUS=0
  73.  
  74. start-stop-daemon --start --quiet --oknodo \
  75. --pidfile /var/run/openvpn.$NAME.pid \
  76. --exec $DAEMON -- $OPTARGS --writepid /var/run/openvpn.$NAME.pid \
  77. $DAEMONARG $STATUSARG --cd $CONFIG_DIR \
  78. --config $CONFIG_DIR/$NAME.conf || STATUS=1
  79. }
  80. stop_vpn () {
  81. kill `cat $PIDFILE` || true
  82. rm -f $PIDFILE
  83. rm -f /var/run/openvpn.$NAME.status 2> /dev/null
  84. }
  85.  
  86. case "$1" in
  87. start)
  88. log_daemon_msg "Starting $DESC"
  89.  
  90. # autostart VPNs
  91. if test -z "$2" ; then
  92. # check if automatic startup is disabled by AUTOSTART=none
  93. if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
  94. log_warning_msg " Autostart disabled."
  95. exit 0
  96. fi
  97. if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  98. # all VPNs shall be started automatically
  99. for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  100. NAME=${CONFIG%%.conf}
  101. start_vpn
  102. done
  103. else
  104. # start only specified VPNs
  105. for NAME in $AUTOSTART ; do
  106. if test -e $CONFIG_DIR/$NAME.conf ; then
  107. start_vpn
  108. else
  109. log_failure_msg "No such VPN: $NAME"
  110. STATUS=1
  111. fi
  112. done
  113. fi
  114. #start VPNs from command line
  115. else
  116. while shift ; do
  117. [ -z "$1" ] && break
  118. if test -e $CONFIG_DIR/$1.conf ; then
  119. NAME=$1
  120. start_vpn
  121. else
  122. log_failure_msg " No such VPN: $1"
  123. STATUS=1
  124. fi
  125. done
  126. fi
  127. log_end_msg ${STATUS:-0}
  128.  
  129. ;;
  130. stop)
  131. log_daemon_msg "Stopping $DESC"
  132.  
  133. if test -z "$2" ; then
  134. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  135. NAME=`echo $PIDFILE | cut -c18-`
  136. NAME=${NAME%%.pid}
  137. stop_vpn
  138. log_progress_msg "$NAME"
  139. done
  140. else
  141. while shift ; do
  142. [ -z "$1" ] && break
  143. if test -e /var/run/openvpn.$1.pid ; then
  144. PIDFILE=`ls /var/run/openvpn.$1.pid 2> /dev/null`
  145. NAME=`echo $PIDFILE | cut -c18-`
  146. NAME=${NAME%%.pid}
  147. stop_vpn
  148. log_progress_msg "$NAME"
  149. else
  150. log_failure_msg " (failure: No such VPN is running: $1)"
  151. fi
  152. done
  153. fi
  154. log_end_msg 0
  155. ;;
  156. # Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
  157. reload|force-reload)
  158. log_daemon_msg "Reloading $DESC"
  159. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  160. NAME=`echo $PIDFILE | cut -c18-`
  161. NAME=${NAME%%.pid}
  162. # If openvpn if running under a different user than root we'll need to restart
  163. if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then
  164. stop_vpn
  165. sleep 1
  166. start_vpn
  167. log_progress_msg "(restarted)"
  168. else
  169. kill -HUP `cat $PIDFILE` || true
  170. log_progress_msg "$NAME"
  171. fi
  172. done
  173. log_end_msg 0
  174. ;;
  175.  
  176. # Only 'soft-restart' running VPNs. New ones will only start with 'start' or 'restart'.
  177. soft-restart)
  178. log_daemon_msg "$DESC sending SIGUSR1"
  179. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  180. NAME=`echo $PIDFILE | cut -c18-`
  181. NAME=${NAME%%.pid}
  182. kill -USR1 `cat $PIDFILE` || true
  183. log_progress_msg "$NAME"
  184. done
  185. log_end_msg 0
  186. ;;
  187.  
  188. restart)
  189. shift
  190. $0 stop ${@}
  191. sleep 1
  192. $0 start ${@}
  193. ;;
  194. cond-restart)
  195. log_daemon_msg "Restarting $DESC."
  196. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  197. NAME=`echo $PIDFILE | cut -c18-`
  198. NAME=${NAME%%.pid}
  199. stop_vpn
  200. sleep 1
  201. start_vpn
  202. done
  203. log_end_msg 0
  204. ;;
  205. status)
  206. GLOBAL_STATUS=0
  207. if test -z "$2" ; then
  208. # We want status for all defined VPNs.
  209. # Returns success if all autostarted VPNs are defined and running
  210. if test "x$AUTOSTART" = "xnone" ; then
  211. # Consider it a failure if AUTOSTART=none
  212. log_warning_msg "No VPN autostarted"
  213. GLOBAL_STATUS=1
  214. else
  215. if ! test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  216. # Consider it a failure if one of the autostarted VPN is not defined
  217. for VPN in $AUTOSTART ; do
  218. if ! test -f $CONFIG_DIR/$VPN.conf ; then
  219. log_warning_msg "VPN '$VPN' is in AUTOSTART but is not defined"
  220. GLOBAL_STATUS=1
  221. fi
  222. done
  223. fi
  224. fi
  225. for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  226. NAME=${CONFIG%%.conf}
  227. # Is it an autostarted VPN ?
  228. if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  229. AUTOVPN=1
  230. else
  231. if test "x$AUTOSTART" = "xnone" ; then
  232. AUTOVPN=0
  233. else
  234. AUTOVPN=0
  235. for VPN in $AUTOSTART; do
  236. if test "x$VPN" = "x$NAME" ; then
  237. AUTOVPN=1
  238. fi
  239. done
  240. fi
  241. fi
  242. if test "x$AUTOVPN" = "x1" ; then
  243. # If it is autostarted, then it contributes to global status
  244. status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  245. else
  246. status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}' (non autostarted)" || true
  247. fi
  248. done
  249. else
  250. # We just want status for specified VPNs.
  251. # Returns success if all specified VPNs are defined and running
  252. while shift ; do
  253. [ -z "$1" ] && break
  254. NAME=$1
  255. if test -e $CONFIG_DIR/$NAME.conf ; then
  256. # Config exists
  257. status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  258. else
  259. # Config does not exist
  260. log_warning_msg "VPN '$NAME': missing $CONFIG_DIR/$NAME.conf file !"
  261. GLOBAL_STATUS=1
  262. fi
  263. done
  264. fi
  265. exit $GLOBAL_STATUS
  266. ;;
  267. *)
  268. echo "Usage: $0 {start|stop|reload|restart|force-reload|cond-restart|soft-restart|status}" >&2
  269. exit 1
  270. ;;
  271. esac
  272.  
  273. exit 0
  274.  
  275. # vim:set ai sts=2 sw=2 tw=0:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement