如何使用Ant获取完全限定的主机名

问题描述 投票:4回答:6

我使用Ant作为服务器的安装脚本,我们需要获取服务器的完全限定名称。如何使用Ant获取它,或者在Ant中是否可以?

完全限定的主机名如下:xxx.company.com

ant
6个回答
7
投票
<exec executable="hostname" outputproperty="computer.hostname"/>

将在Linux和Windows上工作,否则使用Marc O'Connor的groovy解决方案 nslookup也可以在linux和windows上运行,如果你需要在名称之后解析条目的fullhostname:在nslookup ServerName输出中,使用:

<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>

3
投票

有一个名为HostInfo的Ant任务,您可以使用它来设置包含当前计算机的主机名和域的属性。

或者,如果你在Linux / Unix上运行,你可以调用hostname命令:

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

然后在${myhostname}中提供完全限定的主机名。

编辑:对于完全独立于平台的解决方案,像这样(未经测试)的自定义任务应该完成这项工作:

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);
            }
        }
    }
}

这可以使用如下:

<GetHost property="myhostname" />

2
投票

重新回答我为Maven做的答案:-)

使用嵌入式groovy脚本执行Java主机名查找:

       <groovy>
            properties["hostname"] = InetAddress.getLocalHost().getHostName()
        </groovy>

Example

项目是自我记录的:

$ ant -p
Buildfile: /home/mark/tmp/build.xml

    This is a demo project answering the followng stackoverflow question:

        https://stackoverflow.com/questions/14653733

    First install 3rd party dependencies

        ant bootstrap 

    Then run the build

        ant

    Expect the following output

        print-hostname:
            [echo] Hostname: ?????

build.xml文件

<project name="demo" default="print-hostname">

    <description>
    This is a demo project answering the followng stackoverflow question:

        https://stackoverflow.com/questions/14653733

    First install 3rd party dependencies

        ant bootstrap 

    Then run the build

        ant

    Expect the following output

        print-hostname:
            [echo] Hostname: ?????

    </description>

    <target name="bootstrap" description="Install 3rd party dependencies">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0/groovy-all-2.1.0.jar"/>
    </target>

    <target name="print-hostname" description="Retrieve and print the hostname">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

        <groovy>
            properties["hostname"] = InetAddress.getLocalHost().getHostName()
        </groovy>

        <echo message="Hostname: ${hostname}"/>
    </target>

</project>

1
投票

另一种方法是编写一个javascript scriptdef,在属性中设置此信息。

<scriptdef name="get-hostame" language="javascript">
    <attribute name="prefix" /> <![CDATA[
        importClass(java.net.InetAddress);
        address = InetAddress.getLocalHost();
        prefix = attributes.get("prefix");
        project.setNewProperty(prefix + ".hostname", address.getHostName());
        project.setNewProperty(prefix + ".fqdn", address.getCanonicalHostName());
        project.setNewProperty(prefix + ".address", address.getHostAddress());
    ]]>
</scriptdef>

你可以这样称呼它:

<get-hostame prefix="uwi.host" />

结果如下:

[echoproperties] uwi.host.address=10.666.666.666
[echoproperties] uwi.host.fqdn=myhost.stackoverflow.com
[echoproperties] uwi.host.hostname=myhos

这是基于我在这里找到的一些建议:http://grokbase.com/t/ant/user/051sygfj9b/any-way-to-get-at-the-machine-name


0
投票

使用环境

<property name="hostname" value="${env.COMPUTERNAME}"/>

0
投票

使用内置的javascript

<script language="javascript">//<![CDATA[
    project.setProperty("hostname", Packages.java.net.InetAddress.getLocalHost().getHostName());
    project.setProperty("hostname-full", Packages.java.net.InetAddress.getLocalHost().getCanonicalHostName());
//]]></script>
<echo message="hostname=${hostname}"/>
<echo message="hostname-full=${hostname-full}"/>
© www.soinside.com 2019 - 2024. All rights reserved.