Jump to content

How to get terminal services client IP address?


Rece
 Share

Recommended Posts

For Citrix TS Server Do this In VB someone will translate im sure hehe :P

Symptom

The administrator wants to generate a list of users and their client IP addresses.

Resolution

Save the following code to a file named users.vbs and run it with the command cscript users.vbs. It lists all users in the farm and their IP addresses:

Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")

objFarm.Initialize(1)

For Each objSession In objFarm.Sessions

WScript.Echo "User name : " & objSession.UserName

WScript.Echo "IP Address: " & objSession.ClientAddress

Next

Additional Information

Additional information about MFCOM objects is located at www.citrix.com/cdn.

you Can Go There To For Citrix(ICA), And RDP Too Here

And For RDP It A Little More Complicated :lmao:

4. Clay Keller 
 Aug 11 2004, 8:53 pm   show options 

Newsgroups: microsoft.public.windows.terminal_services 
From: "Clay Keller" <NO_claySPAM_kel...@hotmail.com> - Find messages by this author  
Date: Wed, 11 Aug 2004 20:53:03 -0500 
Local: Wed, Aug 11 2004 8:53 pm  
Subject: Re: Need to capture client IP address 
Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse  

Here is a little VB module that is a subset of functionality from a larger 
Terminal services module I wrote. 
This will do what you want. 


Attribute VB_Name = "RDPGetMachine" 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
'This Code Snippet taken from mdlTSAPI.bas written by Clay Keller 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


Option Explicit 


'Terminal Services API Declarations 
Private Declare Function WTSQuerySessionInformation Lib "wtsapi32.dll" _ 
      Alias "WTSQuerySessionInformationA" _ 
      (ByVal hServer As Long, _ 
      ByVal SessionID As Long, _ 
      ByVal WTSInfoClass As Long, _ 
      ByRef ppBuffer As Long, _ 
      ByRef pBytesReturned As Long _ 
      ) As Long 


Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" _ 
      (ByVal pMemory As Any) 


'Win32 Declarations 
Private Declare Sub CopyMemory Lib "kernel32.dll" _ 
      Alias "RtlMoveMemory" _ 
      (ByRef aDestination As Any, _ 
      ByVal lSource As Any, _ 
      ByVal lBytesToCopy As Long _ 
      ) 


'Terminal Services Constants and Definitions 
Public Enum WTS_INFO_CLASS 
    WTSInitialProgram 
    WTSApplicationName 
    WTSWorkingDirectory 
    WTSOEMId 
    WTSSessionId 
    WTSUserName 
    WTSWinStationName 
    WTSDomainName 
    WTSConnectState 
    WTSClientBuildNumber 
    WTSClientName 
    WTSClientDirectory 
    WTSClientProductId 
    WTSClientHardwareId 
    WtsClientAddress 
    WTSClientDisplay 
    WTSClientProtocolType 
End Enum 


Public Type WTS_CLIENT_ADDRESS 
    AddressFamily As Long      'DWORD This member can be AF_INET, AF_IPX, 
AF_NETBIOS, or AF_UNSPEC 
    Address(20) As Byte      ' BYTE Address[20] 
End Type 


Private Const WTS_CURRENT_SERVER_HANDLE = 0& 
Private Const WTS_CURRENT_SESSION = (-1) 


Private Function GetStringFromLP(ByVal StrPtr As Long) As String 
   Dim b As Byte 
   Dim tempStr As String 
   Dim bufferStr As String 
   Dim Done As Boolean 


   Done = False 
   Do 
      ' Get the byte/character that StrPtr is pointing to. 
      CopyMemory b, ByVal StrPtr, 1 
      If b = 0 Then                   ' If you've found a null character, 
then you're done. 
         Done = True 
      Else 
         tempStr = Chr$(b)             ' Get the character for the byte's 
value 
         bufferStr = bufferStr & tempStr 'Add it to the string 


         StrPtr = StrPtr + 1             ' Increment the pointer to next 
byte/char 
      End If 
   Loop Until Done 
   GetStringFromLP = bufferStr 
End Function 


Public Function GetMachinenameofCurrentSession() As String 


    Dim RetVal As Long        'Return Value of API Call 
    Dim lpBuffer As Long        'Buffer to Hold Info Returned 
    Dim Count As Long          'Length of Buffer info 
    Dim MachineName As String 


    'If the function succeeds, the return value is a nonzero value. 
    'If the function fails, the return value is zero. 
    'To get extended error information, call GetLastError API. 
    RetVal = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _ 
                                        WTS_CURRENT_SESSION, _ 
                                        WTSClientName, _ 
                                        lpBuffer, _ 
                                        Count) 


    MachineName = GetStringFromLP(lpBuffer) 
    WTSFreeMemory lpBuffer 'Free the memory used by the buffer. 
    GetMachinenameofCurrentSession = MachineName 


