getting mac address through aglet

M

moneybhai

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import com.ibm.aglet.*;

public class GetMac extends Aglet
{


public String getMacAddress()
{
String macAddress = null;
try{
String command = "ipconfig /all";
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in =new BufferedReader(new InputStreamReader
(pid.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null)
break;
Pattern p = Pattern.compile(".*Physical Address.*: (.*)");
Matcher m = p.matcher(line);
if (m.matches()) {
macAddress = m.group(1);
break;
}
}
in.close();}
catch(Exception e)
{System.out.println(e);}
return macAddress;

}

public void run() {
String address = new GetMac().getMacAddress();
System.out.println(address);
setText(address);
}
}




well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly....
please suggest a proper way to retieve the mac address thru
aglet2.0.2.......
 
J

John B. Matthews

moneybhai said:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly.

I don't know about aglets, but your Process has several problems: What
error do you get? Why not read the entire output, which you call "in",
before parsing? Why not check the error stream, too?

Also, please indent your code more readably. It's hard to tell, but your
code may loop forever if no match is forthcoming. Exceptions should
probably be reported to a logger or System.err.

What result do you get from this example?

<code>
import java.io.*;

/** @author John B. Matthews */
class ExecTest {

public static void main (String[] args) {
String s;
try {
Process p = Runtime.getRuntime().exec(
"ipconfig /all");
// read from the process's stdout
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
System.out.println(s);
}
// read from the process's stderr
BufferedReader stderr = new BufferedReader (
new InputStreamReader(p.getErrorStream()));
while ((s = stderr.readLine()) != null) {
System.err.println(s);
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
System.err.println("Exit value: " + p.waitFor());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
</code>
 
M

moneybhai

 moneybhai said:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly.

I don't know about aglets, but your Process has several problems: What
error do you get? Why not read the entire output, which you call "in",
before parsing? Why not check the error stream, too?

Also, please indent your code more readably. It's hard to tell, but your
code may loop forever if no match is forthcoming. Exceptions should
probably be reported to a logger or System.err.

What result do you get from this example?

<code>
import java.io.*;

/** @author John B. Matthews */
class ExecTest {

    public static void main (String[] args) {
        String s;
        try {
            Process p = Runtime.getRuntime().exec(
                "ipconfig /all");
            // read from the process's stdout
            BufferedReader stdout = new BufferedReader (
                new InputStreamReader(p.getInputStream()));
            while ((s = stdout.readLine()) != null) {
                System.out.println(s);
            }
            // read from the process's stderr
            BufferedReader stderr = new BufferedReader (
                new InputStreamReader(p.getErrorStream()));
            while ((s = stderr.readLine()) != null) {
                System.err.println(s);
            }
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
            System.err.println("Exit value: " + p.waitFor());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }}

</code>

well on running this program the out put is simply the output of
command- ipconfig/all
no mac id is generated.....
suggest a new method.....
 
A

Arne Vajhøj

well on running this program the out put is simply the output of
command- ipconfig/all
no mac id is generated.....
suggest a new method.....

The output from ipconfig/all does include the mac
address (Physical Address).

Arne
 
A

Arne Vajhøj

moneybhai said:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly....
please suggest a proper way to retieve the mac address thru
aglet2.0.2.......

If you are at Java 1.6 then you can use:

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
System.out.println("Net interface: " + ni.getName());
byte[] mac = ni.getHardwareAddress();
if(mac != null) {

System.out.printf("%02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
}
}

Arne
 
M

moneybhai

moneybhai said:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly....
please suggest a proper way to retieve the mac address thru
aglet2.0.2.......

If you are at Java 1.6 then you can use:

         Enumeration e = NetworkInterface.getNetworkInterfaces();
         while(e.hasMoreElements()) {
             NetworkInterface ni = (NetworkInterface)e.nextElement();
             System.out.println("Net interface: " + ni.getName());
             byte[] mac = ni.getHardwareAddress();
             if(mac != null) {

System.out.printf("%02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
             }
         }

Arne

is there another way and that too in jdk 1.4 ,apart from native
methods to retrieve the MAC address.....
 
A

Arne Vajhøj

moneybhai said:
moneybhai said:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly....
please suggest a proper way to retieve the mac address thru
aglet2.0.2.......
If you are at Java 1.6 then you can use:

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
System.out.println("Net interface: " + ni.getName());
byte[] mac = ni.getHardwareAddress();
if(mac != null) {

System.out.printf("%02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
}
}

is there another way and that too in jdk 1.4 ,apart from native
methods to retrieve the MAC address.....

With 1.4 you will need JNI or Runtime exec.

Arne
 
J

John B. Matthews

Arne Vajhøj said:
The output from ipconfig/all does include the mac
address (Physical Address).

Arne: Thank you for confirming this.

OP: For unix/linux clients, you may want to look at the ifconfig and arp
commands. Of course you'll have to parse the results differently. Out of
curiosity, what is the goal of this exercise?
 
M

Martin Gregorie

moneybhai said:
moneybhai wrote:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly.... please suggest a proper
way to retieve the mac address thru aglet2.0.2.......
If you are at Java 1.6 then you can use:

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
System.out.println("Net interface: " + ni.getName());
byte[] mac = ni.getHardwareAddress(); if(mac != null) {

System.out.printf("%02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac [2],mac[3],mac[4],mac[5]);
}
}

is there another way and that too in jdk 1.4 ,apart from native methods
to retrieve the MAC address.....

With 1.4 you will need JNI or Runtime exec.
My copy of 1.6 Javadocs says 'since 1.4' on its NetworkInterface page.
Is this correct or was somebody being economical with the truth?
 
M

Mayeul

Martin said:
moneybhai said:
moneybhai wrote:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly.... please suggest a proper
way to retieve the mac address thru aglet2.0.2.......
If you are at Java 1.6 then you can use:

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
System.out.println("Net interface: " + ni.getName());
byte[] mac = ni.getHardwareAddress(); if(mac != null) {

System.out.printf("%02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac [2],mac[3],mac[4],mac[5]);
}
}
is there another way and that too in jdk 1.4 ,apart from native methods
to retrieve the MAC address.....
With 1.4 you will need JNI or Runtime exec.
My copy of 1.6 Javadocs says 'since 1.4' on its NetworkInterface page.
Is this correct or was somebody being economical with the truth?

It is correct, but this same Javadoc says 'since 1.6' about the
getHardwareAddress() method in the same page.
 
R

Roedy Green

My copy of 1.6 Javadocs says 'since 1.4' on its NetworkInterface page.
Is this correct or was somebody being economical with the truth?

NetworkInterface came in with 1.4, but getHardwareAddress came in
with 1.6.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"If you think it’s expensive to hire a professional to do the job, wait until you hire an amateur."
~ Red Adair (born: 1915-06-18 died: 2004-08-07 at age: 89)
 
A

Arne Vajhøj

Martin said:
moneybhai said:
moneybhai wrote:
well,this is my code for retrieving the mac address thru aglet2.0.2 .
But the solution is not coming properly.... please suggest a proper
way to retieve the mac address thru aglet2.0.2.......
If you are at Java 1.6 then you can use:

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
System.out.println("Net interface: " + ni.getName());
byte[] mac = ni.getHardwareAddress(); if(mac != null) {

System.out.printf("%02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
}
}
is there another way and that too in jdk 1.4 ,apart from native methods
to retrieve the MAC address.....
With 1.4 you will need JNI or Runtime exec.
My copy of 1.6 Javadocs says 'since 1.4' on its NetworkInterface page.
Is this correct or was somebody being economical with the truth?

class != method

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top