07 Mar 2008 04:38 PM EST
[ Tags: mfcom,  ico,  wfapi,  client ip ]

Have you ever tried to get the Client IP address for a Citrix or terminal services session and got stuck? I have had several emails asking me on how to do it. Well I thought I might just blog about it. I can think four ways to do it and if you can add to the list then please do via comments. So let's get to the business right away.

1. The simplest way is to use the MFCOM API to get a list of session and enumerate each session to get username and the Client IP address. Here is a code snippet.

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

You need to be an admin on XenApp (Formerly known as Presentation Sever) Farm to run  MFCOM queries. You can read more and download the MFCOM example here.

2. Use WFAPI SDK WFEnumerateSessions method to get a list of all the sessions on a server and then use WFQuerySessionInformation to extract Client IP for each session on the server I have written a Sample program on how to do it. Follow this link to download WFAPI and Csharp .Net program which enumerates all sessions on a server and their Client IP address.

3. Use native terminal Services API. Similar to WFAPI use TSEnumerateSessions to get a list of sessions on a server and then use TSQuerySessionInformation to extract Client IP address each session on the server.  To read more follow this link to download Terminal Services API sample example written in Csharp .Net.

4. You can also use ICO SDK and  GetClientAddress API to get client IP within a ICO session The ICA Client Object is the framework that exposes the functionality of the Citrix  ICA Win32 Client to third party applications.   The ICA Client Object (ICO) SDK enables developers and administrators to modify the behavior and appearance of a Windows 32-bit Citrix ICA client. The SDK is a series of documents that detail   available application programming interface (API) in the Citrix ICA client Follow this link to download an ICO example which illustrates the use of methods and properties available to get client network name and IP address information using Citrix ICO SDK.

Code snippet for ICO in JavaScript

function GetClientNetworkName(form)
    {
        form.netname.value = document.ICO1.GetClientNetworkName()
    }
    function GetAddrCnt(form)
    {
        form.addrcnt.value = document.ICO1.GetClientAddressCount()
    }
    function GetAddr(form)
    {
        form.addr.value = document.ICO1.GetClientAddress(0)
    }
Permalink | Comments (24) |

Incredibly useful blog. I was looking for a way to do it via WFAPI and .Net but could not find any examples.

Thanks

Mark S.
ILogic Systems.

Posted by Anonymous at Mar 10, 2008 10:09 | Reply To This

Perhaps an another way. Powershell

#Powershell
$objFarm = new-object -com "MetaFrameCOM.MetaFrameFarm"
$objFarm.Initialize(1)
$objFarm.Sessions | ft UserName,ClientAddress

Posted by Anonymous at Mar 10, 2008 10:48 | Reply To This

Hi Brandon /Powershell guru

Thanks for your comment.

I ran into this problem a while back so I used this method here, http://www.adminscripteditor.com/forum/tm.asp?m=9066&mpage=1&key=&#13239

Posted by Anonymous at Mar 17, 2008 05:34 | Reply To This

Hi there

Thanks for mentioning adminscriteditor site. I posted the link to my blog on the forum.

Thanks

Posted by Anonymous at Mar 17, 2008 20:22 | Reply To This

Hi I came across your post via MSDN forums. This post had much better content then MSDN forums.Thanks

Posted by Anonymous at Mar 18, 2008 09:39 | Reply To This

Hi there

You may have a look at http://ibgrueninger.de/blog/index.php/2008/03/21/sessioninfos/.

I uploaded a tool which I use since several years.

Andreas GrĂ¼ninger

Posted by Anonymous at Mar 21, 2008 11:51 | Reply To This

Hi Andreas
Thanks for sharing this info on this great tool. Is the source code available for the tool or just the binary?

Hi Vishal

The source code is based on the shadow example of the SDK and will be available in the next time.

But first I have to check the blog installation and to publish some other tools. Stay tuned.

 Andreas

I just remarked the subroutine that detects the client ip address in my vb-loginscirpt. Using MFCOM can take up to 50 seconds for the process detecting the client ip address. I'm looking for a consistantly faster process doing that.

Hi Torsten
I believe WFAPI will be give you results much faster. MFCOM doesn't well with scale with very large farms

Hi
I m looking for a bit of c# code that allows me to retrieve the current user information.
Especially his IP and Name.
Do you have any idea where can I find this method ?

Regards,

Posted by Anonymous at Mar 30, 2008 15:10 | Reply To This

Hi I am looking for JAVA code to retrieve the ip address and user information of the client.

Posted by Anonymous at Aug 08, 2008 13:02 | Reply To This

Are you looking for Java solution with MFCOM?

I just want to retrive the user info and have to use it on server side code.It doesn't matter whether it it is MFCOM or any other API.I am very new to citrix.

Posted by Anonymous at Aug 08, 2008 14:44 | Reply To This

OK I will update the article with Java sample over the weekend.
Vishal

thanks..........and it would be great if you can mail me the article on anup.anupranjan@gmail.com

Posted by Anonymous at Aug 08, 2008 14:49 | Reply To This

Hi Vishal,

Could you please provide me the article for JAVA sample.

Regards,

Posted by Anonymous at Aug 11, 2008 15:56 | Reply To This

Hi Vishal,

The title of this post indicates that it will explain how to get the IP address AND hostname of the client. Unfortunately, the code given can only get the IP address. I know the hostname is *sometimes* present in the Session.ClientName field, but not always (for example, ClientName contains random garbage when using the Web Interface client). What is a reliable way to get the client's hostname?

Thanks,
Daniel

Posted by Anonymous at Sep 10, 2008 17:13 | Reply To This

Hi Daniel

(MFCOM) session.clientname should return a client name property using web interface if configured properly. See following articles

http://support.citrix.com/article/CTX113227

http://www.jaytomlin.com/blog/2006/07/clientname_issue_resolved_1.html

http://support.citrix.com/article/CTX107112

Hi Vishal,

Thanks for those links, but I'm still a bit confused. The 1st and 3rd links indicate that we will be breaking some functionality of the Web Interface if we configure things so that ClientName returns the hostname, so this is not good.

The 2nd link indicates that everything will be automatically fixed in a "new" version of Presentation Server to be released in 2006. I recently tested with the latest CPS 4.5, and ClientName was still returning the WI_randomstuff string by default. Did Jay's proposed fix never actually make it into CPS?

Regards,
Daniel

Posted by Anonymous at Sep 12, 2008 13:47 | Reply To This

I use this site to [find your ip address|http://ipfinding.com] quick and easy.

Posted by Anonymous at Sep 30, 2008 01:19 | Reply To This

I use this site to find your ip address quick and easy.

Posted by Anonymous at Sep 30, 2008 01:21 | Reply To This