ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dale Anson <dan...@germane-software.com>
Subject Re: Hostname in ant
Date Tue, 24 Aug 2004 23:17:44 GMT
Below is an ant task that gets the hostname. Use it like this:

<taskdef name="hostname" classname="ise.antelope.tasks.Hostname"
    classpath="fill/in/your/path/AntelopeTasks_3.1.9.jar"/>
    <target name="testhostname">
       <hostname ip="true"/>
       <echo>${HOSTNAME}</echo>
    </target>

This will put the IP address in a property named HOSTNAME. Use just
<hostname/>
to get the actual name.


package ise.antelope.tasks;

import java.net.*;
import org.apache.tools.ant.BuildException;

import org.apache.tools.ant.Task;

public class Hostname extends Task {

     private boolean ip = false;

     public void setIP(boolean b) {
         ip = b;
     }

     public void execute() {
         try {
             InetAddress addr = InetAddress.getLocalHost();
             String hostname = "";
             if (ip) {
                 byte[] raw = addr.getAddress();
                 for (int i = 0; i < raw.length; i++) {
                     int b = (int)raw[i];
                     if (b < 0)
                         b += 256;
                     hostname += String.valueOf(b);
                     if (i < raw.length - 1)
                         hostname += ".";
                 }
             }
             else {
                 hostname = addr.getHostName();
             }
             getProject().setUserProperty("HOSTNAME", hostname);
         }
         catch (Exception e) {
             // ignored
         }
     }
}


Yiling Lu wrote:

> Or:
> 
> <exec executable="uname"  dir="./" outputproperty="host.name"
> os="Linux">
> 	<arg value="-n"/>
> </exec>			
> 
> Yiling Lu
> 
> -----Original Message-----
> From: Gera, Prateek [mailto:prateek.gera@hp.com] 
> Sent: Tuesday, August 24, 2004 1:39 PM
> To: user@ant.apache.org
> Subject: Hostname in ant
> 
> Hi,
>   How can I get the hostname OR IP adress of a unix system that ant runs
> on. The environment variable HOSTNAME is not set, as may be on some unix
> systems.
> 
> Thanks
> ./pr
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message