Virtual Servers are accessed by user friendly name. In some cases you will want to know which Virtual Servers are using a given address. This script takes as input a wildcard spec for the address:port of a virtual server and lists the address:port and name for all matches.

Usage

The arguments for this application are the address, username, password of the BIG-IP as well as an optional wildcard spec for the address:port of the requested Virtual Server.  This is declared in the top of the script with the following param statement.  There is also a Write-Usage function to display the arguments to the user.

param (
  $g_bigip = $null,
  $g_uid = $null,
  $g_pwd = $null,
  $g_virtualaddr = $null
);

Set-PSDebug -strict;

function Write-Usage()
{
  Write-Host "Usage: VirtualServerLookup.ps1 host uid pwd [virtual_server]";
  exit;
}

Initialization

As is with all of my PowerShell scripts, the initialization component will look to see if the iControlSnapIn is loaded into the current PowerShell session.  If not, the Add-PSSnapIn Cmdlet is called to add the snapin into the runtime.  Then a call to the Initialize-F5.iControl cmdlet to setup the connection to the BIG-IP.  If this succeeds, then a call to the Lookup-Virtual function is called to lookup the virtual servers containing the requested address:port combination.

function Do-Initialize()
{
  if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
  {
    Add-PSSnapIn iControlSnapIn
  }
  $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;
  
  return $success;
}

if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) )
{
  Write-Usage;
}

if ( Do-Initialize )
{
  if ( $g_virtualaddr )
  {
    Lookup-Virtual $g_virtualaddr;
  }
  else
  {
    Lookup-Virtual;
  }
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}

Querying Virtual Server names and destinations

The Lookup-Virtual function takes an optional argument containing the virtual server address:port query.  If a query is specified, it will attempt to be split apart with a colon separator.

Once the query is disassembled, the Virtual Server List is retrieved as well as the associated destination addresses.  For each of the destination addresses of the virtual servers, a comparison is done to determine if the input query is a match.  If no query is supplied, the entry is displayed.

The Write-VSEntry then is called to print a spaced formatted output for the results.

function Lookup-Virtual()
{
  param([string]$query = $null);

  $addr = "*";
  $port = "*";

  if ( $query )
  {
    $addr = $query;
    $tokens = $query.Split(':');
    if ( $tokens.Length -eq 2 )
    {
      $addr = $tokens[0];
      $port = $tokens[1];
    }
    if ( !$addr ) { $addr = "*"; }
    if ( !$port ) { $port = "*"; }
  }

  $vs_list = (Get-F5.iControl).LocalLBVirtualServer.get_list();
  $dest_list = (Get-F5.iControl).LocalLBVirtualServer.get_destination($vs_list);

  for($i=0; $i-lt$dest_list.Length; $i++)
  {
    $vs = $vs_list[$i];
    $daddr = $dest_list[$i].address;
    $dport = $dest_list[$i].port;

    if ( !($query) -or ("${daddr}:${dport}" -like "${addr}:${port}") )
    {
      Write-VSEntry -vs $vs -address $daddr -port $dport;
    }
  }
}
function Write-VSEntry()
{
  param(
    [string]$vs = $null,
    [string]$address = $null,
    [int]$port = $null
  );
  if ( $vs -and $address -and $port )
  {
    "{0,16}:{1,-5} -> {2}" -f ${address}, ${port}, $vs;
  }
}

Running The Code

Running the code with no query parameter will print out all the Virtual Servers

PS> .\VirtualServerLookup.ps1 bigip user pass
   172.27.11.116:80    -> dc-llix-web
   172.27.11.101:80    -> xpbert-http
   172.27.11.101:443   -> xpbert-https
   172.27.11.101:22    -> xpbert-ssh
   172.27.11.101:69    -> tftp_server_vip
   172.27.11.115:80    -> dc-sea-web
PS> .\VirtualServerLookup.ps1 bigip user pass 172.27.11.101:*"
   172.27.11.101:80    -> xpbert-http
   172.27.11.101:443   -> xpbert-https
   172.27.11.101:22    -> xpbert-ssh
   172.27.11.101:69    -> tftp_server_vip
PS> .\VirtualServerLookup.ps1 bigip user pass *:80
   172.27.11.116:80    -> dc-llix-web
   172.27.11.101:80    -> xpbert-http
   172.27.11.115:80    -> dc-sea-web
PS> .\VirtualServerLookup.ps1 bigip user pass *.11.*:8*
   172.27.11.116:80    -> dc-llix-web
   172.27.11.101:80    -> xpbert-http
   172.27.11.115:80    -> dc-sea-web

Conclusion

This script shows the steps on how to do a reverse wildcard lookup for virtual server names that have a destination address matching a specified search criteria.  Similar logic could be performed to do reverse lookups for pool members or node addresses.

You can download the entire script from the DevCentral Codeshare under PsVirtualServerLookup.

 

Get the Flash Player to see this player.