package demo_p;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Ping1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String begin = "192.168.1.1" ;
String end = "192.168.1.255";
ForPing(begin,end ) ;
}
/*Ping 方法*/
public static boolean Ping (String IP ) {
InetAddress address ;
boolean isIPRechable = false ;
try {
address=InetAddress.getByName(IP);
isIPRechable=address.isReachable(500);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isIPRechable;
}
// 循环的IP
public static void ForPing (String beginip,String endip) {
String IPbegin[] = beginip.split("\\.");
String IPend[] = endip.split("\\.");
for(int i = Integer.valueOf(IPbegin[3] ) ; i<= Integer.valueOf( IPend[3] ) ; i++) {
String IP = IPbegin[0]+"."+IPbegin[1]+"."+IPbegin[2]+"."+ i;
Boolean isp = Ping(IP);
if (isp) {
System.out.println(IP+"通");
}else {
System.out.println(IP+"不通");
}
}
}
}