End Function 


Public Function GetIPofClientSession() As String 
  Dim lRet As Long 
  Dim Ret As Long 
  Dim wca_Tmp As WTS_CLIENT_ADDRESS 
  Dim pBytesReturned As Long 
  Dim ppBuffer As Long 
  Dim m_IpAddress As String 


  ' IP address information for a session is bogus unless the session 
  ' is a connected remote session. 


    lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _ 
                                      WTS_CURRENT_SESSION, _ 
                                      WtsClientAddress, _ 
                                      ppBuffer, _ 
                                      pBytesReturned) 


    If lRet Then 
      CopyMemory wca_Tmp, ppBuffer, pBytesReturned 
      WTSFreeMemory ppBuffer 
      m_IpAddress = Join(Array(wca_Tmp.Address(2), _ 
                               wca_Tmp.Address(3), _ 
                               wca_Tmp.Address(4), _ 
                               wca_Tmp.Address(5)), ".") 
    Else 
        m_IpAddress = vbNullString 
    End If 
    GetIPofClientSession = m_IpAddress 


End Function 


'End Module 



"Mike Pratt" <MikePr...@discussions.microsoft.com> wrote in message 


news:24F7ACE4-EAC7-426A-86B2-CF7271A97910@microsoft.com...

And look in the RDPClient for: Execute Script When Connect,Option. This script excute on the local client! not like logon script who will be excuted on the server it self at the userlevel. And this script will create a file in a dir like \\clientmachine\admin$\ipaddr.txt and you retreive this file to know the ip address! and if you have to see the external ip address of a VPN connected Client to the TS do same but get IP from

http://checkip.dyndns.org:8245/

Anyway I hope this help!

bye bye!

Edit: check this file too

Edited by Greenseed

GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.

Link to comment
Share on other sites

