Java request host IP address and its corresponding subnet mask

Source: Internet
Author: User

Not much to say, directly on the code.
Package Com.ckw.test;import Java.io.BufferedReader; Import java.io.IOException; Import Java.io.inputstreamreader;import Java.util.*;import Java.util.map.entry;public class TestOfSubNetMask {Map <string, string> map = new hashmap<string, string> (); String ipAddress; String SubnetMask; Boolean flag = false; Used to mark a pair of IP addresses with a subnet mask (a pair that is adjacent to the location)//Get the subnet mask for the corresponding IP address public void Getipaddressandmask () {String OS = system.getproperty ("OS.N   Ame ");  if (OS! = null && os.startswith ("Windows")) {try {String command = "cmd.exe/c Ipconfig/all";    Process p = runtime.getruntime (). exec (command);    BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream (), "gb2312"));      String Line; System.out.println ("---------------------------------------------------------------");            while (line = Br.readline ())! = null) {System.out.println (line); This sentence can be omitted, mainly in order to print out all the information convenient comparison/* * To obtain the machine IP address * In the line beginning with "IPV4" to obtain an IP address, the English version is "IP Address" beginning * Format: * IPV4 Address ............:10.10.116.132(preferred) <span style= "White-space:pre" ></span>//see the command output under Doc */if ( Line.indexof ("IPV4 address") > 0 | |   Line.indexof ("IP Address") > 0) {int index = Line.indexof (":");     Index + = 2;    ipAddress = line.substring (index); Remove Chinese Characters "10.10.116.132 (preferred)" <span style= "white-space:pre" ></span> int i; char [] array = Ipaddress.tochararray (); for (i=0; i<array.length; i++) {if (array[i]!= '. ' && (array[i]< ' 0 ' | | Array[i]> ' 9 ')) break;  } ipAddress = ipaddress.substring (0, I);  Flag = true; There is an IP address, there must be its corresponding subnet mask}//* Get the machine subnet mask * In the line beginning with "subnet mask" to get the subnet mask, the English version is "Subnet mask" beginning * format: * Subnet mask ... ...: 255.255.255.192 */if (flag==true && (Line.indexof ("Subnet mask") > 0 | |       Line.indexof ("Subnet Mask") > 0) {int index = Line.indexof (":");       Index + = 2;      SubnetMask = line.substring (index);  Flag = false; Consumes Map.put (ipAddress, SubnetMask); }} br.close (); System.out.println ("---------------------------------------------------------------");  } catch (IOException e) {e.printstacktrace (); }}}//main function test public static void main (String args[]) {testofsubnetmask test = new Testofsubnetmask (); test.ge  Tipaddressandmask (); map<string, string> map = Test.map; iterator<entry<string, string>> i = Map.entryset (). Iterator (); while (I.hasnext ()) {entry<string, string> e = I.next (); String ipAddress = E.getkey (); String SubnetMask = E.getvalue (); System.out.println ("IP Address:" + ipAddress); SYSTEM.OUT.PRINTLN ("Subnet mask:" + SubnetMask);        } } }


The result is:

---------------------------------------------------------------Windows IP Configuration host name ............. : DADI primary DNS suffix ........... : node type ............: hybrid IP routing enabled ..........: no WINS agent enabled ... . : No Wireless LAN Adapter wireless network connection 29: Connection specific DNS suffix ...: description ............... : Microsoft Virtual WiFi Miniport Adapter #26 Physical address ............. : 22-16-d8-c9-a3-b2 DHCP is enabled ........... : No automatic configuration Enabled ..........: is a local link IPv6 address ..... : fe80::89eb:f7f1:e6b7:6133%74 (preferred) <span style= "color: #ff0000;" > IPV4 Address ............:172.17.31.1(preferred) </span> <span style= "color: #ff0000;" > Subnet Mask ............:255.255.0.0</span> default gateway ............. : DHCPv6 IAID ...........  : 1512183512 DHCPv6 Client DUID ...: 00-01-00-01-19-03-ef-d5-08-60-6e-dc-a7-b7 DNS server ...........            : fec0:0:0:ffff::1%1                           fec0:0:0:ffff::2%1 fec0:0:0:ffff::3%1 TCPIP on the NetBIOS ...: Wireless LAN adapter wireless network connection enabled: Media status ............: media disconnected specific DNS suffix .....: Description ...... . . . . . . . . . . . : Qualcomm Atheros AR9485 Wireless Network Adapter Physical Address ............. : 20-16-d8-c9-a3-b2 DHCP is enabled ........... : Is automatic configuration enabled ..........: is an Ethernet adapter Local Area Connection: Connection specific DNS suffix ...: description ............... : Realtek PCIe GBE Family Controller Physical Address ............. : 08-60-6e-dc-a7-b7 DHCP is enabled ........... : Is automatic configuration enabled ..........: is a local link IPv6 address ..... : fe80::c0c9:c8b6:4003:7976%12 (preferred) <span style= "color: #ff0000;"   >ipv4 Address ............:10.10.116.132(preferred) Subnet mask ............:255.255.255.192</span> Time to get a lease ... . : July 18, 2015 23:01:04 time the lease expires ..... . : July 25, 2015 23:01:02 defaultGateway............. : 10.10.116.129 DHCP Server ........... : 222.200.98.70 DHCPv6 IAID ...........  : 252207214 DHCPv6 Client DUID ...: 00-01-00-01-19-03-ef-d5-08-60-6e-dc-a7-b7 DNS server ........... : 202.96.128.86 202.116.128.1 TCPIP on NetBIOS ...: Tunnel adapter Local connection is enabled * 37 : Connect a specific DNS suffix ...: description ............... : Teredo Tunneling Pseudo-Interface Physical Address ............. : 00-00-00-00-00-00-00-e0 DHCP is enabled ........... : No automatic configuration enabled ..........: is IPV6 address ............:2001:0:9d38:6ab8:aa:e4df:48c0:88c7(preferred) Local link IP V6 Address ... : fe80::aa:e4df:48c0:88c7%66 (preferred) Default gateway ............. ::: TCPIP on NetBIOS ...: Tunnel adapter 6to4 Adapter: Media status ............: Media has been disconnected from a specific DNS suffix. . . . . . . : Description ............... : Microsoft 6to4 Adapter Physical Address ............ . : 00-00-00-00-00-00-00-e0 DHCP is enabled ...........  : No automatic configuration enabled ..........: is a tunnel adapter reusable Microsoft 6to4 Adapter: Media status ............: media disconnected-specific DNS suffix ...: description ............... : Microsoft 6to4 Adapter #2 Physical Address ............. : 00-00-00-00-00-00-00-e0 DHCP is enabled ...........     : No automatic configuration enabled ..........: is tunnel adapter Local Area Connection * 17: Media status ............: media disconnected specific DNS suffix ....: Describe............... : Microsoft 6to4 Adapter #3 Physical Address ............. : 00-00-00-00-00-00-00-e0 DHCP is enabled ........... : No automatic configuration enabled ..........: Yes//part omitted ... ---------------------------------------------------------------
This is the main output <span style= "color: #000099;" >IP site  : 10.10.116.132 Subnet Mask: 255.255.255.192IP  address: 172.17.31.1 Subnet mask:255.255.0.0</span>


Analysis: Red for the IP address to be crawled with the subnet mask, Blue is the result of the last crawl. A comparison of Ipconfig results under Doc is attached below



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java request host IP address and its corresponding subnet mask

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.