java检测linux系统中所有端口的ip地址
来源:互联网 发布:网络新技术专题报告 编辑:程序博客网 时间:2024/06/05 06:34
boolean globFlag = false;
int tempPort = Integer.valueOf(port);
//获取设备所有网口的ip地址
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (java.net.SocketException e) {
e.printStackTrace();
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;
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服务。
0 0
- java检测linux系统中所有端口的ip地址
- java中替换所有的IP地址
- 固定VMWare虚拟机中linux系统的IP地址
- 获取系统所有IP地址
- 在Linux系统中修改IP地址
- linux系统中设置静态ip地址
- Linux检测IP地址冲突
- Linux中如何检测IP地址冲突问题
- Linux系统修改IP地址的方法
- tiny6410的Linux系统修改IP地址
- linux系统ip地址的配置
- 如何配置Linux系统的IP地址?
- linux/Windows中JAVA获取IP地址
- linux下查看局域网的所有主机的ip地址
- 检测linux网络IP和端口连通性
- linux 获取本机的所有IP地址
- 检测输入框中是否是合法的IP地址
- Linux系统IP冲突检测
- *Codeforces Round #337 (Div. 2) C Harmony Analysis 构造题
- <Error> <WebLogicServer> <BEA-000337>
- 记几个android网站
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- Android SQLite数据库存储
- java检测linux系统中所有端口的ip地址
- linux shell数据重定向(输入重定向与输出重定向)详细分析
- [Java开发之路](12)JDOM和DOM4J解析XML文档
- Opencv人脸识别项目简介
- 不用百度地图,不用高德地图,使用原生的android api也可以定位并做相应的优化
- 解析Excel时列号数字转换为字母
- 从第一个字符串中删除第二个字符串中出现的所有字符
- linux下网络连接socket统计工具ss学习小结
- BZOJ 1009 KMP思想 + DP + 矩阵快速幂