java获取当前网站的IP地址

 1 package ip;
 2 
 3 import java.net.InetAddress;
 4 import java.net.UnknownHostException;
 5 
 6 /**
 7  * 
 8  * @author 
 9  *
10  */
11 public class Ipspider {
12 
13     public static void main(String[] args) {
14      
15         try {
16             //用 getLocalHost() 方法创建的InetAddress的对象
17             InetAddress address = InetAddress.getLocalHost();
18             System.out.println(address.getHostName());//主机名
19             System.out.println(address.getCanonicalHostName());//主机别名
20             System.out.println(address.getHostAddress());//获取IP地址
21             System.out.println("===============");
22

       
23           //用域名创建 InetAddress对象
24             InetAddress address1 = InetAddress.getByName("www.baidu.com");
25             //获取的是该网站的ip地址,如果我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地址
26             System.out.println(address1.getHostName());//www.baidu.com
27             System.out.println(address1.getCanonicalHostName());//180.97.33.108
28             System.out.println(address1.getHostAddress());//180.97.33.108
29             System.out.println("===============");
30

  
31             InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
32             for (InetAddress addr : addresses) {
33                 System.out.println(addr);
34                // www.baidu.com/180.97.33.107
35                //www.baidu.com/180.97.33.108
36             }
37         } catch (UnknownHostException e) {
38             e.printStackTrace();
39         }
40         
41     }
42 }

 

转载于:https://www.cnblogs.com/csh520mjy/p/10180143.html

你可能感兴趣的:(java,运维)