获取VPN IP地址

时间:2013-01-31 23:38:11

标签: c# vpn network-interface

如何从PPP或PPPT vpn连接获取IP?

我知道VPN连接的名称,所以如何过滤

var nics = NetworkInterface.GetAllNetworkInterfaces();

只获取名为“VPNConnection”的vpn接口,并获取给定的ip地址?

2 个答案:

答案 0 :(得分:3)

也许:

using System.Linq;

var vpn = NetworkInterface.GetAllNetworkInterfaces()
                          .First(x => x.Name == "VPNConnection");
编辑:哎呀我没有完成:

var ip = vpn.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).Address.ToString();

答案 1 :(得分:0)

我已经为这种情况提出了申请。使用带有以下命令的批处理文件,我执行了一个过程,该过程执行.bat文件并将ip存储在变量ip

            //make a batch file and write this in it
        File.WriteAllText(FolderPath + "\\getip.bat", "@echo off \r\n FOR /F \"tokens=1-6 delims=:. \" %%a in ('netsh int ip show address \"SanadConnectVPN\" ^|find \"IP Address\"') do set IP=%%c.%%d.%%e.%%f \r\n echo %IP%");

        var newProcess = new Process
        {
            StartInfo =
            {
                FileName = FolderPath + "\\getip.bat",
                WindowStyle = ProcessWindowStyle.Normal
            }
        };
        newProcess.StartInfo.RedirectStandardOutput = true;
        newProcess.StartInfo.UseShellExecute = false;
        newProcess.Start();
        string ip = newProcess.StandardOutput.ReadToEnd();
        newProcess.WaitForExit();
        MessageBox.Show(ip);
        return ip