0

Is the output of ipconfig same in all windows machine ??. I want to capture ip-address , default gateway, subnet mask from ipconfig's output.

Currently i am doing this :

String[] arr = s.split("IPv4 Address. . . . . . . . . . . : ");
            ipAddress = (arr[1].split("\\n"))[0];
            String[] arr1 = s.split("Subnet Mask . . . . . . . . . . . : ");
            subnetMask = (arr1[1].split("\\n"))[0];
            String[] arr2 = s.split("Default Gateway . . . . . . . . . : ");
            defaultGateway = (arr2[1].split("\\n"))[0];

Anyone has better ideas to use some regex???

Thanks in advance.

ayush
  • 14,350
  • 11
  • 53
  • 100
  • look here: http://stackoverflow.com/questions/11930/how-can-i-determine-the-ip-of-my-router-gateway-in-java – shem Jul 22 '12 at 10:38

2 Answers2

2

You probably shouldn't rely on the format of the command always being the same (for example, if someone in a non-English speaking country uses your code, the names of the various values will be different - this may or may not be important to you).

You can retrieve the same information using the java.net package. Also, this question might give you a useful example.

Community
  • 1
  • 1
codebox
  • 19,927
  • 9
  • 63
  • 81
1

Use regex pattern:

Pattern pattern = Pattern.compile(
  "[\\n\\r]\\s*IP\\s+Address[^\\d]+(\\d\\S+).*Mask[^\\d]+(\\d\\S+).*Gateway[^\\d]+(\\d\\S+)",
  Pattern.DOTALL);
Ωmega
  • 42,614
  • 34
  • 134
  • 203