3

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

我知道 VPN 连接的名称,那么我该如何过滤

var nics = NetworkInterface.GetAllNetworkInterfaces();

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

4

2 回答 2

6

也许:

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();
于 2013-01-31T23:43:50.117 回答
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
于 2020-07-27T10:59:06.567 回答