OpenWrt Forum Archive

Topic: Easy startup script for OpenVPN?

The content of this topic has been archived on 26 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Currently running OpenWRT 10.03.1 Luci on my WNDR3700v2 and working flawlessly.
I also have setup an OpenVPN connection and working ok, iptables and routing also working.

The main thing is that I have to start OpenVPN every time the router starts with this command: openvpn --cd /etc/openvpn --config ovpn140.ovpn

I have tried some scripts and played with the S95done and S95openvpn. The problem is that if it starts, not sure if it do, there's no internet connetion sad
I remove all the scripts and lines, reboot router and start with the command, everything is ok.

Is it possible to run the command after 2 min or etc. when the router is finally booted and WAN is up?

Conclusion, I just want OpenVPN to start when the router is up and have WAN IP and finished with the booting smile

Maybe you need hotplug? If I'm not mistaking, there can be a script in /etc/hotplug/iface or in /etc/hotplug/net which is executed when the interface is taken up. Consult here.

Thanks, looks like /etc/hotplug.d/iface should be my right place since the DDNS is in that folder (25-ddns).
So the 25-ddns file looks like this:

#!/bin/sh

. /usr/lib/ddns/dynamic_dns_functions.sh

if [ "$ACTION" = "ifup" ]; then
        start_daemon_for_all_ddns_sections "$INTERFACE"
fi

If I create a file with name "30-openvpn", how can I make a script to run the openvpn like in my first post?
Writing scripts is not normally for me tongue

I think something like this sould work

#!/bin/sh

if [ "$ACTION" = "ifup" && "$INTERFACE"="<name of your wan interface, i.e. ppp0 or 3g-wan or ...>" ]; then
        openvpn --cd /etc/openvpn --config ovpn140.ovpn &
fi

What about just using /etc/config/openvpn ?

package openvpn

#################################################
# Sample to include a custom config file.       #
#################################################

config openvpn custom_config

    # Set to 1 to enable this instance:
    option enabled 0

    # Include OpenVPN configuration
    option config /etc/openvpn/my-vpn.conf

...

jow wrote:

What about just using /etc/config/openvpn ?

package openvpn

#################################################
# Sample to include a custom config file.       #
#################################################

config openvpn custom_config

    # Set to 1 to enable this instance:
    option enabled 0

    # Include OpenVPN configuration
    option config /etc/openvpn/my-vpn.conf

...

Here's my settings in beginning of it:


config 'openvpn' 'custom_config'
        option 'config' '/etc/openvpn/my-vpn.conf'

Then I replace my-vpn.conf to ovpn140.conf
and then add the line "option enabled 1"?

Yes. But that'll obviously only work if the openvpn init script is still the original one.

jow wrote:

Yes. But that'll obviously only work if the openvpn init script is still the original one.

No problem, the script is set back to it's original and I will test smile

But, should i delete rest of the lines in the /etc/config/openvpn file?

You can delete it, yes. But the rest is disabled by default anyway, so it won't interfere.

Hmm...that trick did not the work. It's not starting the OpenVPN and there is no OpenVPN process running.
Did I miss something?

If your config is indeed failing without "--cd /etc/openvpn" you have to add "option cd /etc/openvpn" as well.

jow wrote:

If your config is indeed failing without "--cd /etc/openvpn" you have to add "option cd /etc/openvpn" as well.

Tried that, without luck. Here's my config: http://img715.imageshack.us/img715/3214/19662860.png

Maybe I have to delete the other config anyway so it just run the custom config?

Edit: My ovpn140 file suppose to be ovpn160 from the beginning.

(Last edited by regeli on 12 Jan 2012, 21:03)

Alleb57, can you confirm if that script will work or not?
The second trick I got in this thread did not work for me sad

This automatically starts openvpn at boot for me, added the following line to file: /etc/rc.local (LuCi local startup file)

openvpn /etc/config/openvpn &

The ampersand is required.

(Last edited by robrob on 13 Jan 2012, 23:49)

regeli wrote:

Alleb57, can you confirm if that script will work or not?

What's the problem, just use it and see whether it works or not wink

robrob, I tried that earlier, but did not work. The router is starting up, but no VPN process is running sad

I also tried with the suggestion from Alleb57, without success:

#!/bin/sh

if [ "$ACTION" = "ifup" && "$INTERFACE"="pppoe-wan" ]; then
        openvpn --cd /etc/openvpn --config ovpn160.ovpn &
fi

Not sure why nothing works, I'll bet I have to check some logs or make a cronjob after 2 min the router is up.
This automatically start stuff works in DD-WRT, but may take up to 2 min after WAN is up before it's trying to reconnect.

Is there possible to add a line with "wait 1 min" after the interface is up?

(Last edited by regeli on 15 Jan 2012, 00:19)

regeli wrote:

Not sure why nothing works, I'll bet I have to check some logs or make a cronjob after 2 min the router is up.
This automatically start stuff works in DD-WRT, but may take up to 2 min after WAN is up before it's trying to reconnect.

Try adding debug info to the script, something like

#!/bin/sh
logger -t test ACTION="$ACTION", INTERFACE="$INTERFACE"

if [ "$ACTION" = "ifup" && "$INTERFACE"="pppoe-wan" ]; then
        logger -t test Trying to start openvpn
        openvpn --cd /etc/openvpn --config ovpn160.ovpn &
fi

If you need to wait, try adding
sleep 120
before starting openvpn

I wonder why you even need to sync your openvpn startup to the wan state, usually the process just tries to reach its peer indefinitely, so there's no need to wait until wan is up.

If the current openvpn config includes a resolv-retry parameter, make sure its set to "infinite" or remove it entirely. Also add "verb 5" to increase the log level and add "syslog"
to make openvpn send all its info to logread.

jow, here's the VPN settings I have:

remote 123.456.789.123 4672
proto udp
ca ca.crt
cert ovpn160.crt
key ovpn160.key
tls-auth ta.key 1
client
dev tun
resolv-retry infinite
nobind
persist-key
persist-tun
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
verb 4
mute 5
tun-mtu 1500
route-method exe
route-delay 2
comp-lzo adaptive
explicit-exit-notify 2
fragment  1390
mssfix 1390

I will add the logging and see what's going on smile

The problem is fixed smile

Just edited the rc.local file with the sleep command.

Thanks folks!

The discussion might have continued from here.