Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get fully qualified hostname using Ant

Tags:

ant

I am using Ant as our setup script for our server, and we need to get the fully qualified name of our server. How can I get it using Ant, or is is possible in Ant?

The fully qualified hostname is like: xxx.company.com

like image 917
performanceuser Avatar asked Dec 11 '22 18:12

performanceuser


2 Answers

<exec executable="hostname" outputproperty="computer.hostname"/>

will work on linux and windows, otherwise use the groovy solution from Marc O'Connor
Also nslookup will work on linux and windows, if you need the fullhostname you have to parse for the entry after Name: in nslookup ServerName output, use :

<groovy>
properties.'hostname' = "hostname".execute().text
//or
properties.'hostname' = InetAddress.getLocalHost().getHostName()

properties.'hostnamefull' = "nslookup ${"hostname".execute().text}".execute().text.find(/Name:\s(.+)/).split(/:\s+/)[1]
</groovy>

<echo>
$${hostname} => ${hostname}
$${hostnamefull} => ${hostnamefull}
</echo>
like image 136
Rebse Avatar answered Mar 12 '23 13:03

Rebse


There is an Ant task called HostInfo that you can use to set properties containing the host name and domain of the current machine.

Alternatively, if you're running on Linux/Unix you could just call out to the hostname command:

<exec executable="hostname" outputproperty="myhostname">
  <arg line="-f"/>
</exec>

The fully-qualified host name is then available in ${myhostname}.

EDIT: For a fully platform-independent solution, a custom task something like this (untested) should do the job:

public class GetHost extends Task
{
    private String property;

    public void setProperty(String property)
    {
        this.property = property;
    }

    @Override
    public void execute() throws BuildException
    {
        if (property == null || property.length() == 0)
        {
            throw new BuildException("Property name must be specified.");
        }
        else
        {
            try
            {
                String hostName = InetAddress.getLocalHost().getHostName();
                getProject().setProperty(property, hostName);
            }
            catch (IOException ex)
            {
                throw new BuildException(ex);
            }
        }
    }
}

This can be used as follows:

<GetHost property="myhostname" />
like image 36
Dan Dyer Avatar answered Mar 12 '23 13:03

Dan Dyer