1、命令行模式:
Linux:
如果Linux系统是刚建的,ifconfig找不到
ifconfig
yum install ifconfig
//报错
yum search ifconfig
//最后一行如果有net-tools.x86_64
yum install net-tools
windows:
ipconfig
2、前端:
搜狐插件:
//html引入
<script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
//js测试
console.info(returnCitySN["cip"]);
注意:http和https请求头都可以查到ip地址,推荐https,实际项目中如果使用HTTP请求,则无影响,若项目使用https,这里地址必须也是https,否则前端报错returnCitySN未定义
请求地址:
转载于两种快速查询主机公网IP的方法_Thomas_Lean的博客-CSDN博客_公网ip查询
- http://ip.42.pl/raw
- https://ifconfig.me/ip
- http://jsonip.com
- http://httpbin.org/ip
- https://api.ipify.org/?format=json
$. ajax( {
url :'https://api.ipify.org/?format=json',
type :'get' ,
success : function(rel){
console. info(rel) ;
}
})
3、Java:
import java.net.InetAddress;
public class Main {
public static void main(String[] args)
throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress:
"+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: "+hostname);
}
}