【问题标题】:How to get fully qualified hostname using Ant如何使用 Ant 获取完全限定的主机名
【发布时间】:2013-01-17 04:54:06
【问题描述】:

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

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

【问题讨论】:

标签: ant


【解决方案1】:
<exec executable="hostname" outputproperty="computer.hostname"/>

适用于 linux 和 windows,否则使用 Marc O'Connor 的 groovy 解决方案
nslookup 也可以在 linux 和 windows 上运行,如果您需要完整的主机名,您必须解析名称后的条目:在 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>

【讨论】:

    【解决方案2】:

    有一个名为 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" />
    

    【讨论】:

    • 我想要可以在所有平台上运行的东西。 hostinfo 也是用于设置而不是用于获取,是吗?
    • @performanceuser HostInfo 使用它获得的值设置属性。但是,刚刚尝试过它似乎没有我想象的那么有用。如果您不指定主机,则默认为本地主机,但似乎将HOST 设置为localhost 并将DOMAIN 设置为localdomain,而不是获取实际主机名。我没有包括的第三个选项是创建您自己的调用InetAddress.getLocalHost().getHostName() 的自定义任务。这需要更多的工作,但将是跨平台的(也许其他人已经创建了这样的任务)。
    • 我发现&lt;hostinfo prefix="host" /&gt;&lt;hostinfo prefix="host" host="localhost" /&gt;不一样,我得到了有用的信息,第一个总是host.NAME=localhost
    【解决方案3】:

    重新整理我为Maven 所做的答案:-)

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

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

    示例

    项目是自我记录的:

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

    【讨论】:

      【解决方案4】:

      另一种方法是编写一个 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

      【讨论】:

        【解决方案5】:

        使用环境

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

        【讨论】:

          【解决方案6】:

          使用内置的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}"/>
          

          【讨论】:

            猜你喜欢
            • 2014-01-14
            • 1970-01-01
            • 1970-01-01
            • 2012-07-19
            • 2023-03-15
            • 1970-01-01
            • 2018-08-11
            • 1970-01-01
            • 2013-11-21
            相关资源
            最近更新 更多