在网管的设计中,经常要对ip资源进行扫描,来确定ip资源使用情况和历史的活跃ip。传统的办法就是对路由交换的fdb和arp的数据进行分析,然后得出ip的资源信息。但是在经常的情况。
但是很多分析一个ip段的数据,有时候因为网络的复杂,无法准确取到ip相关信息,造成ip资源扫描的误差。这里我们主要介绍2个算法,都是通过多线程来扫描一个网段的ip资源和一个设备的端口资源。
核心的分析类的源代码:
package com.shine.NetAnalisys;
import java.util.List;
import com.shine.NetAnalisys.model.NetAnalyzeCallBack;
import com.shine.NetAnalisys.threadModel.NetPortThreadModel;
import com.shine.NetAnalisys.threadModel.NetScanThreadModel;
import com.shine.NetAnalisys.util.NetWorkUtil;
import com.shine.framework.ThreadPoolUtil.ThreadPoolManager;
import com.shine.framework.ThreadPoolUtil.util.SuperThread;
/**
* 网络分析类库 主要包括ip扫描,端口分析
*
* @author [email protected]
*
*/
publicclass NetAnalisysManager {
privatestatic NetAnalisysManager manager = null;
privateint netScanSize = 10;
privateint netPortSize = 10;
publicstatic NetAnalisysManager getManager() {
if (manager == null)
manager = new NetAnalisysManager();
return manager;
}
publicvoid initThreadModel() {
initNetScanThreadModel();
initNetPortThreadModel();
}
publicvoid initNetScanThreadModel() {
for (int i = 0; i < netScanSize; i++) {
addNetScanThreadModel(i);
}
ThreadPoolManager.getManager().startThreadPool();
}
publicvoid initNetPortThreadModel() {
for (int i = 0; i < netPortSize; i++) {
addNetPortThreadModel(i);
}
ThreadPoolManager.getManager().startThreadPool();
}
privatevoid addNetScanThreadModel(int i) {
NetScanThreadModel threadModel = new NetScanThreadModel();
threadModel.setThreadName("netScanThreadModel" + i);
ThreadPoolManager.getManager().addThread(threadModel);
threadModel = null;
}
privatevoid addNetPortThreadModel(int i) {
NetPortThreadModel threadModel = new NetPortThreadModel();
threadModel.setThreadName("netPortThreadModel" + i);
ThreadPoolManager.getManager().addThread(threadModel);
threadModel = null;
}
/**
* 开始执行网络扫描
*
* @param startIp
* @param endIp
* @param callback
* @param methodName
*/
publicvoid startNetScan(String startIp, String endIp,
NetAnalyzeCallBack callback, String methodName) {
List
ipRangeList = NetWorkUtil.seperateIpRange(startIp, endIp, 2);
int index = 0;
callback.setAnalyzeFinished(false);
long firstTime = System.currentTimeMillis();
while (index < ipRangeList.size()) {
// 获取空闲线程,如果线程繁忙则等待
SuperThread theThread = ThreadPoolManager.getManager()
.getIdleThread("netScanThreadModel");
if (theThread != null) {
NetScanThreadModel netScanModel = (NetScanThreadModel) theThread
.getThreadModel();
if (netScanModel.getObject() == null) {
netScanModel.setObject(callback);
}
netScanModel.setMethodName(methodName);
theThread.setValues(ipRangeList.get(index));
index++;
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
while (ThreadPoolManager.getManager().getIdleThreadSize(
"netScanThreadModel") != this.netScanSize) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
callback.setAnalyzeFinished(true);
long passTime = System.currentTimeMillis() - firstTime;
// System.out.println("执行所需时间:" + passTime);
// ThreadPoolManager.getManager().stopThreadPool();
}
/**
* 开始执行端口扫描
*
* @param ip
* @param minPort
* @param maxPort
* @param callback
* @param methodName
*/
publicvoid startNetPort(String ip, int minPort, int maxPort,
NetAnalyzeCallBack callback, String methodName) {
List
ipPortList = NetWorkUtil.seperateIpPort(ip, minPort, maxPort);
int index = 0;
callback.setAnalyzeFinished(false);
while (index < ipPortList.size()) {
// 获取空闲线程,如果线程繁忙则等待
SuperThread theThread = ThreadPoolManager.getManager()
.getIdleThread("netPortThreadModel");
if (theThread != null) {
NetPortThreadModel netPortModel = (NetPortThreadModel) theThread
.getThreadModel();
netPortModel.setObject(callback);
netPortModel.setMethodName(methodName);
theThread.setValues(ipPortList.get(index));
index++;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
callback.setAnalyzeFinished(true);
}
publicvoid stopNetScanThread() {
ThreadPoolManager.getManager().stopThreadPool("netScanThreadModel");
}
publicvoid stopNetPortThread() {
ThreadPoolManager.getManager().stopThreadPool("netPortThreadModel");
}
publicint getNetScanSize() {
return netScanSize;
}
publicvoid setNetScanSize(int netScanSize) {
this.netScanSize = netScanSize;
}
publicint getNetPortSize() {
return netPortSize;
}
publicvoid setNetPortSize(int netPortSize) {
this.netPortSize = netPortSize;
}
}
调用的例子,ip段扫描:
package com.shine.NetAnalisys;
import com.shine.NetAnalisys.model.NetPortCallBack;
import com.shine.NetAnalisys.model.NetScanCallBack;
publicclass Example {
publicstaticvoid main(String args[]) {
NetAnalisysManager.getManager().setNetScanSize(10);
NetAnalisysManager.getManager().initThreadModel();
long curTime = System.currentTimeMillis();
NetScanCallBack callback = new NetScanCallBack();
NetAnalisysManager.getManager().startNetScan("192.168.11.1", "192.168.11.255", callback, "callback");
for (String ip : callback.getIpList()) {
System.out.println("接收IP" + ip + "可ping通");
}
System.out.println("执行时间" + (System.currentTimeMillis() - curTime));
}
}
调用端口扫描的代码:
package com.shine.NetAnalisys;
import com.shine.NetAnalisys.model.NetPortCallBack;
import com.shine.NetAnalisys.model.NetScanCallBack;
publicclass Example {
publicstaticvoid main(String args[]) {
NetAnalisysManager.getManager().setNetScanSize(10);
NetAnalisysManager.getManager().initThreadModel();
long curTime = System.currentTimeMillis();
NetScanCallBack callback = new NetScanCallBack();
NetAnalisysManager.getManager().startNetScan("192.168.11.1", "192.168.11.255", callback, "callback");
for (String ip : callback.getIpList()) {
System.out.println("接收IP" + ip + "可ping通");
}
System.out.println("执行时间" + (System.currentTimeMillis() - curTime));
}
}
下载的地址(jar含源代码):
http://ken-javaframeword.googlecode.com/files/javaFramework2_5_2.jar
svn地址:http://code.google.com/p/ken-javaframeword/
全部代码请参照svn,上面代码只能导入jar包后执行