Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically connect to a VPN?

I have a VPN connection that I keep losing, that I need to connect to our DB server, but every second or third connection fails because I have lost the VPN connection. I'd like to add somde code - for DEBUG config only - to check the VPN connection and reconnect if necessary, before proceeding to attempt the database connection.

like image 745
ProfK Avatar asked Aug 16 '11 10:08

ProfK


People also ask

Can I use a VPN to connect to my work VPN?

You can connect across a VPN no matter what network you're on and “appear” to be sitting at your desk at work using all the resources you could if you actually were there.

How do I find my VPN server address Android?

Open the Settings app and tap “More” under Wireless & Networks. Tap the VPN option on the Wireless & Networks screen. Tap the + button and provide the VPN's details.


1 Answers

You could use System.Net.NetworkInformation.Ping to check if the connection is up - then rasdial to reconnect the vpn if the connection is lost.

eg

System.Diagnostics.Process.Start("rasdial.exe", "VPNName Username Password"); 

You can also disconnect the VPN using

System.Diagnostics.Process.Start("rasdial.exe", "VPNName /d"); 

Update

Calling rasdial with no arguments returns a list of open connections - might be a more robust solution for you.

like image 179
PaulB Avatar answered Oct 22 '22 16:10

PaulB