Create a Virtual IP Range, Bind a Citrix Presentation Server to a Virtual IP Range, and Add a Process to a Virtual IP List

Added by Citrix CDN , last edited by Vishal Ganeriwala on Aug 18, 2008  (view change)
Tags: 

Description

The following examples allow you to:

  • Create a virtual IP range
  • Bind a Citrix Presentation Server to a virtual IP range
  • Add a process to a virtual IP list

Script Version

1.0

cdn:Download

Code Snippet

Creating a Virtual IP Range

To create a new virtual IP Range this C# application console script requires three command line arguments:

  • The starting IP address of the virtual IP range
  • The ending IP address
  • The subnet mask

The script checks for the preexistence of the virtual IP range in the farm. The script then uses a try and catch statement to add the IP range and subnet mask to the farm using the IMetaFrameVIPRange object.

using System;
using MetaFrameCOM;
using System.Reflection;

namespace VirtualIP
{
    class VIP
    {
        [STAThread]
              static void Main(string[] args)
              {
        if (args.Length == 3)
              {
                  string startaddress = args\[0\];
                        string endaddress = args\[1\];
                        string subaddress = args\[2\];
                        //check if VIP range exist and don't try to create
                        if (CkRange(startaddress, endaddress, subaddress))
                        {
              try
                               {
                                  IPRange(startaddress, endaddress, subaddress)
                               }
                               catch
                               {
                                  Console.WriteLine ("Invalid Parameter");
                               }
                        }
                        else
                        {
                             Console.WriteLine ("VIP Range Exists");
                        }
            }
              else
               {
                  Console.WriteLine ("Invalid number of parameters");
Console.WriteLine("Usage:String:StartAddress,
String:EndAddress, String SubAddress");
Console.WriteLine("Ex:VIPCREATE169.8.9.1-169.8.9.100 255.255.255.0");
                     }

                     Console.WriteLine ();
                     Console.WriteLine ("======================");
                     Console.WriteLine ("VIP Ranges ");
                     Console.WriteLine ("======================");
                     displayVIPRange();
          }

private static void IPRange(string baddr, string eaddr, string saddr)
{
    string beginAddress = baddr;
    string endAddress = eaddr;
    string subAddress = saddr;
    IMetaFrameVIPRange VIPRange = new MetaFrameVIPRangeClass();
    VIPRange.StartAddress = beginAddress;
    VIPRange.EndAddress = endAddress;
    VIPRange.SubnetMask = subAddress;
    VIPRange.SaveData ();
}

private static void displayVIPRange()
{
    MetaFrameFarm Farm;
    Farm = new MetaFrameFarm();
    Farm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject);
    int i = 0;
    foreach (IMetaFrameVIPRange VIPRange in Farm.VIPRanges )

            i++;
            VIPRange.LoadData (1);
            Console.WriteLine ("("+i+")
"+VIPRange.StartAddress+"- "+VIPRange.EndAddress+" | "+VIPRange.SubnetMask );
     }
}

private static bool CkRange(string baddr, string eaddr, string saddr)
{
          string beginAddress = baddr;
          string endAddress = eaddr;
          string subAddress = saddr;
          MetaFrameFarm Farm;
          Farm = new MetaFrameFarm();
                     Farm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject);
          bool retValue = true;
          foreach (IMetaFrameVIPRange VIPRange in Farm.VIPRanges )
         {
             VIPRange.LoadData (1);
 if ((VIPRange.StartAddress == beginAddress) && (VIPRange.EndAddress == endAddress))
              {
Console.WriteLine (VIPRange.StartAddress+"-"+VIPRange.EndAddress+" | "+VIPRange.SubnetMask );
// values match and VIP Range already exists in the farm
retValue = false;
                    }
          }
          return retValue;
}

}
}

Binding Citrix Presentation Server 4.0 to a Virtual IP Range

To bind a server to a virtual IP range this C# application console script requires one command line parameter for the server name. The script will add the server supplied to every virtual IP range in the farm using the IMetaFrameVIPRange:AddServerByVariant method. IMetaFrameVIPRange:AddServerByVariant requires Server to be a reference object. AddServerByVariant method can assign one server, as this example demonstrates, or a list of servers. The second parameter indicates the number of servers being added to the Virtual IP range.
Snippet

static void Main(string[] args)
{
   if (args.Length == 1)
          {
               string ServerName = args[0];
               AddVIPServer(ServerName);
          }
          else
          {
               Console.WriteLine ("Usage: VIPAddServer <ServerName>");
          }
}

private static void AddVIPServer(string ServerName)
{
          MetaFrameFarm Farm;
          Farm = new MetaFrameFarm();
          Farm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject);
          int i = 0;
          foreach (IMetaFrameVIPRange VIPRange in Farm.VIPRanges )
          {
                     foreach (IMetaFrameServer5 server in Farm.Servers)
                     {
                                if (ServerName.ToUpper() == server.ServerName)
                                {
                                        object serverObj = server;
                                          i++;
                                          VIPRange.LoadData (1);
                                          VIPRange.AddServerByVariant (ref serverObj, 1);
                                          VIPRange.SaveData ();
                                 }
                     }
          }

}

Add a Process to a Virtual IP List

All processes must be added to the IMetaFrameFarm:VIPProcesslist in order to take advantage of the functions of virtual IP. This C# application console script stores the current IMetaFrameFarm:VIPProcessList in the object array VIPProcessesArray. Then it appends the command line supplied new process name to the end of the array and sets the IMetaFrameProcessList with all of the Virtual IP processes.
Snippet

static void Main(string\[\] args)
{
if (args.Length == 1)
{
                     string ProcessName= args\[0\];
                     AddVIPProcess(ProcessName);
           }
           else
           {
                     Console.WriteLine ("Usage: VIPLoopback <ProcessName>");
           }
}

private static void AddVIPProcess(string ProcessName)
{
MetaFrameFarm Farm;
Farm = new MetaFrameFarm();
          Farm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject);
          // Get all VIP Processes store them in the object array
          // VIPProcessesArray.
         int i=0;
object\[\] VIPProcessesArray = new object\[Farm.VIPProcessList.Length+1\];
         foreach (object VIPProcess in Farm.VIPProcessList)
          {
                     VIPProcessesArray\[i\] = (object) VIPProcess;
                     i++;{color}
          }
          // Assign the new Process name to the end
          // of the VIPProcessesArray.

          VIPProcessesArray\[i\] = (object) ProcessName;

          // Set the Farm's VIP Process list with
          // the original processes plus the new one.

          Farm.VIPProcessList = VIPProcessesArray;
}

}

Disclaimer

These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.