Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > getting mac address through aglet

Reply
Thread Tools

getting mac address through aglet

 
 
moneybhai
Guest
Posts: n/a
 
      08-16-2009
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.......
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      08-16-2009
In article
<449fa187-854f-48c9-8f8c->,
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>
 
Reply With Quote
 
 
 
 
moneybhai
Guest
Posts: n/a
 
      08-16-2009
On Aug 17, 12:02*am, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <449fa187-854f-48c9-8f8c-f438b5aab...@v20g2000yqm.googlegroups.com>,
>
> *moneybhai <manish...@gmail.com> 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.....
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-16-2009
moneybhai wrote:
> On Aug 17, 12:02 am, "John B. Matthews" <nos...@nospam.invalid> wrote:
>> In article
>> <449fa187-854f-48c9-8f8c-f438b5aab...@v20g2000yqm.googlegroups.com>,
>> moneybhai <manish...@gmail.com> 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
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-16-2009
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
 
Reply With Quote
 
moneybhai
Guest
Posts: n/a
 
      08-16-2009
On Aug 17, 12:53*am, Arne Vajhøj <a...@vajhoej.dk> 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.....
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-16-2009
moneybhai wrote:
> On Aug 17, 12:53 am, Arne Vajhøj <a...@vajhoej.dk> 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
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      08-17-2009
In article <4a886350$0$303$>,
Arne Vajhøj <> wrote:

> moneybhai wrote:
> > On Aug 17, 12:02 am, "John B. Matthews" <nos...@nospam.invalid> wrote:
> >> In article
> >> <449fa187-854f-48c9-8f8c-f438b5aab...@v20g2000yqm.googlegroups.com>,
> >> moneybhai <manish...@gmail.com> 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>
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      08-17-2009
On Sun, 16 Aug 2009 16:45:35 -0400, Arne Vajhøj wrote:

> moneybhai wrote:
>> On Aug 17, 12:53 am, Arne Vajhøj <a...@vajhoej.dk> 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 |
 
Reply With Quote
 
Mayeul
Guest
Posts: n/a
 
      08-17-2009
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 <a...@vajhoej.dk> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Differnce between setting mac address port security under theinterface vs. the mac address-table global command ttripp Cisco 5 02-05-2010 10:49 PM
aglet moneybhai Java 3 08-17-2009 08:37 AM
Problem in loading a text file to java aglet application. Ravikumar Java 7 04-11-2007 04:57 PM
aglet problem computerman Java 0 02-11-2007 03:53 AM
Mobile Agent Java Aglet Java 0 05-01-2004 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57