Getting output from OpenVPN in a perl script

How to customize and extend your OpenVPN installation.

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

Post Reply
Kr0nZ
OpenVpn Newbie
Posts: 1
Joined: Wed Mar 07, 2012 5:36 pm

Getting output from OpenVPN in a perl script

Post by Kr0nZ » Wed Mar 07, 2012 5:49 pm

hi im trying to write a perl script to control what server my openvpn client connects to

from my perl script i have

Code: Select all

    my $openvpn = "/usr/sbin/openvpn";
    my %servers = ("usa" => "/etc/openvpn/ustcp.conf",
                    "uk" => "/etc/openvpn/uktcp.conf");
    my $cmdStr = $openvpn . " " . $servers{$serverToStart} . "  |";
    open(STDIN, $cmdStr);
    while( <> ) { print $_ }
but the while loop never executes and the openvpn output never gets set to $_
I can see the output on my terminal, put the ouput never gets piped through my perl script

this code works fine if i use another program, for example tcpdump


In my final script i want to have something like:
-i issue a command from wherever (terminal/3rd party program) that will start a script
-if openvpn isnt running then a second(main script) starts in deamon mode
-this script then checks my main script to see which server openvpn is connected to
-if the server that its connected to is the same as the one i want to connect to then it sends a connected msg back to the calling script
-else it disconnects openvpn, and reconnects to new server

User avatar
janjust
Forum Team
Posts: 2703
Joined: Fri Aug 20, 2010 2:57 pm
Location: Amsterdam
Contact:

Re: Getting output from OpenVPN in a perl script

Post by janjust » Wed Mar 07, 2012 7:26 pm

you're not capturing stderr - and with the current script openvpn is refusing to start due to a misconfiguration: try changing
my $cmdStr = $openvpn . " " . $servers{$serverToStart} . " |";
to

Code: Select all

my $cmdStr = $openvpn . " --config " . $servers{$serverToStart} . "  |";

Post Reply