高性能的java的ip资源扫描和端口分析

      在网管的设计中,经常要对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 viruscodecn@gmail.com
 * 
 */
public class NetAnalisysManager {
	private static NetAnalisysManager manager = null;

	private int netScanSize = 10;

	private int netPortSize = 10;

	public static NetAnalisysManager getManager() {
		if (manager == null)
			manager = new NetAnalisysManager();
		return manager;
	}

	public void initThreadModel() {
		initNetScanThreadModel();
		initNetPortThreadModel();
	}

	public void initNetScanThreadModel() {
		for (int i = 0; i < netScanSize; i++) {
			addNetScanThreadModel(i);
		}
		ThreadPoolManager.getManager().startThreadPool();
	}

	public void initNetPortThreadModel() {
		for (int i = 0; i < netPortSize; i++) {
			addNetPortThreadModel(i);
		}
		ThreadPoolManager.getManager().startThreadPool();
	}

	private void addNetScanThreadModel(int i) {
		NetScanThreadModel threadModel = new NetScanThreadModel();
		threadModel.setThreadName("netScanThreadModel" + i);
		ThreadPoolManager.getManager().addThread(threadModel);
		threadModel = null;
	}

	private void 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
	 */
	public void startNetScan(String startIp, String endIp,
			NetAnalyzeCallBack callback, String methodName) {
		List<String[]> 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
	 */
	public void startNetPort(String ip, int minPort, int maxPort,
			NetAnalyzeCallBack callback, String methodName) {
		List<String[]> 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);
	}

	public void stopNetScanThread() {
		ThreadPoolManager.getManager().stopThreadPool("netScanThreadModel");
	}

	public void stopNetPortThread() {
		ThreadPoolManager.getManager().stopThreadPool("netPortThreadModel");
	}

	public int getNetScanSize() {
		return netScanSize;
	}

	public void setNetScanSize(int netScanSize) {
		this.netScanSize = netScanSize;
	}

	public int getNetPortSize() {
		return netPortSize;
	}

	public void setNetPortSize(int netPortSize) {
		this.netPortSize = netPortSize;
	}
}


   调用的例子,ip段扫描:

package com.shine.NetAnalisys;

import com.shine.NetAnalisys.model.NetPortCallBack;
import com.shine.NetAnalisys.model.NetScanCallBack;

public class Example {
	public static void 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;

public class Example {
	public static void 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包后执行




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
Java IP端口扫描设计文档 设计目标: 设计一个能够扫描指定IP地址和端口范围的Java程序,用于网络安全测试和设备监控。该程序需要能够快速、准确地扫描目标IP地址上的开放端口,并提供相应的扫描结果。 设计方案: 1. 确定扫描范围: 使用者可以通过命令行参数或者界面输入方式指定要扫描IP地址和端口范围。 2. 多线程扫描: 为了提高扫描的效率,可以使用多线程技术来同时扫描多个端口。可以创建一个线程池来管理扫描线程的数量,并使用线程间的同步机制来确保扫描结果的正确性。 3. 连接测试: 对于每个要扫描IP地址和端口,可以使用Socket类来尝试建立连接。如果连接成功,则说明该端口是开放的,否则是关闭的。 4. 扫描结果显示: 扫描程序可以将扫描结果打印到控制台或者保存到文件中,方便用户查看。可以对扫描结果进行分类,如将开放的端口和关闭的端口分别显示或标记出来。 5. 异常处理: 在进行连接测试时,可能会遇到一些异常情况,如连接超时或者拒绝访问。可以在代码中进行相应的异常处理,以确保程序的稳定性。 设计实现步骤: 1. 获取用户输入的IP地址和端口范围。 2. 创建一个线程池,并设置线程数量。 3. 创建一个扫描任务,将IP地址和端口范围作为参数传入。 4. 将扫描任务提交给线程池执行。 5. 在扫描任务中,使用Socket类来尝试建立连接,并根据连接的结果记录端口是否开放。 6. 将扫描结果保存到数据结构中。 7. 结束线程池,并打印或保存扫描结果。 设计考虑: 1. 扫描速度: 使用多线程技术可以加快扫描速度,但要注意不要过度使用线程,以免影响系统性能。 2. 用户友好性: 提供良好的用户界面或者命令行参数,让用户能够方便地指定扫描IP地址和端口范围。 3. 异常处理: 对于可能的异常情况要进行相应的处理,如设置连接超时时间、处理连接被拒绝等情况。 4. 扫描结果的存储和显示方式要简洁、清晰明了。 设计完成后,可以进行测试和优化,确保程序在各种情况下能够正确稳定地执行扫描任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值