Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java get the basic information of the host?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java is how to get the basic information of the host, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Recently, in the need to do a host resource monitoring, the first thing is to obtain some of the simplest basic parameters, such as some host name, system type, ip, cpu, memory and disk, and so on. Although it seems very simple, the basic library of Java can be completed, but there are still some holes when you really use it. Record it and prepare it for later use.

1. Get basic information 1.1 get host name and system

The host name can be obtained through the network class InetAddress, and the host system and users can be obtained through the System class.

Public static void getLocalHost () {try {InetAddress ip = InetAddress.getLocalHost (); String localName = ip.getHostName (); String osName = System.getProperty ("os.name"); String userName = System.getProperty ("user.name"); String osVersion = System.getProperty ("os.version"); String osArch = System.getProperty ("os.arch") System.out.println ("current user:" + userName); System.out.println ("user's home directory: + props.getProperty (" user.home ")); System.out.println (" user's current working directory: + props.getProperty ("user.dir")); System.out.println ("host name:" + localName) System.out.println ("Host system:" + osName); System.out.println ("system version:" + osVersion); System.out.println ("system Architecture:" + osArch);} catch (Exception e) {e.printStackTrace ();}} 1.2 get user information

User information is obtained using the System class.

Public static void getUserInfo () {try {String userName = System.getProperty ("user.name"); String userHome = System.getProperty ("user.home"); String userDir = System.getProperty ("user.dir"); System.out.println ("current user:" + userName); System.out.println ("user home directory:" + userHome) System.out.println ("current working directory:" + userDir);} catch (Exception e) {e.printStackTrace ();}} 1.3 get host IP and other information

The ip of the host can be obtained through the network class InetAddress, but this method is very mysterious, when there are multiple network cards and virtual machines on the machine, it is not accurate. At present, the method of obtaining is to beat the convenient network card to obtain ip. Because traversing the network card to obtain the ip needs to filter some unimportant network cards, the filtering method is a stupid method from "experience", which can be used for reference, but it does not guarantee that the acquisition is not accurate when the network card conditions are complex in the future. Testing the availability of Linux, Mac, and Windows systems. Because the filter conditions are different, it is divided into Windows acquisition and non-Windows acquisition.

Windows system acquires IP:

Public static void getWindowsIpAndMac () {try {Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces (); / / traversing the Nic interface while (allNetInterfaces.hasMoreElements ()) {NetworkInterface netInterface = allNetInterfaces.nextElement () / / remove loopback interface, subinterface, not running and interface if (netInterface.isLoopback () | | netInterface.isVirtual () | |! netInterface.isUp ()) {continue } / / the key point is: "experience" / / in order to filter out the network card of the virtual machine, the basic filtering can be carried out through the name of the network card. The name of the network card corresponding to the windows host ip will contain the following three: Intel Wireless, Realtek network cable, and Ethernet compatible xp system if (! netInterface.getDisplayName (). Contains ("Intel") & &! netInterface.getDisplayName (). Contains ("Realtek") & &! netInterface.getDisplayName (). Contains ("Ethernet")) {continue } String ip = "; String mac ="; String niName ="; Enumeration addresses = netInterface.getInetAddresses (); while (addresses.hasMoreElements ()) {InetAddress ia = addresses.nextElement () / / remove local loopback address, subinterface Not running and address if (ia! = null & &! ia.isLoopbackAddress () & & ia.isSiteLocalAddress () & &! ia.isAnyLocalAddress ()) {/ / determine whether it is an ip v4 address if (ia instanceof Inet4Address) {ip = ia.getHostAddress () / / get the MAC address mac = getMac (ia); niName = netInterface.getName (); if (StringUtils.isNotBlank (ip) & & StringUtils.isNotBlank (mac) & & StringUtils.isNotBlank (niName)) {System.out.println ("current Nic:" + niName) System.out.println ("current host ip:" + ip); System.out.println ("current host MAC:" + mac); return } catch (SocketException e) {e.printStackTrace ();}}

Non-Windows system acquires IP:

In fact, and windows access is similar, but also traverse the network card and then filter, but this is no "experience", do not know to filter those, so use InetAddress to obtain, after testing on the non-windows or accurate (maybe my linux network card is single). However, in order to obtain the current network card using a more stupid method, since the current acquisition of ip is accurate, then according to ip to obtain the network card. However, this method has not been found, so you can remove the network card that matches the current ip when traversing the network card. This method is OK in my need, and there is no guarantee that it can be used if it is taken away.

Public static void getLinuxIpAndMac (AgentMonitor agentMonitor) {try {/ / get ip InetAddress iad = InetAddress.getLocalHost (); String localIp = iad.getHostAddress (); / / traverse the Nic Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces (); while (allNetInterfaces.hasMoreElements ()) {NetworkInterface netInterface = allNetInterfaces.nextElement () / / remove loopback interface, subinterface, not running and interface if (netInterface.isLoopback () | | netInterface.isVirtual () | |! netInterface.isUp ()) {continue;} String ip = ""; String mac = ""; String niName = ""; Enumeration addresses = netInterface.getInetAddresses () While (addresses.hasMoreElements ()) {InetAddress ia = addresses.nextElement () If (ia! = null & &! ia.isLoopbackAddress () & & ia.isSiteLocalAddress () & &! ia.isAnyLocalAddress ()) {/ / determine whether it is an ip v4 address and whether it is consistent with the obtained ip if (ia instanceof Inet4Address & & ia.getHostAddress (). Equals (localIp)) {ip = ia.getHostAddress () / / get the MAC address mac = getMac (ia); niName = netInterface.getName (); if (StringUtils.isNotBlank (ip) & & StringUtils.isNotBlank (mac) & & StringUtils.isNotBlank (niName)) {System.out.println ("current Nic:" + niName) System.out.println ("current host ip:" + ip); System.out.println ("current host MAC:" + mac); return } catch (Exception e) {e.printStackTrace ();}}

Get the MAC address

Public static String getMac (InetAddress ia) {try {/ / get the network card and get the address byte [] mac = NetworkInterface.getByInetAddress (ia). GetHardwareAddress (); StringBuffer sb = new StringBuffer (); if (mac! = null & & mac.length > 0) {for (int iTuno; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report