About multiple NIC read IP related information

Source: Internet
Author: User

Recently, the cluster will add a batch of dual network card machines, then I suddenly think how to obtain the IP address of these network cards. But the first attempt to use inetaddress did not get the expected results.

Run results here use/etc/hosts this file to the native (MAC OS environment) to obtain the relevant IP information (of course inetaddress get host name processing is not so simple, here is not detailed)

Through/etc/hosts to get

inetaddress[] Adds = Inetaddress.getallbyname ("master");

for (inetaddress T_add:adds) {

System.out.println (T_add);

}


Later on the Internet search, the relevant processing, found that many are using the way to call external commands to obtain, I personally think it is a bit awkward, and then look at the relevant Java source code, you can use this class NetworkInterface implementation, using the method

Very simply, the code intercepts as follows:

Get local related network card information similar to Ifconfig

enumeration<networkinterface> list = NetworkInterface

. Getnetworkinterfaces ();

for (; List.hasmoreelements ();) {

NetworkInterface nint = List.nextelement ();

System.out.println ("Net card:" + nint.getdisplayname ());

for (enumeration<inetaddress> addresses = nint.getinetaddresses (); addresses

. hasMoreElements ();) {

InetAddress I_adr = Addresses.nextelement ();

System.out.println ("------->" + i_adr.gethostaddress ());

}

}

You can obtain the IP address of multiple network adapters.


The results of the operation are as follows:

Net Card:en1

------->fe80:0:0:0:xxxx:xxxx:xxxx:a6c0%5

------->192.168.1.18

Net Card:en0

------->fe80:0:0:0:xxxx:xxxx:xxxx:32e5%4

------->10.xxx.xxx.xxx

Net Card:lo0

------->0:0:0:0:0:0:0:1

------->fe80:0:0:0:0:0:0:1%1

------->127.0.0.1

X means to block out real IP.

In fact, here also the MAC address to parse out.

Here are a few questions to extend:

Is the Hadoop itself to the multi-card processing mechanism, in the master host configuration hosts this file, how to modify.

What is the utilization of the NIC when it is a multiple NIC?

Recently in the Intranet trial of the next multiple network card, found as long as a network card port access network cable, another network card mouth does not connect network cable. Or you can ping this network card (not connect network cable) IP address.


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.