Java to seek 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 of adjacent positions)//Get the subnet mask of the corresponding IP address public void Getipaddressandmask () {String OS = System.getprope  
		 Rty ("Os.name"); 
				 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 to facilitate the comparison
					 * * Obtain the Machine IP address * In the first line of "IPV4" to obtain the IP address, the English version is "IP addresses" at the beginning of the * format: * IPV4 address ... . . . . . . : 10.10.116.132 (preferred)//See doc under the command output/if (Line.indexof ("IPV4 address") > 0 | | line.indexof ("IP addresses") ;  
						 0) {int index = Line.indexof (":");    
						 Index + 2;  
						 
						 IPAddress = line.substring (index);
						 Remove the Chinese character "10.10.116.132 (preferred)" 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 that must have its corresponding subnet mask}/* Get the machine subnet mask * In the line beginning with subnet mask, get the subnet mask, the English version is "Subnet Mask The opening * format is: * 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;
					 Consume 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 Testofsubnetm
		 Ask ();
		 
		 Test.getipaddressandmask ();
		 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 results are:

---------------------------------------------------------------Windows IP Configuration host name ............. : DADI primary DNS suffix ........... : node type ............: mixed IP routing enabled ..........: no WINS agent enabled ... . : No Wireless LAN Adapter wireless network connection 29: Connect a 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) IPV4 address ............:172.17.31.1(preferred) Subnet mask ............ : 255.255.0.0 Default gateway ............. : DHCPv6 Iaid ........... : 1512183512 DHCPv6 Client DUID ...: 00-01-00-01-19-03-ef-d5-08-60-6e-dc-a7-b7 the DNS server .....
                           ...: fec0:0:0:ffff::1%1 fec0:0:0:ffff::2%1            NetBIOS on fec0:0:0:ffff::3%1 TCPIP ...: Wireless LAN adapter wireless network connection enabled: Media status ........... . : The media has disconnected the 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 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) IPV4 address ............:10.10.116.132(preferred) Subnet mask ........... . : 255.255.255.192 time to obtain a lease ... . : July 18, 2015 23:01:04 Lease expiration Time ... . : July 25, 2015 23:01:02 default gateway ............. : 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 NetBIOS ...: Tunnel adapter local Connection 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 IPv6 address ..... : fe80::aa:e4df:48c0:88c7%66 (preferred) Default gateway ............. ::: NetBIOS on TCPIP ...: Disabled tunnel Adapter 6to4 Adapter: Media status ............: media disconnected specific DNS Suffix ...: description ............... : Microsoft 6to4 Adapter Physical Address ............. : 00-00-00-00-00-00-00-e0 DHCP is enabled ........... : no sinceDynamic configuration enabled ..........: is a tunnel adapter reusable Microsoft 6to4 Adapter: Media status ............: media disconnected specific D NS suffix ...: description ............... : Microsoft 6to4 Adapter #2 Physical Address ............. : 00-00-00-00-00-00-00-e0 DHCP is enabled ........... : No automatic configuration enabled ..........: is a tunnel adapter Local Area Connection * 17: Media status ............: media disconnected specific DNS suffix .... ..: Description ............... : Microsoft 6to4 Adapter #3 Physical Address ............. : 00-00-00-00-00-00-00-e0 DHCP is enabled ...........
: No automatic configuration enabled ..........: is//partially omitted ... ---------------------------------------------------------------
That's the main output.
IP  Address: 10.10.116.132
subnet Mask: 255.255.255.192
IP  address: 172.17.31.1
Subnet Mask: 255.255.0.0


Below is a comparison of ipconfig results under doc



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.