Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know whether a VPN connection is established or not?

I have a VPN connection. In order to establish the VPN connection, there is a PPTP.bk file which must be executed. Upon running this file and entering the credentials, the VPN connection is established.

I am trying to connect and disconnect the VPN connection programmatically. The catch is there is no VPN connection created in the Windows so I need to be able to verify any VPN connection at any time and if it is not present establish one.

like image 304
Hossein Avatar asked Sep 01 '12 12:09

Hossein


People also ask

How is a VPN connection established?

A VPN is created by establishing a virtual point-to-point connection through the use of dedicated circuits or with tunneling protocols over existing networks. A VPN available from the public Internet can provide some of the benefits of a wide area network (WAN).


3 Answers

A slight modification - this is the code that worked for me.

public bool CheckForVPNInterface()
    {
        if (NetworkInterface.GetIsNetworkAvailable())
        {
            NetworkInterface[] interfaces = 
NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Interface in interfaces)
            {
                // This is the OpenVPN driver for windows. 
                if (Interface.Description.Contains("TAP-Windows Adapter")
                  && Interface.OperationalStatus == OperationalStatus.Up)
                {
                        return true;
                }
            }
        }
        return false;
    }
like image 74
Mike Avatar answered Oct 11 '22 16:10

Mike


I check the VPN connection status using the NetworkInterface class. Here is the code I wrote for this goal:

if (NetworkInterface.GetIsNetworkAvailable())
{
    NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface  Interface in interfaces)
    {
        if (Interface.OperationalStatus == OperationalStatus.Up)
        {
            if ((Interface.NetworkInterfaceType == NetworkInterfaceType.Ppp) && (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback))
            {
                IPv4InterfaceStatistics statistics = Interface.GetIPv4Statistics();
                MessageBox.Show(Interface.Name + " "  + Interface.NetworkInterfaceType.ToString() + " " + Interface.Description);
            }
            else
            {
                MessageBox.Show("VPN Connection is lost!");
            }
        }
    }
}
like image 30
Hossein Avatar answered Oct 11 '22 14:10

Hossein


On my network driver there was Cisco in the description text. Here is a more up-todate version which is only a few lines:

public static class VPNCheck
{
    public static bool IsOn()
    {
        return ((NetworkInterface.GetIsNetworkAvailable())
                && NetworkInterface.GetAllNetworkInterfaces()
                                   .FirstOrDefault(ni => ni.Description.Contains("Cisco"))?.OperationalStatus == OperationalStatus.Up);
    }
}
like image 4
ΩmegaMan Avatar answered Oct 11 '22 14:10

ΩmegaMan