Java gets the native name, IP, MAC address, and network card name

Source: Internet
Author: User
Tags stringbuffer

Java gets the native name, IP, MAC address, and network card name

Excerpt: 80364370

May 18, 2018 14:53:19Hits: 134

   
  
  1. import java.net.InetAddress;
  2. import java.net.NetworkInterface;
  3. Public class IpConfig {
  4. @SuppressWarnings ("static-access")
  5. Public static void main(string[] args) throws Exception {
  6. InetAddress IA = null;
  7. Try {
  8. ia = Ia.getlocalhost ();
  9. String localname = Ia.gethostname ();
  10. String Localip = ia.gethostaddress ();
  11. System.out.println ("The Machine name is:" + localname);
  12. System.out.println ("The IP of this machine is:" + localip);
  13. } catch (Exception e) {
  14. E.printstacktrace ();
  15. }
  16. InetAddress ia1 = Inetaddress.getlocalhost (); //Get local IP object
  17. System.out.println ("Mac of native is:" + getmacaddress (ia1));
  18. }
  19. Ways to get MAC addresses
  20. private static String getmacaddress(inetaddress ia) throws Exception {
  21. Get the network interface object (that is, the NIC), and get the MAC address, the MAC address exists in a byte array.
  22. byte [] mac = Networkinterface.getbyinetaddress (IA). Gethardwareaddress ();
  23. The following code is to assemble the MAC address into a string
  24. StringBuffer sb = new stringbuffer ();
  25. for (int i = 0; i < mac.length; i++) {
  26. if (i! = 0) {
  27. Sb.append ("-");
  28. }
  29. Mac[i] & 0xFF is to convert byte to a positive integer
  30. String s = integer.tohexstring (Mac[i] & 0xFF);
  31. System.out.println ("--------------");
  32. System.err.println (s);
  33. Sb.append (s.length () = = 1 ? 0 + s:s);
  34. }
  35. Change all lowercase letters of the string to uppercase to become a regular MAC address and return
  36. return sb.tostring (). toUpperCase ();
  37. }
  38. }

The output results are as follows:

The name of the machine is: Pc-daihaijiao
The IP of this machine is: 172.16.0.31

Mac for this machine is: 00-ff-0d-99-5e-1e



   
  
  1. import java.net.Inet4Address;
  2. import java.net.InetAddress;
  3. import java.net.NetworkInterface;
  4. import java.util.Enumeration;
  5. Public class networkinterfacetest {
  6. Public static void main(string[] args) throws Exception {
  7. Get all the network interfaces for this machine
  8. enumeration<networkinterface> nifs = Networkinterface.getnetworkinterfaces ();
  9. while (Nifs.hasmoreelements ()) {
  10. NetworkInterface nif = Nifs.nextelement ();
  11. Obtain an IP address that is bound to the network interface, typically with only one
  12. Enumeration<inetaddress> addresses = nif.getinetaddresses ();
  13. while (Addresses.hasmoreelements ()) {
  14. InetAddress addr = Addresses.nextelement ();
  15. if (Addr instanceof inet4address) { //care only about IPV4 address
  16. SYSTEM.OUT.PRINTLN ("NIC Interface name:" + nif.getname ());
  17. SYSTEM.OUT.PRINTLN ("NIC interface address:" + addr.gethostaddress ());
  18. System.out.println ();
  19. }
  20. }
  21. }
  22. }
  23. }

The output results are as follows:

Nic Interface Name: Lo
Nic Interface Address: 127.0.0.1


Nic Interface Name: eth0
Nic Interface Address: 172.16.0.31


Nic Interface Name: eth2
Nic Interface Address: 192.168.220.1


Nic Interface Name: wlan2
Nic Interface Address: 192.168.0.108


Nic Interface Name: Eth8
Nic Interface Address: 192.168.138.1

Java gets the native name, IP, MAC address, and network card name

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.