windows 如何获取远程桌面客户端的IP地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1510093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:47:37  来源:igfitidea点击:

How to get the IP Address of the Remote Desktop Client?

windowsloggingscriptingremote-desktoprdp

提问by BlueGene

I'm trying to write a script to log the IP address of the Windows client from which the user initiated Remote Desktop to log in to the Windows Server. How to capture the IP address of the client in the Server?

我正在尝试编写一个脚本来记录用户启动远程桌面以登录 Windows 服务器的 Windows 客户端的 IP 地址。如何在Server中捕获客户端的IP地址?

采纳答案by Dewfy

So, you ignore proxy...

所以,你忽略代理...

  • using environment var: CLIENTNAME in domain you can resolve it back to IP
  • 使用环境变量:域中的 CLIENTNAME 您可以将其解析回 IP

without domain controller:

没有域控制器:

  • using WMI script you can get to Event Log, source: Security, look for category Logon/Logoff where username = environment variable USERNAME
  • 使用 WMI 脚本,您可以访问事件日志,来源:安全,查找类别登录/注销,其中用户名 = 环境变量 USERNAME

回答by Dan Ports

If you are using PowerShell or a .NET language, the trunk version of the Cassia librarysupports this -- just grab the latest build from the build server(login as a guest and use the artifacts link). To print the remote addresses of all sessions on the local server, you might use something like the following:

如果您使用 PowerShell 或 .NET 语言,Cassia 库的主干版本支持这一点——只需从构建服务器获取最新构建(以访客身份登录并使用工件链接)。要打印本地服务器上所有会话的远程地址,您可以使用以下内容:

ITerminalServicesManager manager = new TerminalServicesManager();
foreach (ITerminalServicesSession session in manager.GetLocalServer().GetSessions())
{
    IPEndPoint ipEndPoint = session.RemoteEndPoint as IPEndPoint;
    if (ipEndPoint != null)
    {
        Console.WriteLine(ipEndPoint.Address);
    }
}

回答by Remko

If you want to use "pure" Powershell 2.0:

如果您想使用“纯”Powershell 2.0:

$Wtsapi32 = @'
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Wtsapi32 {

    public enum WTS_INFO_CLASS
    {
        WTSInitialProgram,
        WTSApplicationName,
        WTSWorkingDirectory,
        WTSOEMId,
        WTSSessionId,
        WTSUserName,
        WTSWinStationName,
        WTSDomainName,
        WTSConnectState,
        WTSClientBuildNumber,
        WTSClientName,
        WTSClientDirectory,
        WTSClientProductId,
        WTSClientHardwareId,
        WTSClientAddress,
        WTSClientDisplay,
        WTSClientProtocolType
    };  

    [StructLayout(LayoutKind.Sequential)]
    public struct WTS_CLIENT_ADDRESS
    {
        public uint AddressFamily;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public byte[] Address;
    }

    public class PS {

        public const IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
        public const int WTS_CURRENT_SESSION = -1;

        [DllImport("wtsapi32.dll",  EntryPoint="WTSQuerySessionInformation")]
        public static extern bool WTSQuerySessionInformation(
            System.IntPtr hServer, 
            int sessionId, 
            WTS_INFO_CLASS wtsInfoClass, 
            out System.IntPtr ppBuffer, 
            out uint pBytesReturned);

        [DllImport("wtsapi32.dll",  EntryPoint="WTSFreeMemory")]
        public static extern void WTSFreeMemory(
            IntPtr memory);         
    }
}
'@

Add-Type -TypeDefinition $Wtsapi32