Advertisement
Guest User

Untitled

a guest
May 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package com.frw.tangerino.web.ws;
  2.  
  3. import java.net.InetAddress;
  4. import java.net.UnknownHostException;
  5.  
  6. import org.xbill.DNS.*;
  7.  
  8. /*
  9. <dependency>
  10. <groupId>dnsjava</groupId>
  11. <artifactId>dnsjava</artifactId>
  12. <version>2.1.8</version>
  13. </dependency>
  14. */
  15.  
  16. public class UsingDNS {
  17.  
  18. public static void main(String[] args) {
  19. try {
  20. InetAddress address = InetAddress.getLocalHost();
  21. System.out.println("LOCAL ADDRESS : " + address.getHostAddress());
  22. System.out.println("LOCAL HOST : " + address.getHostName());
  23.  
  24. InetAddress ipAddress = java.net.InetAddress.getByName("api.tangerino.com.br");
  25. String ip = ipAddress.getHostAddress();
  26. System.out.println("API IP : " + ip);
  27.  
  28. final InetAddress ip2 = InetAddress.getByName("52.67.84.7");
  29. final byte[] bytes = ip2.getAddress();
  30. final String host = getHostByIPAddress(bytes);
  31. System.out.println("API HOST : " + host);
  32.  
  33. } catch (UnknownHostException e) {
  34. System.out.println("Host NOT Avaialble");
  35. }
  36.  
  37. }
  38.  
  39. public static String getHostByIPAddress(byte[] addr) throws UnknownHostException {
  40.  
  41. Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
  42.  
  43. // OPEN DNS SERVERS
  44. //final String[] openDNS = new String[] {"208.67.222.222", "208.67.220.220"};
  45.  
  46. // GOOGLE DNS SERVERS
  47. final String[] openDNS = new String[] {"8.8.8.8", "8.8.4.4"};
  48.  
  49. final Resolver resolver = new ExtendedResolver(openDNS);
  50. final Lookup lookUp = new Lookup(name, Type.PTR);
  51. lookUp.setResolver(resolver);
  52. Record[] records = lookUp.run();
  53. if (records == null) {
  54. throw new UnknownHostException();
  55. }
  56. return ((PTRRecord) records[0]).getTarget().toString();
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement