网络发现之Thread_Ping

public List<Device> devices = new Vector();

	public static void main(String[] args) throws IOException,
			InterruptedException {
		Test test = new Test();
		List<Device> rs = test.searchDevice();
	}

	public List<Device> searchDevice() {
		List<String> ips = IPUtils.getIPByNet("10.10.10");
		ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 5,TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
		Iterator<String> it = ips.iterator();
		while (it.hasNext()) {
			String ip = it.next();
			executor.execute(new IPTask(devices, ip));
		}
		return devices;
	}

 

 

public class IPTask implements Runnable {

	List<Device> devList;
	String ip;
	SearchDeviceService search;

	public IPTask(List<Device> devList, String ip) {
		this.devList = devList;
		this.ip = ip;
		search = SearchDeviceService.getInstance();
	}

	@Override
	public void run() {
		Device tmp = search.Ping(ip);
		if (tmp != null) {
			synchronized (devList) {
				if (!devList.contains(tmp)) {
					System.out.println(Thread.currentThread().getId() + "  " + tmp.toString());
					devList.add(tmp);
				}
			}
		}
	}
}

 

 

public class SearchDeviceService {

	private static SearchDeviceService instance;

	public static SearchDeviceService getInstance() {
		if (instance == null) {
			return new SearchDeviceService();
		}
		return instance;
	}

	public Device Snmp(String ip) {
		Device info = new Device();
		Address targetAddress = GenericAddress.parse("udp:" + ip + "/161");
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString(Config.COMMUNITY));
		target.setAddress(targetAddress);
		target.setRetries(Config.RETRIES);
		target.setVersion(SnmpConstants.version2c);
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(SNMP.WINDOW_HOSTNAME)));
		try {
			ResponseEvent event = SNMP.getInstance().get(pdu, target);
			if (event != null && event.getResponse() != null) {
				Object localObject = event.getResponse().getVariableBindings();
				for (int j = 0; j < ((Vector) localObject).size(); j++) {
					VariableBinding recVB = (VariableBinding) ((Vector) localObject).elementAt(j);
					info.setDeviceName(recVB.getVariable().toString());
					info.setDeviceIP(ip);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return info;
	}

	public Device Ping(String ip) {
		Device info = null;
		try {
			Runtime run = Runtime.getRuntime();
			Process localProcess = run.exec("ping -n 2 -w 1000 " + ip);
			BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));
			String str2 = localBufferedReader.readLine();
			while (str2 != null) {
				if (str2.toUpperCase().contains("TTL=")) {
					InetAddress host = InetAddress.getByName(ip);
					info = new Device();
					info.setDeviceName(host.getCanonicalHostName());
					info.setDeviceIP(ip);
				}
				str2 = localBufferedReader.readLine();
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return info;
	}
}

执行结果:

9  IP:10.10.10.1,name:10.10.10.1
9  IP:10.10.10.30,name:NPIE353B1
10  IP:10.10.10.150,name:ljw-PC
12  IP:10.10.10.153,name:Fisrt-PC
13  IP:10.10.10.165,name:10.10.10.165
11  IP:10.10.10.189,name:10.10.10.189
9  IP:10.10.10.190,name:10.10.10.190
13  IP:10.10.10.196,name:10.10.10.196
9  IP:10.10.10.199,name:PC-20130710QYPU
11  IP:10.10.10.201,name:10.10.10.201
12  IP:10.10.10.202,name:10.10.10.202
12  IP:10.10.10.210,name:10.10.10.210
11  IP:10.10.10.208,name:10.10.10.208
13  IP:10.10.10.212,name:10.10.10.212
9  IP:10.10.10.216,name:10.10.10.216
10  IP:10.10.10.234,name:10.10.10.234
11  IP:10.10.10.231,name:10.10.10.231
9  IP:10.10.10.243,name:10.10.10.243
13  IP:10.10.10.255,name:10.10.10.255
9  IP:10.10.10.253,name:10.10.10.253

 

 

 

 

首先创建一个Vector容器,存放数据,然后通过ThreadPoolExector创建线程管理;具体工作由IPTask开始;

Iptask中,负责接收当前要操作的ip信息,多线程,synchronized当然是必须的;

在service中,runtime执行ping命令,然后得到结果……

 

 

 

 

 

 

你可能感兴趣的:(网络发现之Thread_Ping)