user3759159 user3759159 - 3 months ago 17
PHP Question

Wait for VPN connection to be established loop

I was looking for a way in PHP to check and wait for vpn connection (tun0 interface) to come up before continuing with the code
here is what I come up with but it's not seeing any change after interface tun0 is up

$val = exec("/bin/netstat -i | grep tun0 | wc -l");
while($val == "0")
{
echo "Still Not Connected\n";
sleep(2);
}
echo "VPN Connected";

Answer

Your command is just not in the loop :p

do
{
    $val = exec("/bin/netstat -i | grep tun0 | wc -l");

    if( $val == "0" ){ echo "Still Not Connected\n"; sleep(2); }

} while($val == "0")

echo "VPN Connected";

I'm using do-while structure because you will do at least one time the loop.