java检测linux系统中所有端口的ip地址
2016-01-03 23:02
597 查看
boolean globFlag = false;
int tempPort = Integer.valueOf(port);
//获取设备所有网口的ip地址
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (java.net.SocketException e) {
e.printStackTrace();
}
//检测每一个网口上的这个端口是否可以连接上
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()){
ip = (InetAddress) addresses.nextElement();
if(ip!=null && ip instanceof Inet4Address){
String ipAddress = ip.getHostAddress();
log.error("ip:-------"+ipAddress);
try{
Socket socket = new Socket(ipAddress,tempPort);
globFlag = true;
}catch (Exception e) {
// TODO: handle exception
}
if(globFlag){
break;
}
}
}
if(globFlag){
break;
}
}
if(globFlag){
response.setContentType("text/json;charset=UTF-8");
response.getWriter().write(
"{\"success\":"
+ false
+ ",\"msg\":\""
+ "该端口已经被占用" + "\"}");
return null;
}
说明:这种用java的方式去检测linux下的端口 的占用情况存在漏洞,原因就是DNS端口53却没有被检测到,
可以使用 netstat -nap|grep 53 命令去查看,会发现该设备上的所有的网口对应的ip上的端口虽然是没有进程在使用的;
但是该端口是不能作为web服务的端口,因为一旦用web使用该端口号,可以查看到web进程是启动起来的,但是DNS端口就被web占用了,
从而形成死锁,无法访问到web服务。
int tempPort = Integer.valueOf(port);
//获取设备所有网口的ip地址
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (java.net.SocketException e) {
e.printStackTrace();
}
//检测每一个网口上的这个端口是否可以连接上
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()){
ip = (InetAddress) addresses.nextElement();
if(ip!=null && ip instanceof Inet4Address){
String ipAddress = ip.getHostAddress();
log.error("ip:-------"+ipAddress);
try{
Socket socket = new Socket(ipAddress,tempPort);
globFlag = true;
}catch (Exception e) {
// TODO: handle exception
}
if(globFlag){
break;
}
}
}
if(globFlag){
break;
}
}
if(globFlag){
response.setContentType("text/json;charset=UTF-8");
response.getWriter().write(
"{\"success\":"
+ false
+ ",\"msg\":\""
+ "该端口已经被占用" + "\"}");
return null;
}
说明:这种用java的方式去检测linux下的端口 的占用情况存在漏洞,原因就是DNS端口53却没有被检测到,
可以使用 netstat -nap|grep 53 命令去查看,会发现该设备上的所有的网口对应的ip上的端口虽然是没有进程在使用的;
但是该端口是不能作为web服务的端口,因为一旦用web使用该端口号,可以查看到web进程是启动起来的,但是DNS端口就被web占用了,
从而形成死锁,无法访问到web服务。
相关文章推荐
- linux下命令执行时间统计工具time使用小结
- linux 命令 —— mv
- 阿里云 CentOS6.5 ssh连接慢的解决方案
- “Linux 中国”发布微信矩阵:不细分,毋宁死
- Linux字符设备驱动的简单总结
- linux中的通配符学习小结
- eSpeak: Linux 文本转语音工具
- 每天一个Linux之chmod命令
- LInux 中的i386,i486,i586,i686和AMD_64,X86,x86_64后缀的区别
- linux中的各种计算方式expr|bc|dc|let
- 终于搞定samba中文乱码的问题
- 【全栈工程师之路】环境搭建-CentOS服务器配置
- Linux日志文件总管——logrotate
- rc.local自启动学习
- 同一个镜像配置的两台不同ip的CentOS主机只有一台能ping得了网的解决方案
- Linux入门教程:CDN加速
- Linux命令之修改主机名
- grub2中添加windows的启动项 安装linux后添加windows系统的引导入口
- CentOS rpm的spec简单模板
- Linux特殊符号大全