Thanks Greenseed! This is what I need (the second code because I've Win2000 terminal services not Citrix). I will try to build in this code into my AutoIt script. If somebody can help me it will probably succeed. I would like to bulid in everyhing into my program...

JS: I need the client's IP. I've written an initial program which starts when the client connects to the terminal server and shows a menu with icons (the programs which can be executed by the user) instead of the Win2000 desktop. I want this program to log the logins and logouts with the client's (static) IP address. So I need the IP of the computer from which the client connects to the server...

Link to comment
Share on other sites

@codeworm

where TS store is log?

because with a audit you can log user login and userlogoff but it will not help you getting the IP of present connected user!

Hmmm I could've sworn TS created a plain-text log somewhere under the system folder, but I don't see one on my servers. I guess you'll have to stick to checking the event logs.

I also assumed you could get the client IP using WMI, but I can't find any code in MSDN. I do see client properties, but it only provides a name at best, not the IP. Again, you could do an nslookup on the computer name if you have DNS set up.

My UDFs: ExitCodes

Link to comment
Share on other sites

The main reason why I want to log IPs is that clients can change their IPs. So storing PC names and IPs in a database is not the best solution. nslookup seems to be better (I've DNS). But Greenseed's code seems to be the best but I need to "translate" it to AutoIt script. And I think it's easier than reading from the event log.

Edited by Rece
Link to comment
Share on other sites

did you check the file with the solution i send! it the VB code compilated then you can use it in autoit without convert it!

Yes, but I would like to get only 1 EXE so I need to convert the VB code to AutoIt code... Edited by Rece
Link to comment
Share on other sites

Could somebody translate this into AutoIt script?

4. Clay Keller 
 Aug 11 2004, 8:53 pm   show options 

Newsgroups: microsoft.public.windows.terminal_services 
From: "Clay Keller" <NO_claySPAM_kel...@hotmail.com> - Find messages by this author  
Date: Wed, 11 Aug 2004 20:53:03 -0500 
Local: Wed, Aug 11 2004 8:53 pm  
Subject: Re: Need to capture client IP address 
Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse  

Here is a little VB module that is a subset of functionality from a larger 
Terminal services module I wrote. 
This will do what you want. 
Attribute VB_Name = "RDPGetMachine" 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
'This Code Snippet taken from mdlTSAPI.bas written by Clay Keller 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Option Explicit 
'Terminal Services API Declarations 
Private Declare Function WTSQuerySessionInformation Lib "wtsapi32.dll" _ 
      Alias "WTSQuerySessionInformationA" _ 
      (ByVal hServer As Long, _ 
      ByVal SessionID As Long, _ 
      ByVal WTSInfoClass As Long, _ 
      ByRef ppBuffer As Long, _ 
      ByRef pBytesReturned As Long _ 
      ) As Long 
Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" _ 
      (ByVal pMemory As Any) 
'Win32 Declarations 
Private Declare Sub CopyMemory Lib "kernel32.dll" _ 
      Alias "RtlMoveMemory" _ 
      (ByRef aDestination As Any, _ 
      ByVal lSource As Any, _ 
      ByVal lBytesToCopy As Long _ 
      ) 
'Terminal Services Constants and Definitions 
Public Enum WTS_INFO_CLASS 
    WTSInitialProgram 
    WTSApplicationName 
    WTSWorkingDirectory 
    WTSOEMId 
    WTSSessionId 
    WTSUserName 
    WTSWinStationName 
    WTSDomainName 
    WTSConnectState 
    WTSClientBuildNumber 
    WTSClientName 
    WTSClientDirectory 
    WTSClientProductId 
    WTSClientHardwareId 
    WtsClientAddress 
    WTSClientDisplay 
    WTSClientProtocolType 
End Enum 
Public Type WTS_CLIENT_ADDRESS 
    AddressFamily As Long      'DWORD This member can be AF_INET, AF_IPX, 
AF_NETBIOS, or AF_UNSPEC 
    Address(20) As Byte      ' BYTE Address[20] 
End Type 
Private Const WTS_CURRENT_SERVER_HANDLE = 0& 
Private Const WTS_CURRENT_SESSION = (-1) 
Private Function GetStringFromLP(ByVal StrPtr As Long) As String 
   Dim b As Byte 
   Dim tempStr As String 
   Dim bufferStr As String 
   Dim Done As Boolean 
   Done = False 
   Do 
      ' Get the byte/character that StrPtr is pointing to. 
      CopyMemory b, ByVal StrPtr, 1 
      If b = 0 Then                   ' If you've found a null character, 
then you're done. 
         Done = True 
      Else 
         tempStr = Chr$(b)             ' Get the character for the byte's 
value 
         bufferStr = bufferStr & tempStr 'Add it to the string 
         StrPtr = StrPtr + 1             ' Increment the pointer to next 
byte/char 
      End If 
   Loop Until Done 
   GetStringFromLP = bufferStr 
End Function 
Public Function GetMachinenameofCurrentSession() As String 
    Dim RetVal As Long        'Return Value of API Call 
    Dim lpBuffer As Long        'Buffer to Hold Info Returned 
    Dim Count As Long          'Length of Buffer info 
    Dim MachineName As String 
    'If the function succeeds, the return value is a nonzero value. 
    'If the function fails, the return value is zero. 
    'To get extended error information, call GetLastError API. 
    RetVal = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _ 
                                        WTS_CURRENT_SESSION, _ 
                                        WTSClientName, _ 
                                        lpBuffer, _ 
                                        Count) 
    MachineName = GetStringFromLP(lpBuffer) 
    WTSFreeMemory lpBuffer 'Free the memory used by the buffer. 
    GetMachinenameofCurrentSession = MachineName 
End Function 
Public Function GetIPofClientSession() As String 
  Dim lRet As Long 
  Dim Ret As Long 
  Dim wca_Tmp As WTS_CLIENT_ADDRESS 
  Dim pBytesReturned As Long 
  Dim ppBuffer As Long 
  Dim m_IpAddress As String 
  ' IP address information for a session is bogus unless the session 
  ' is a connected remote session. 
    lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _ 
                                      WTS_CURRENT_SESSION, _ 
                                      WtsClientAddress, _ 
                                      ppBuffer, _ 
                                      pBytesReturned) 
    If lRet Then 
      CopyMemory wca_Tmp, ppBuffer, pBytesReturned 
      WTSFreeMemory ppBuffer 
      m_IpAddress = Join(Array(wca_Tmp.Address(2), _ 
                               wca_Tmp.Address(3), _ 
                               wca_Tmp.Address(4), _ 
                               wca_Tmp.Address(5)), ".") 
    Else 
        m_IpAddress = vbNullString 
    End If 
    GetIPofClientSession = m_IpAddress 
End Function 
'End Module 
"Mike Pratt" <MikePr...@discussions.microsoft.com> wrote in message 
news:24F7ACE4-EAC7-426A-86B2-CF7271A97910@microsoft.com...
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...

I would like to write an initial program for my Win2000 terminal server. I need the client IP for logging but I don't know how to get it. I think wtsapi32.dll's WTSQuerySessionInformation can give this information for me but I don't understand exactly the usage of DllCall() in this case... Could somebody help me?

I know this is a reeaaaaaaal old post by now, but if you still need... You don't have to hack your way through everything... Terminal Services can be set to audit/log IP addresses. Check your Event Viewer and your auditing settings.

Link to comment
Share on other sites

  • 2 years later...

Found this while working on my own solution. If you are looking to find How to get the Client IP address or hostname of a Citrix session or Terminal Services Session?

then check out citrix developer network site. The site actually list 4 ways to do it with Citrix and TS API.

http://community.citrix.com/x/-INMAQ

http://community.citrix.com/pages/viewpage...pageId=21791740

thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...