Active IQ Unified Manager Discussions

WFA Function to Reverse DNS Lookup

coreywanless
6,209 Views

I have an need to be able to mount an NFS Datastore into VMWare using its DNS Name. 

 

I'm not familiar with MVEL, and there isn't much material that I can find on it.

 

Can someone assist in creating a function that returns back the DNS name when it is passed an IP address? 

 

I want to do this so that I don't have to customize the certified Mount Datastore command, and it will assist down the line with return parameters.

1 ACCEPTED SOLUTION

sinhaa
6,040 Views

WFA MVEL fucntions are java functions. You can do anything with a Java code, you can create a function and do it. For the Reverse DNS lookup, the following function will do.

===

function reverseDns(ip)
{
import java.net.*;

InetAddress add = InetAddress.getByName(ip);
String hostfqdn = add.getCanonicalHostName();
return hostfqdn;


}

===

 

Now you can use it in your workflow and get the DNS names.

 

 

 

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

View solution in original post

6 REPLIES 6

mbeattie
6,074 Views

Hi Corey,

 

I would recommend you do this using the "System.Net.Dns" .NET class within your WFA Command using PowerShell (IE your command would accept an IP Address as an input parameter but will perform a DNS lookup of the hostname then mount the datastore using the hostname). You could do it one line of PowerShell

 

> [String]$ipAddress = "192.168.100.10"
> [String]$hostName = [System.Net.Dns]::GetHostEntry($ipAddress).HostName

> $hostName
> TESTWFA1.testlab.local

 

Or...you could create a PowerShell function to include in your WFA Command. EG:

 

#'------------------------------------------------------------------------------
Function Get-DNSHostName{
    [CmdletBinding()]
    Param(
    [Parameter(Position=0,
       Mandatory=$True,
       ValueFromPipeLine=$True,
       ValueFromPipeLineByPropertyName=$True)]
       [String]$IPAddress
    )
    Try{
       [String]$hostName = [System.Net.Dns]::GetHostEntry($ipAddress).HostName
    }Catch{
       [String]$hostName = ""
    }
    Return $hostName;
}#End Function
#'------------------------------------------------------------------------------
[String]$ipAddress = "192.168.100.10"
[String]$hostName = Get-DNSHostName -IPAddress $ipAddress
Write-Host $hostName

 

Note: This assumes you have configured your A & PTR records in DNS.

 

Hope that helps

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

coreywanless
6,058 Views

Thanks Matt for the reply. I have used a similar method in non-certified commands in WFA. What I am trying to avoid is cloning the "certified" command, and making the modifications to it. 

 

In my example I'm wanting to use the certified command "Create VMware NFS Datastore".  So what I was looking to do was use the function capability inside WFA (not powershell functions), and feed the ip address to the function outside of the command to feed to the certified command.

sinhaa
6,041 Views

WFA MVEL fucntions are java functions. You can do anything with a Java code, you can create a function and do it. For the Reverse DNS lookup, the following function will do.

===

function reverseDns(ip)
{
import java.net.*;

InetAddress add = InetAddress.getByName(ip);
String hostfqdn = add.getCanonicalHostName();
return hostfqdn;


}

===

 

Now you can use it in your workflow and get the DNS names.

 

 

 

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

coreywanless
5,998 Views

That's exactly what I'm looking for!

 

One day I will learn java syntax. 🙂

TiMatrix
4,374 Views

Hi sinhaa,

 

this is very helpful and sorry for hi-jacking this thread.

But I have the issue, when no entry was found in DNS, an error is thrown.

Since I'm not very familiar with MVEL, how can I achieve it, that a NULL value or a message like 'No DNS entry found!' will be returned?

 

Thanks,

Tino

sinhaa
4,322 Views

@TiMatrix

 

I can't put try/catch blocks in WFA functions. Try the below function.

 

Its taken care of all 3 cases.

 

1. IP not pingable. 

2. IP Pingable but, DNS entry not found.

3. Found DNS entry, return the FQDN.

 

 

function reverseDns(ip)
{
import java.net.*;
InetAddress add = InetAddress.getByName(ip);

if(add.isReachable(10000))
{
String hostfqdn = add.getCanonicalHostName();

if (add.getHostAddress().contentEquals(hostfqdn))
{
	return 'IP pingable but DNS entry NOT found';
}
else
{
return hostfqdn;
}
}

else
{
	return 'Host Not Reachable';
}

}

 

sinhaa

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Public