| |

VerySource

 Forgot password?
 Register
Search
View: 814|Reply: 3

How to obtain the Mac address of the network card under Linux and Unix

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-16 15:30:02
| Show all posts |Read mode
Ladies and gentlemen, has anyone done the method of obtaining Mac address under linux and unix, how to write using java program?
Experience provides a little reference, thank you!
Reply

Use magic Report

0

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-5-12 14:00:01
| Show all posts
Java is regardless of operating system, cross-platform!
Java can get the mac address. It is the process flow.

import java.io. *;

public class GetMac
{
    // Get the network card address by IP
        private String getMacAddressIP (String remotePcIP) {
      String str = "";
      String macAddress = "";
      try {
           Process pp = Runtime.getRuntime (). Exec ("nbtstat -A" + remotePcIP);
           InputStreamReader ir = new InputStreamReader (pp.getInputStream ());
           LineNumberReader input = new LineNumberReader (ir);
           for (int i = 1; i <100; i ++)
              {
               str = input.readLine ();
               if (str! = null)
                 {
                   if (str.indexOf ("MAC Address")> 1)
                     {macAddress = str.substring (str.indexOf ("MAC Address") + 14, str.length ());
                       break;
                    }
                 }
              }
          }
      catch (IOException ex) {}
      return macAddress;
  }
  // Get the network card address by machine name
  private String getMacAddressName (String remotePcIP) {
      String str = "";
      String macAddress = "";
      try {
           Process pp = Runtime.getRuntime (). Exec ("nbtstat -a" + remotePcIP);
           InputStreamReader ir = new InputStreamReader (pp.getInputStream ());
           LineNumberReader input = new LineNumberReader (ir);
           for (int i = 1; i <100; i ++)
              {
               str = input.readLine ();
               if (str! = null)
                 {
                   if (str.indexOf ("MAC Address")> 1)
                     {macAddress = str.substring (str.indexOf ("MAC Address") + 14, str.length ());
                       break;
                    }
                 }
              }
          }
      catch (IOException ex) {}
      return macAddress;
  }


        public static void main (String [] args)
        {
        GetMac getmac;
        getmac = new GetMac ();
                String mac = "";
        mac = getmac.getMacAddressIP ("10.67.0.6");
        System.out.println (mac);
        mac = getmac.getMacAddressName ("gxy");
        System.out.println (mac);
        }
}
Reply

Use magic Report

0

Threads

13

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-5-14 22:15:01
| Show all posts
Thewh_familycommand is still a bit problematic, I think it should be ifconfig eth0 instead of netstat -A
Then the following str.indexOf ("MAC Address") is changed to str.indexOf ("HWaddr")
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

 Author| Post time: 2020-6-4 16:45:01
| Show all posts
Not bad, thanks for the good suggestions upstairs!
I want to bind my software to the user's network card, so that users will not use my software for others to use for free, playing a controlling role.
Can the command statement in this sentence Process pp = Runtime.getRuntime().exec ("nbtstat -A "+ remotePcIP); get the Mac address on any platform? In fact, I just want to find a general command, otherwise the user will not be able to monitor the Mac address and change the platform.
But I tried it upstairs, thank you very much!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list