调用process类 进行ping
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* ping测试
* @author kernel
*
*/
public class PINGUtil {
private final ArrayList<String> ping_res;
public ArrayList<String> getResult(){
return ping_res;
}
public PINGUtil() throws UnknownHostException{
ping_res=new ArrayList<String>();
InetAddress inet=InetAddress.getLocalHost();
String hostadress=inet.getHostAddress();
System.out.println("hostadress:"+hostadress);
int k=0;
k=hostadress.lastIndexOf(".");
String ping_sub=hostadress.substring(0,k+1);
System.out.println("start execute");//获取网段
ExecutorService exec=Executors.newFixedThreadPool(30);
for(int i=1;i<=255;i++){
String pingip=ping_sub+i;
exec.execute(new PingTread(pingip));
}
System.out.println("run shutdown");
exec.shutdown();//terminate shut 任务完成 就关闭线程
System.out.println("asyc task:");
try {
System.out.println("awaitTermination task start");
//堵塞time out 30s block 超时 直接跳过awaitTermination 但是exec会继续未完成的任务
exec.awaitTermination(100000, TimeUnit.MILLISECONDS);
System.out.println("awaitTermination task end");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end execute");
}
public class PingTread extends Thread{
String ip;
public PingTread(String ping_sub){
this.ip=ping_sub;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
InputStreamReader in=null;
BufferedReader br=null;
try {
/**
* Linux:ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l<前置载入>][-p<范本样式>][-s<数据包大小>][-t<存活数值>][主机名称或IP地址]
* [ -d] [ -D ] [ -n ] [ -q ] [ -r] [ -v] [ \ -R ] [ -a addr_family ]
* [ -c Count ] [ -w timeout ] [ -f | -i \ Wait ]
* [ -l Preload ] [ -p Pattern ] [ -s PacketSize ]
* [ -S hostname/IP addr ] \ [ -L ] [ - I a.b.c.d. ]
* [ -o interface ] [ -T ttl ] Host [ PacketSize ] \ [ Count ]
*
* Windows:
* ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
* [-r count] [-s count] [[-j host-list] | [-k host-list]]
* [-w timeout] target_name
*
*/
Process proces=Runtime.getRuntime().exec("cmd.exe /c ping "+ip+" -w 300 -n 1");//win /c 关闭
//Process proces=Runtime.getRuntime().exec("/usr/bin/sh /c ping "+ip+" -w 300 -c 1");//linux /c 关闭
//System.out.println("proces.hashCode() "+proces.hashCode());
in=new InputStreamReader(proces.getInputStream());
br=new BufferedReader(in);
StringBuffer sb=new StringBuffer();
String buff;
while((buff=br.readLine())!=null){
sb.append(buff);
}
if(sb.indexOf("timed out")==(-1)){
System.out.println(sb.toString());
//可以ping通的ip
ping_res.add(ip);
}
if(proces.waitFor()==0){
System.out.println("进程结束");
};
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* @param args
*
*/
public static void main(String[] args) throws UnknownHostException {
PINGUtil pin= new PINGUtil();
ArrayList<String> re=pin.getResult();
for(String s:re){
System.out.println("本地局域网:"+s);
}
}
}
运行结果:
hostadress:192.168.2.103
start execute
run shutdown
asyc task:
awaitTermination task start
Pinging 192.168.2.1 with 32 bytes of data:Reply from 192.168.2.1: bytes=32 time=1ms TTL=64Ping statistics for 192.168.2.1: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 1ms, Maximum = 1ms, Average = 1ms
进程结束
Pinging 192.168.2.103 with 32 bytes of data:Reply from 192.168.2.103: bytes=32 time<1ms TTL=64Ping statistics for 192.168.2.103: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms
进程结束
Pinging 192.168.2.102 with 32 bytes of data:Reply from 192.168.2.102: bytes=32 time=126ms TTL=64Ping statistics for 192.168.2.102: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 126ms, Maximum = 126ms, Average = 126ms
进程结束
Pinging 192.168.2.255 with 32 bytes of data:Reply from 192.168.2.1: bytes=32 time=6ms TTL=64Ping statistics for 192.168.2.255: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 6ms, Maximum = 6ms, Average = 6ms
进程结束
awaitTermination task end
end execute
本地局域网:192.168.2.1
本地局域网:192.168.2.103
本地局域网:192.168.2.102
本地局域网:192.168.2.255