Greenseed Posted December 3, 2005 Posted December 3, 2005 (edited) For Citrix TS Server Do this In VB someone will translate im sure hehe 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 HereAnd For RDP It A Little More Complicated expandcollapse popup4. 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 fromhttp://checkip.dyndns.org:8245/Anyway I hope this help!bye bye!Edit: check this file too Edited December 3, 2005 by Greenseed GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.
Rece Posted December 5, 2005 Author Posted December 5, 2005 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...
Gigglestick Posted December 5, 2005 Posted December 5, 2005 1. Terminal Services logs logins and logouts already. Have your program scour the event logs or TS's file log. 2. If all your clients have static IP's, put them in a database and use the %clientname% evironment variable to lookup the IP from the database. My UDFs: ExitCodes
Gigglestick Posted December 5, 2005 Posted December 5, 2005 You could also do a DNS lookup of the %clientname% environment variable if you have DNS set up. My UDFs: ExitCodes
Greenseed Posted December 5, 2005 Posted December 5, 2005 @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! GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.
Gigglestick Posted December 5, 2005 Posted December 5, 2005 @codewormwhere 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
Rece Posted December 6, 2005 Author Posted December 6, 2005 (edited) 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 December 6, 2005 by Rece
Greenseed Posted December 6, 2005 Posted December 6, 2005 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! GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.
Rece Posted December 7, 2005 Author Posted December 7, 2005 (edited) 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 December 7, 2005 by Rece
Rece Posted December 14, 2005 Author Posted December 14, 2005 Could somebody translate this into AutoIt script?expandcollapse popup4. 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...
Rece Posted January 1, 2006 Author Posted January 1, 2006 So I think I need to DllCall() the wtsapi32.dll, and query WTSQuerySessionInformation and then WTSClientAddress gives me the client IP...But I really don't know how to do it. I can't translate the VB code... References:WTSQuerySessionInformationWTS_Client_Address
JonathanChan Posted February 16, 2006 Posted February 16, 2006 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.
CitrixCDN Posted March 18, 2008 Posted March 18, 2008 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/-INMAQhttp://community.citrix.com/pages/viewpage...pageId=21791740thanks
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now