getting mac address through aglet

Discussion in 'Java' started by moneybhai, Aug 16, 2009.

  1. moneybhai

    moneybhai Guest

    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.......
     
    moneybhai, Aug 16, 2009
    #1
    1. Advertising

  2. In article
    <>,
    moneybhai <> wrote:

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

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Aug 16, 2009
    #2
    1. Advertising

  3. moneybhai

    moneybhai Guest

    On Aug 17, 12:02 am, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >  moneybhai <> wrote:
    > > 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>
    >
    > --
    > John B. Matthews
    > trashgod at gmail dot com
    > <http://sites.google.com/site/drjohnbmatthews>


    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.....
     
    moneybhai, Aug 16, 2009
    #3
  4. moneybhai

    Arne Vajhøj Guest

    moneybhai wrote:
    > On Aug 17, 12:02 am, "John B. Matthews" <> wrote:
    >> In article
    >> <>,
    >> moneybhai <> wrote:
    >>> well,this is my code for retrieving the mac address thru aglet2.0.2 .
    >>> But the solution is not coming properly.

    >> Process p = Runtime.getRuntime().exec(
    >> "ipconfig /all");


    > 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
     
    Arne Vajhøj, Aug 16, 2009
    #4
  5. moneybhai

    Arne Vajhøj Guest

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

    Arne
     
    Arne Vajhøj, Aug 16, 2009
    #5
  6. moneybhai

    moneybhai Guest

    On Aug 17, 12:53 am, Arne Vajhøj <> wrote:
    > 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]);
    >              }
    >          }
    >
    > Arne


    is there another way and that too in jdk 1.4 ,apart from native
    methods to retrieve the MAC address.....
     
    moneybhai, Aug 16, 2009
    #6
  7. moneybhai

    Arne Vajhøj Guest

    moneybhai wrote:
    > On Aug 17, 12:53 am, Arne Vajhøj <> wrote:
    >> 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.

    Arne
     
    Arne Vajhøj, Aug 16, 2009
    #7
  8. In article <4a886350$0$303$>,
    Arne Vajhøj <> wrote:

    > moneybhai wrote:
    > > On Aug 17, 12:02 am, "John B. Matthews" <> wrote:
    > >> In article
    > >> <>,
    > >> moneybhai <> wrote:
    > >>> well,this is my code for retrieving the mac address thru aglet2.0.2 .
    > >>> But the solution is not coming properly.
    > >> Process p = Runtime.getRuntime().exec(
    > >> "ipconfig /all");

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

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Aug 17, 2009
    #8
  9. On Sun, 16 Aug 2009 16:45:35 -0400, Arne Vajhøj wrote:

    > moneybhai wrote:
    >> On Aug 17, 12:53 am, Arne Vajhøj <> wrote:
    >>> 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?


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Aug 17, 2009
    #9
  10. moneybhai

    Mayeul Guest

    Martin Gregorie wrote:
    > On Sun, 16 Aug 2009 16:45:35 -0400, Arne Vajhøj wrote:
    >
    >> moneybhai wrote:
    >>> On Aug 17, 12:53 am, Arne Vajhøj <> wrote:
    >>>> 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.

    --
    Mayeul
     
    Mayeul, Aug 17, 2009
    #10
  11. moneybhai

    Roedy Green Guest

    On Mon, 17 Aug 2009 08:42:45 +0000 (UTC), Martin Gregorie
    <> wrote, quoted or indirectly quoted
    someone who said :

    >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)
     
    Roedy Green, Aug 17, 2009
    #11
  12. moneybhai

    Roedy Green Guest

    I am not familiar with Aglet, but there are other ways to get the MAC.
    See http://mindprod.com/jgloss/mac.html
    --
    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)
     
    Roedy Green, Aug 17, 2009
    #12
  13. On Mon, 17 Aug 2009 07:18:24 -0700, Roedy Green wrote:

    > On Mon, 17 Aug 2009 08:42:45 +0000 (UTC), Martin Gregorie
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>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.


    My mistake. I missed that.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Aug 17, 2009
    #13
  14. Martin Gregorie wrote:
    > On Sun, 16 Aug 2009 16:45:35 -0400, Arne Vajhøj wrote:
    >> moneybhai wrote:
    >>> On Aug 17, 12:53 am, Arne Vajhøj <> wrote:
    >>>> 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
     
    Arne Vajhøj, Aug 18, 2009
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SJ

    MAC Address/Physical Address?

    SJ, Jan 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    558
  2. Guest

    Mobile Agent Java Aglet

    Guest, May 1, 2004, in forum: Java
    Replies:
    0
    Views:
    967
    Guest
    May 1, 2004
  3. computerman

    aglet problem

    computerman, Feb 11, 2007, in forum: Java
    Replies:
    0
    Views:
    383
    computerman
    Feb 11, 2007
  4. Ravikumar
    Replies:
    7
    Views:
    828
    Oliver Wong
    Apr 11, 2007
  5. moneybhai

    aglet

    moneybhai, Aug 16, 2009, in forum: Java
    Replies:
    3
    Views:
    444
    Martin Gregorie
    Aug 17, 2009
Loading...

Share This Page