-3

Many motherboards have an integrated network adapter. I need to get mac address from this device if exists. From Network adapter:

private void getMacFromInetAddress(){
    try {

            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            while (networks.hasMoreElements()) {
                NetworkInterface network = networks.nextElement();
                byte[] mac = network.getHardwareAddress();

                if (mac != null) {
                    System.out.print("Current MAC address : ");

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                }
           }
        } catch (UnknownHostException | SocketException e) {
            System.out.println(e.getLocalizedMessage());
        }
}

how to get specify the name of a integrated network adapter from this code?

Saahon
  • 404
  • 1
  • 6
  • 27

4 Answers4

4
InetAddress ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());

NetworkInterface network = NetworkInterface.getByInetAddress(ip);

byte[] mac = network.getHardwareAddress();

System.out.print("Current MAC address : ");

StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());

and to get the hardware info u can use wmic commands like wmic cpu get ProcessorId

 Process p = Runtime.getRuntime().exec("wmic cpu get ProcessorId");
            String procesSerial=
                    new BufferedReader
                            (new InputStreamReader(p.getInputStream())).[ReadSecondLine];

and bios

wmic bios get SerialNumber

 Process p = Runtime.getRuntime().exec("wmic bios get SerialNumber");
RamiReddy P
  • 1,628
  • 1
  • 18
  • 29
  • 1
    you can even get the list of ethernet adapters/Mac address if you want – RamiReddy P Oct 17 '17 at 07:22
  • friend, here and mac address virtual network adapters, too, gives and then will be the same mac addresses on multiple machine – Saahon Oct 19 '17 at 06:09
  • @Saahon Then create a combo String like CPUID+"|"+BIOSSerial+"|"+MAC so then duplication will be minimised. if you want to read hardware info, it is no more platform independent. Thanks. – RamiReddy P Oct 19 '17 at 06:24
  • Nope yar, no need to copy for such simple question,, if its useful upote – RamiReddy P Oct 20 '17 at 03:01
1

How to get mac address from device?

There is no direct way to get mac address from system through Java. But you can

To get mac addresses on the computer, Just run the command getmac to get the mac addresses.enter image description here

  1. Use ipconfig command to deal with this.
  2. Retrieve the mac addressses for a remote computer using nbtstat command.
  3. Run the getmac and getmac /s remote_computer /u username /p password command to retrieve the mac addresses of a remote computer.

Get mac address from command line

But through Java you need to use Java exec system with Java ProcessBuilder and Process

For example:

ProcessBuilder pb = new ProcessBuilder("getmac");
//Map<String, String> env = pb.environment(); //set env
pb.directory("C\\");
Process p = pb.start();

or

// build my command as a list of strings
//@@@ For Unix/Linux
List<String> command = new ArrayList<String>();
command.add("getmac");

// execute my command
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(command);
int result = commandExecutor.executeCommand();

Then read output through:

// get the output from the command
StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
StringBuilder stderr = commandExecutor.getStandardErrorFromCommand();

// print the output from the command
System.out.println("STDOUT");
System.out.println(stdout);
System.out.println("STDERR");
System.out.println(stderr);

ProcessBuilder and Process Code

Note: Your java application need administrative permission during execution.

0

Use the UUID with some changes

1st time it runs, generate a unique UUID and SAVE it in local storage. From 2nd time onwards use the saved UUID from 1st run.

mithwick93
  • 71
  • 5
  • let's say this: my program for each machine gives only one unique key. If I generate uuid and save it local storage, then the client will copy this program with local storage and put it on another machine instead of with a local key .... – Saahon Oct 15 '17 at 05:36
0

I suggest you to look at the oshi library to get system OS/hardware information.

Look at the test case for the example.

Below is the truncated output on my system.

Microsoft Windows 7 SP1 build 7601
manufacturer: LENOVO
model: 20AWA0MAIN
serialnumber: PB01FEYL
baseboard:
  manufacturer: LENOVO
  serialnumber: L1HF43B026Z               
Intel(R) Core(TM) i7-4600M CPU @ 2.90GHz
 2 physical CPU(s)
 4 logical CPU(s)
Identifier: Intel64 Family 6 Model 60 Stepping 3
ProcessorID: BFEBFBFF000306C3
Memory: 9.2 GiB/15.7 GiB
Swap used: 0 bytes/15.7 GiB
Uptime: 0 days, 04:24:20
Disks:
 \\.\PHYSICALDRIVE0: (model: HGST HTS725050A7E6300 SCSI Disk Device (Standard disk drives) - S/N:       TF655AWH2RKE6L) size: 500.1 GB, reads: 325475 (7.1 GiB), writes: 306825 (4.4 GiB), xfer: 12447064 ms
 |-- Disk #0, Partition #0: Installable File System (Installable File System) Maj:Min=0:0, size: 104.9 MB
Network interfaces:
 Name: eth8 (Intel(R) Ethernet Connection I217-LM)
   MAC Address: 28:d2:44:68:40:23 
   MTU: 1500, Speed: 0 bps 
   IPv4: [] 
   IPv6: [fe80:0:0:0:553d:8bc9:6a95:236e] 
   Traffic: received ?/?; transmitted ?/? 
 Name: wlan9 (Intel(R) Dual Band Wireless-N 7260)
   MAC Address: 7c:7a:91:37:cb:f7 
   MTU: 1500, Speed: 130 Mbps 
   IPv4: [192.168.1.66] 
   IPv6: [fe80:0:0:0:55e0:97b3:f282:4c06] 
   Traffic: received 121644 packets/114.0 MiB (0 err); transmitted 90086 packets/12.7 MiB (0 err) 
Displays:
 Display 0:
  Manuf. ID=LEN, Product ID=40a0, Analog, Serial=00000000, ManufDate=1/2012, EDID v1.4
  31 x 17 cm (12.2 x 6.7 in)
  Preferred Timing: Clock 72MHz, Active Pixels 2656x768 
  Preferred Timing: Clock 63MHz, Active Pixels 2656x768 
  Manufacturer Data: 0000000F008C09328C093214090006AF3C33
  Unspecified Text: B140XTN03.3
skadya
  • 4,330
  • 19
  • 27