Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public void getIPInformation() {
  2. try {
  3. Pattern ipAddressPattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
  4. Process ipconfig = Runtime.getRuntime().exec("ipconfig");
  5. BufferedReader in = new BufferedReader(new InputStreamReader(ipconfig.getInputStream()));
  6. String s = null;
  7. while((s = in.readLine()) != null) {
  8. Matcher m = ipAddressPattern.matcher(s);
  9. if(m.find()) {
  10. String ip = m.group();
  11. if(s.toLowerCase().contains("gateway")) {
  12. defaultGateway = ip;
  13. } else if(s.toLowerCase().contains("ip") && s.toLowerCase().contains("address")) {
  14. ipAddress = ip;
  15. }
  16. }
  17. System.out.println(s);
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. if(defaultGateway == null) {
  23. System.out.println("Could not find default gateway.");
  24. }
  25. if(ipAddress == null) {
  26. System.out.println("Could not find IPv4 address.");
  27. }
  28. }
Add Comment
Please, Sign In to add comment