Java gets the native name, IP, MAC address, and network card name
Excerpt: 80364370
May 18, 2018 14:53:19Hits: 134
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- Public class IpConfig {
- @SuppressWarnings ("static-access")
- Public static void main(string[] args) throws Exception {
- InetAddress IA = null;
- Try {
- ia = Ia.getlocalhost ();
- String localname = Ia.gethostname ();
- String Localip = ia.gethostaddress ();
- System.out.println ("The Machine name is:" + localname);
- System.out.println ("The IP of this machine is:" + localip);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- InetAddress ia1 = Inetaddress.getlocalhost (); //Get local IP object
- System.out.println ("Mac of native is:" + getmacaddress (ia1));
- }
- Ways to get MAC addresses
- private static String getmacaddress(inetaddress ia) throws Exception {
- Get the network interface object (that is, the NIC), and get the MAC address, the MAC address exists in a byte array.
- byte [] mac = Networkinterface.getbyinetaddress (IA). Gethardwareaddress ();
- The following code is to assemble the MAC address into a string
- StringBuffer sb = new stringbuffer ();
- for (int i = 0; i < mac.length; i++) {
- if (i! = 0) {
- Sb.append ("-");
- }
- Mac[i] & 0xFF is to convert byte to a positive integer
- String s = integer.tohexstring (Mac[i] & 0xFF);
- System.out.println ("--------------");
- System.err.println (s);
- Sb.append (s.length () = = 1 ? 0 + s:s);
- }
- Change all lowercase letters of the string to uppercase to become a regular MAC address and return
- return sb.tostring (). toUpperCase ();
- }
- }
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
- import java.net.Inet4Address;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.util.Enumeration;
- Public class networkinterfacetest {
- Public static void main(string[] args) throws Exception {
- Get all the network interfaces for this machine
- enumeration<networkinterface> nifs = Networkinterface.getnetworkinterfaces ();
- while (Nifs.hasmoreelements ()) {
- NetworkInterface nif = Nifs.nextelement ();
- Obtain an IP address that is bound to the network interface, typically with only one
- Enumeration<inetaddress> addresses = nif.getinetaddresses ();
- while (Addresses.hasmoreelements ()) {
- InetAddress addr = Addresses.nextelement ();
- if (Addr instanceof inet4address) { //care only about IPV4 address
- SYSTEM.OUT.PRINTLN ("NIC Interface name:" + nif.getname ());
- SYSTEM.OUT.PRINTLN ("NIC interface address:" + addr.gethostaddress ());
- System.out.println ();
- }
- }
- }
- }
- }
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