如何在ant 1.5.1中获取本地IP地址

发布于 2024-11-26 07:11:48 字数 145 浏览 0 评论 0原文

我想知道是否有办法用ant找出本地IP地址。我无法使用 hostinfo 任务,因为我绑定到 ant 1.5.1。现在,我将为每个平台编写一些小脚本,并使用 ant 条件机制为每个平台执行适当的脚本。但是,也许你们中有人知道一种更优雅的方式?提前致谢。

本杰明

I was wondering, if there is a way to figure out the local IP address with ant. I cannot use the hostinfo task, since I am bound to ant 1.5.1. Now I would write little scripts for each platform and use ant conditional mechanics to execute the appropriate script for each platform. However, maybe any of you know a more elegant way? Thanks in advance.

Benjamin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

躲猫猫 2024-12-03 07:11:48

这适用于我运行 os x 10.8.2 的 Mac:

    <target name="getCurrentIP">
        <exec executable="/usr/sbin/ipconfig" outputproperty="currentIP">
            <arg value="getifaddr"/>
            <arg value="en0"/>
        </exec>
        <echo>currentIP : ${currentIP}</echo>
    </target>

This works on my mac running os x 10.8.2 :

    <target name="getCurrentIP">
        <exec executable="/usr/sbin/ipconfig" outputproperty="currentIP">
            <arg value="getifaddr"/>
            <arg value="en0"/>
        </exec>
        <echo>currentIP : ${currentIP}</echo>
    </target>
吹梦到西洲 2024-12-03 07:11:48
<target name="checkos">
    <condition property="isWindows" value="true">
        <os family="windows" />
    </condition>

    <condition property="isLinux" value="true">
        <os family="unix" />
    </condition>
</target>

<target name="if_windows" depends="checkos" if="isWindows">
    <exec executable="cmd" outputproperty="myHostName">
        <arg value="/c" />
        <arg value="hostname"/>
    </exec>

    <exec executable="cmd" outputproperty="infraServerIPTemp" >
        <arg value="/c"/>
        <arg value="FOR /f "tokens=1 delims=:" %d IN ('ping ${myHostName} -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %g IN ("%d") DO echo infraServerIP=%g > myIP.properties"/>
    </exec>

    <property file="myIP.properties"/>
</target>

<target name="if_unix" depends="checkos" if="isLinux">
    <exec executable="hostname" outputproperty="infraServer">
        <arg line="-i"/>
    </exec>
    <property name="infraServerIP" value="${infraServer}"/>
</target>

<target name="checkOSType" depends="if_windows, if_unix"/>

<target name="do-something" depends="checkOSType">

</target>
<target name="checkos">
    <condition property="isWindows" value="true">
        <os family="windows" />
    </condition>

    <condition property="isLinux" value="true">
        <os family="unix" />
    </condition>
</target>

<target name="if_windows" depends="checkos" if="isWindows">
    <exec executable="cmd" outputproperty="myHostName">
        <arg value="/c" />
        <arg value="hostname"/>
    </exec>

    <exec executable="cmd" outputproperty="infraServerIPTemp" >
        <arg value="/c"/>
        <arg value="FOR /f "tokens=1 delims=:" %d IN ('ping ${myHostName} -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %g IN ("%d") DO echo infraServerIP=%g > myIP.properties"/>
    </exec>

    <property file="myIP.properties"/>
</target>

<target name="if_unix" depends="checkos" if="isLinux">
    <exec executable="hostname" outputproperty="infraServer">
        <arg line="-i"/>
    </exec>
    <property name="infraServerIP" value="${infraServer}"/>
</target>

<target name="checkOSType" depends="if_windows, if_unix"/>

<target name="do-something" depends="checkOSType">

</target>
像你 2024-12-03 07:11:48

在 Ant 插件 Flaka 的帮助下,您可以使用:

<project xmlns:fl="antlib:it.haefelinger.flaka">

<!-- on windows -->
<exec executable="cmd" outputproperty="winip">
 <arg value="/c" />
 <arg value="ipconfig" />
</exec>

<!-- simple echo -->
<fl:echo>localip => #{replace('${winip}', '$2' , '\s(IP.+):\s?(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)')}</fl:echo>
<!-- set property -->
<fl:let>localip := replace('${winip}', '$2' , '\s(IP.+):\s?(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)')</fl:let>

<!-- on linux -->
<exec executable="hostname" outputproperty="linuxip">
 <arg value="-i" />
</exec>

<!-- simple echo -->
<fl:echo>localip => #{replace('${linuxip}', '$1' , '(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)\s(.+)')}</fl:echo>
<!-- set property -->
<fl:let>localip := replace('${linuxip}', '$1' , '(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)\s(.+)')</fl:let>

</project>

with the help of Ant addon Flaka you might use :

<project xmlns:fl="antlib:it.haefelinger.flaka">

<!-- on windows -->
<exec executable="cmd" outputproperty="winip">
 <arg value="/c" />
 <arg value="ipconfig" />
</exec>

<!-- simple echo -->
<fl:echo>localip => #{replace('${winip}', '$2' , '\s(IP.+):\s?(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)')}</fl:echo>
<!-- set property -->
<fl:let>localip := replace('${winip}', '$2' , '\s(IP.+):\s?(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)')</fl:let>

<!-- on linux -->
<exec executable="hostname" outputproperty="linuxip">
 <arg value="-i" />
</exec>

<!-- simple echo -->
<fl:echo>localip => #{replace('${linuxip}', '$1' , '(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)\s(.+)')}</fl:echo>
<!-- set property -->
<fl:let>localip := replace('${linuxip}', '$1' , '(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)\s(.+)')</fl:let>

</project>
空心↖ 2024-12-03 07:11:48

我们解决这个问题的方法是,我们构建了一个小的 Java 程序,它将本地 ip 打印到标准输出。我们将此输出存储在 ant 属性中。 (我们使用 Java 而不是某种脚本语言,因为否则我们必须在许多系统上部署语言运行时,而 Java 已经部署在我们的整个系统环境中)

Our solution to the problem is, that we built a little Java program, which prints the local ip to the standard output. We store this output in an ant property. (We use Java instead of a scripting language of some sort, because we would otherwise have to deploy the language runtime on many systems and Java is already deployed throughout our system landscape)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文