在远程会话中查找物理机的IP地址 [英] Finding IP address of physical machine when in a remote session

查看:122
本文介绍了在远程会话中查找物理机的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过stackoverflow,但是它所描述的只是在远程工作时如何获取远程IP地址.可以执行以下操作:

I have looked around stackoverflow, but all it describes is how to get a remote IP address when you're working remote. That can be done doing the following:

var ipHostEntry = Dns.GetHostEntry(string.Empty);
var ipAddressList = ipHostEntry.AddressList;
var ipv4Addresses = Array.FindAll(ipAddressList, a => a.AddressFamily == AddressFamily.InterNetwork);
result = ipv4Addresses[0].ToString();

result是远程IP地址(或本地IP地址,取决于您是否在远程工作).

Where result is the remote IP address (or the local one, depending on whether or not you're working remote).

但是,我想要以下内容:

However, I want the following:

  • 我正在远程工作(使用RDP)
  • 我需要物理计算机(运行RDP的计算机才能连接到另一台计算机)而不是远程计算机的IP

有什么方法可以使用C#完成此操作吗?

Is there any way to accomplish this using C#?

推荐答案

我阅读了很多行代码,并浏览了许多文档页面,但这帮助我解决了问题:

I read many many lines of codes and went through many documentation pages, but this helped me solve it: https://www.codeproject.com/Articles/111430/Grabbing-Information-of-a-Terminal-Services-Sessio

以上内容可帮助您获取所有会话及其对应IP地址的列表.但是,如果到一台远程服务器的RDP连接很多,那么您将需要获取特定客户端的IP地址,我这样解决了这个问题:

The above helps you get a list of all sessions and their corresponding IP addresses. However, if you have many RDP connections to one remote server, then you'll want to get the IP address of your specific client, I solved that like this:

[DllImport("Kernel32.dll")]
static extern int GetCurrentProcessId();

[DllImport("Kernel32.dll")]
static extern int ProcessIdToSessionId(int dwProcessid, ref int pSessionId);

[StructLayout(LayoutKind.Sequential)]
private struct WTS_CLIENT_ADDRESS
{
    public int iAddressFamily;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public byte[] bAddress;
}

private static string GetIpAddressFromSession(int sessionId, IntPtr server)
{
    var ipAddress = string.Empty;
    if (WTSQuerySessionInformation(server, sessionId, WTS_INFO_CLASS.WTSClientAddress, out IntPtr ipAddressPointer, out uint returnedBytes) == true)
    {
        var clientAddress = new WTS_CLIENT_ADDRESS();
        clientAddress = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(ipAddressPointer, clientAddress.GetType());

        ipAddress = string.Format("{0}.{1}.{2}.{3}", clientAddress.bAddress[2], clientAddress.bAddress[3], clientAddress.bAddress[4], clientAddress.bAddress[5]);
    }

    return ipAddress;
}

public static string GetLocalIpAddressWhileRemote()
{
    var ipAddress = string.Empty;
    var processId = GetCurrentProcessId();
    var sessionId = 0;
    var hasFoundSessionId = ProcessIdToSessionId(processId, ref sessionId) != 0;
    if (hasFoundSessionId)
    {
        ipAddress = GetIpAddressFromSession(sessionId, IntPtr.Zero);
    }

    return ipAddress;
}

首先,获得客户端的processId并使用该客户端获取sessionId.使用sessionId,然后可以将WTSQuerySessionInformationWTS_INFO_CLASS.WTSClientAddress参数一起使用以获取会话IP地址.我还没有包括WTS_INFO_CLASS枚举,您仍然需要自己添加它才能使其正常工作.

First, you get the processId of your client and use that to get the sessionId. With the sessionId you can then use WTSQuerySessionInformation with the WTS_INFO_CLASS.WTSClientAddress parameter to get the session IP-address. I have not included the WTS_INFO_CLASS enum, you will still have to add this yourself for it to work.

这篇关于在远程会话中查找物理机的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