获取本机的内外网ip

package tool;

import java.net.InetAddress;import java.net.UnknownHostException;

/** * @description * @author: 123.com * @create: 2019-01-17 17:34:52 **/

public class GetIp {    public static void getip() {        try {            InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121            String hostAddress = address.getHostAddress();//192.168.0.121            InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");//获取的是该网站的ip地址,比如我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地            String hostAddress1 = address1.getHostAddress();//124.237.121.122            InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根据主机名返回其可能的所有InetAddress对象            for(InetAddress addr:addresses){                System.out.println(addr);//www.baidu.com/14.215.177.38                //www.baidu.com/14.215.177.37            }        } catch (Exception e){            e.printStackTrace();        }    }

public static void main(String[] args) {        String serverIp = getServerIp();        System.out.println(serverIp);    }

/**     * 获取服务器IP地址     * @return     */    @SuppressWarnings("unchecked")    public static String  getServerIp(){        InetAddress addr = null;        try {            addr = InetAddress.getLocalHost();        } catch (UnknownHostException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }

byte[] ipAddr = addr.getAddress();        String ipAddrStr = "";        for (int i = 0; i < ipAddr.length; i++) {            if (i > 0) {                ipAddrStr += ".";            }            ipAddrStr += ipAddr[i] & 0xFF;        }        //System.out.println(ipAddrStr);        return "局域网:"+ipAddrStr;    }}/***************************************************分割线******************************************************/
package tool;

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;

import java.net.URL;

public class Getip2 {    public static String getWebIp() {        try {

//如果请求超时 ,请查看查询本机IP网站 是否更换            URL url = new URL("http://2018.ip138.com/ic.asp");

BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

String s = "";

StringBuffer sb = new StringBuffer("");

String webContent = "";

while ((s = br.readLine()) != null) {                sb.append(s + "\r\n");

}

br.close();            webContent = sb.toString();            int start = webContent.indexOf("[")+1;            int end = webContent.indexOf("]");            webContent = webContent.substring(start,end);            return webContent;

} catch (Exception e) {            System.err.println("获取IP发生错误");            e.printStackTrace();            return "error";

}

}

public static void  main(String [] args) throws IOException {        String ip = Getip2.getWebIp();//        System.out.println(ip);        String str ="";        StringBuilder sb = new StringBuilder(ip);        sb.replace(12,15,str);        System.out.println("所属外网:" + sb.toString());

String serverIp = GetIp.getServerIp();        System.out.println(serverIp);    }

}

原文地址:https://www.cnblogs.com/tk112/p/10283647.html

时间: 2024-07-29 18:51:39

获取本机的内外网ip的相关文章

C#获取本机的外网IP

/// <summary> /// 功能:获取本地的外网IP地址 /// 作者:黄海 /// 时间:2016-07-22 /// </summary> /// <returns></returns> private static string GetPublicIp() { var urlList = new List<string> { "http://ip.qq.com/", "http://pv.sohu.co

Py获取本机指定网卡的ip地址

前段时间需要批量修改服务器的配置文件,里面包含本机的IP地址,由于服务器有多块网卡并且配置多个ip地址,一开始想写shell脚本批量修改,但是既然现在正在学Python,何不研究下使用Python写,在网络上搜了相关文章,根据自己实际情况,便有了下面的脚本: 利用Python获取本机指定网卡的ip地址: #!/usr/bin/env python # -.- coding: utf-8 -.- # By Sandler import socket import fcntl import stru

js获取本机mac地址,IP地址,计算机名

<!DOCTYPE HTML> <html> <head> <title>js获取本机mac地址,IP地址,计算机名</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta content="MSHTML 6.00.2800.1106" name="

iOS 获取内外网ip

外网ip  直接访问ip网站即可  https的不知道有什么 #import "IPAddresses.h" #import <sys/socket.h> #import <sys/sockio.h> #import <sys/ioctl.h> #import <net/if.h> #import <arpa/inet.h> #import <ifaddrs.h> #import <arpa/inet.h&g

node 获取本机主机名和ip

/**获取本机ip**/function getIPAdress(){    var interfaces = require('os').networkInterfaces();    for(var devName in interfaces){        var iface = interfaces[devName];        for(var i=0;i<iface.length;i++){            var alias = iface[i];           

c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息

public class Computer { public static string CpuID; //1.cpu序列号 public static string MacAddress; //2.mac序列号 public static string DiskID; //3.硬盘id public static string IpAddress; //4.ip地址 public static string LoginUserName; //5.登录用户名 public static stri

Linux 编程获取本机所有网卡和IP

#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <ifaddrs.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/in.h> #include <errno.h> #include <string.h> int main

获取本机的第一个IP(常用例程:1)

上曳净俾仕JOY献星佬突http://weibo.com/p/1001604186887362322984?8O=UK/24_2017_12 兹辉彰缺惹GBG忻霞善兄http://weibo.com/p/1001604186887370711632?06=Ei/24_2017_12 献酥茄挥前OFW孔喜南际http://weibo.com/p/1001604186887391683230?uo=2w/24_2017_12 喜汲谝埔夏PZP嘏弥泻胁http://weibo.com/p/100160

如何获取本机内网和外网IP(windows+linux)

1:场景描述 在做Netty相关项目的时候,我们往往需要绑定本机的IP和端口号,如果我们把它写在配置文件中,那么我们每次换电脑运行或者部署到其他环境时候都需要修改配置文件.这样就会比较麻烦,如果我们把它做成智能的获取本机的IP,这样我们的代码的可移植性就提高了.下面就介绍一种在windows和linux下面可以智能获取我们本机的局域网IP和外网IP的方法,不妥之处还请大家多多指教. 2:解决方法以及代码 首先贴上获取IP的工具类 /** * Copyright (C) 2015 Raxtone