Openvpn and shell script

This forum is for admins who are looking to build or expand their OpenVPN setup.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Forum rules
Please use the [oconf] BB tag for openvpn Configurations. See viewtopic.php?f=30&t=21589 for an example.
Post Reply
joeskop
OpenVpn Newbie
Posts: 4
Joined: Thu Nov 18, 2010 11:35 am

Openvpn and shell script

Post by joeskop » Thu Nov 18, 2010 11:41 am

Hi,

I need to create a script (on a linux environment) that does:

- launch the openvpn client up (openvpn remote_factory.ovpn);
- when the openvpn is up, execute a script on the local server that brings some datas on the "ovpnenned" remote server (/bin/sh somescript.sh);
- when the execution og the script is finished, stop the ovpn (thought killall openvpn or something better).

I tryed ti find informations almost everywere, probably someone did it but I can't find infos.

I also know I can do all with a coule of scripts and semaphore technics, but if I can do this more simple is better than playing with files...

Thanks for everyone that can help on it!

REgards,
JS

marto72
OpenVpn Newbie
Posts: 3
Joined: Thu Nov 18, 2010 10:58 am

Re: Openvpn and shell script

Post by marto72 » Thu Nov 18, 2010 12:41 pm

I would go for something like

/etc/rc.d/openvpn start ( o service openvpn start depending on the version of linux you have).
rsh myremoteserver /home/martin/myscript.sh
/etc/rc.d/openvpn stop.

Of course you can add checks like a loop until its finished and the call the stop.

joeskop
OpenVpn Newbie
Posts: 4
Joined: Thu Nov 18, 2010 11:35 am

Re: Openvpn and shell script

Post by joeskop » Thu Nov 18, 2010 1:18 pm

Hi, Thanks for the answer.

The problem of using daemon (/etc/init.d/openvpn start|stop) is that in this server there are many ovpn confs, so I must start the openvpn like how I wrote, "openvpn remote_factory.ovpn"... on non daemon method...

This is the problem.

Regards,
JS

joeskop
OpenVpn Newbie
Posts: 4
Joined: Thu Nov 18, 2010 11:35 am

Re: Openvpn and shell script

Post by joeskop » Thu Nov 18, 2010 2:23 pm

Hi,

some other news:

I tried both this methods, but non luck:

openvpn --up 'script_to_run.sh' --config factory.ovpn --script-security 3

and

openvpn --route-delay 10 --route-up 'script_to_run.sh' --config factory.ovpn --script-security 3

In the first example, the routes are not added, and the execution of the script fails; on the 2nd, the routes are added but the initialization isn't completed, so the script fails also.

If someone have any idea... thanks...

Regards,
JS

joeskop
OpenVpn Newbie
Posts: 4
Joined: Thu Nov 18, 2010 11:35 am

SOLVED Re: Openvpn and shell script

Post by joeskop » Thu Nov 18, 2010 4:13 pm

Hi,

I respond again to myself: i solved using the --daemon option.

I did a simple bash script where first i create the connession ovpn:

Code: Select all

openvpn --config remote_firm.ovpn --daemon;
after, e sleep for be sure that everything is ok:

Code: Select all

sleep 10;
after, I execute the script:

Code: Select all

./execute_script.sh
And at the end, I kill the openvpn process;

Code: Select all

killall openvpn;
Everything ok.

Regards,
JS

User avatar
krzee
Forum Team
Posts: 728
Joined: Fri Aug 29, 2008 5:42 pm

Re: SOLVED Re: Openvpn and shell script

Post by krzee » Fri Nov 19, 2010 12:07 am

in that case

Code: Select all

#!/path/to/bash
openvpn --writepid remote_firm.pid --daemon --config remote_firm.ovpn
sleep 10
./execute_script.sh
pkill -F remote_firm.pid
hope that helps
(you may need to install pkill)

Post Reply