扫描500个ip10秒钟左右, 一个ip等待0.3秒
说来惭愧, 我觉得是用java写的过程编程, 少贴一点仅供参考:
package steeven;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.http.*;
public class ip extends common implements runnable{
public string ip; // ip, 用户名, 主机名
resultset list; // 分页显示的记录集
public ip cur; // 分页显示的当前记录
static public hashtable ping = new hashtable(); //ping 后的结果集
static int threadcount = 0; //当前线程的数量, 防止过多线程摧毁电脑
public ip() {}
public ip(string ip){
this.ip=ip;
thread r = new thread(this);
r.start();
}
public static void ping(string ip) throws exception{
//最多30个线程
while(threadcount>30)
thread.sleep(50);
threadcount +=1;
ip p = new ip(ip);
}
public void pingall() throws exception{
threadcount =0;
ping = new hashtable();
while(next()) //next()对所有局域网ip放到cur
ping(cur.ip);
//等着所有ping结束
while(threadcount>0)
thread.sleep(50);
}
public void run(){
try{
process p= runtime.getruntime().exec ("ping "+ip+ " -w 300 -n 1");
inputstreamreader ir = new inputstreamreader(p.getinputstream());
linenumberreader input = new linenumberreader (ir);
//读取结果行
for (int i=1 ; i<7; i++)
input.readline();
string line= input.readline();
if (line.length()<17 || line.substring(8,17).equals("timed out"))
ping.put(ip,new boolean(false));
else
ping.put(ip,new boolean(true));
//线程结束
threadcount -= 1;
}catch (ioexception e){}
}
public static void main(string[] args) throws exception{
ip ip= new ip();
ip.pingall();
java.util.enumeration key = ping.keys();
string k;
while((k = (string)key.nextelement()) != null)
system.out.println(k+": "+ping.get(k));
}
}
1. 利用ping比较愚蠢, 但是相对简单些
2. 如果ping 成功后, 用nbtstat还可以得到主机名, 当前用户名, mac地址…一切尽在掌握中 🙂
