【问题标题】:Get result after connecting to a VPN连接到 VPN 后获取结果
【发布时间】:2015-01-01 07:51:20
【问题描述】:

我可以连接到使用以下代码创建的现有 VPN。

string args = string.Format("{0} {1} {2}", connVpnName, connUserName, connPassWord);
var proc = new System.Diagnostics.Process
{
    StartInfo = new System.Diagnostics.ProcessStartInfo
    {
        FileName = "C:\\WINDOWS\\system32\\rasdial.exe",
        Arguments = args,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};
proc.Start();
string output = "";
while (!proc.StandardOutput.EndOfStream)
{
    output += proc.StandardOutput.ReadLine();
    txtOutput.Text += proc.StandardOutput.ReadLine();
}

我需要知道连接是否成功、使用了错误的凭据、VPN 不存在或 ip 是否在线。

我的第一个想法是从命令提示符获取输出并搜索诸如“已连接”之类的关键字。这种方法的问题是我的用户使用多种语言,关键字会不同。

我可以使用其他方法来实现吗?

【问题讨论】:

  • 你能 ping 子网中的一台机器来检查你是否连接吗?
  • 直接拨打RasDial怎么样? msdn.microsoft.com/en-us/library/windows/desktop/… 或通过检查 Win32_NetworkAdapterConfiguration WMI 类中的更改来检测 VPN 连接
  • 本人对rasdial.exe 没有经验,建议您在命令行中使用相同的参数进行尝试。当你手动操作时它是否有效?
  • @Philippe:是的,我可以,但是我需要连接 200 多个 VPN(此程序适用于帮助台公司),而且我并不总是知道那里的 ip存在于子网上。
  • @maartenvdv 啊,好的。我已经实施了一个类似的解决方案,其中将定期检查连接并在需要时重新建立连接。不过,它看起来对你不可行。

标签: c# .net vpn


【解决方案1】:

我找到了答案:

            proc.Start();

            //het resultaat bekijken
            proc.WaitForExit();
            switch (proc.ExitCode)
            {
                case 0: //connection succeeded
                    MessageBox.Show("Je bent geconnecteerd met de VPN");
                    isConnectedVPN = true;
                    btnConnectToVPN.Text = "Stop VPN";
                    break;
                case 691: //wrong credentials
                    MessageBox.Show("De username en/of het wachtwoord kloppen niet.");
                    break;
                case 623: // The VPN doesn't excist
                    MessageBox.Show("Deze VPN staat niet tussen de bestaande VPN's.");
                    break;
                case 868: //the IP or domainname can't be found
                    MessageBox.Show("Het ip-adres of de domeinnaam kan niet gevonden worden");
                    break;
                default: //other faults
                    MessageBox.Show("fout " + proc.ExitCode.ToString());
                    break;
            }

【讨论】:

【解决方案2】:

使用DotRAS 这个库是windows api 的包装器。几乎所有功能都记录在 MSDN 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2011-03-28
    • 1970-01-01
    • 2012-11-03
    • 2020-09-30
    • 2018-02-11
    相关资源
    最近更新 更多