ping 端口 java_Java ping 主机 端口

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.InetAddress;importjava.net.InetSocketAddress;importjava.net.Socket;importjava.util.List;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.log4j.Logger;importorg.junit.Test;public classPing {private static final Logger LOGGER = Logger.getLogger(Ping.class);

@Testpublic voidscanPortTest() {

scanPort("192.168.1.107", 9200);//for (int i = 1000; i <= 9999; i++) {//scanPort("127.0.0.1", i);//}

}public void scanPort(String ipAddress, intport) {

Socket client= null;try{

client= newSocket();

client.connect(new InetSocketAddress(ipAddress, port), 300);

LOGGER.info(ipAddress+ ":" +port);

}catch(IOException e) {

LOGGER.error("端口:" + port + " 异常 " +e);

}finally{try{

client.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

@Testpublic void discoveryTest() throwsException {

Discovery("192.168.1", 1, 254);

}private static List Discovery(String networkSection, int start, int end) throwsException {for (int i = start; i <= end; i++) {

String ipAddress= networkSection + "." +i;

ping(ipAddress);

}return null;

}private static boolean ping(String ipAddress) throwsException {//超时应该在3钞以上

int timeOut = 3000;//当返回值是true时,说明host是可用的,false则不可。

boolean status =InetAddress.getByName(ipAddress).isReachable(timeOut);

LOGGER.info("目标地址 " + ipAddress + " 是否可到达:" +status);returnstatus;

}private static void ping02(String ipAddress) throwsException {

String line= null;try{

Process pro= Runtime.getRuntime().exec("ping " +ipAddress);

InputStreamReader isr= new InputStreamReader(pro.getInputStream(), "GBK");

BufferedReader buf= newBufferedReader(isr);while ((line = buf.readLine()) != null)

LOGGER.info(line);

}catch(Exception ex) {

LOGGER.error(ex.getMessage());

}

}private static boolean ping(String ipAddress, int pingTimes, inttimeOut) {

BufferedReader in= null;//将要执行的ping命令,此命令是windows格式的命令

Runtime r =Runtime.getRuntime();

String pingCommand= "ping " + ipAddress + " -n " + pingTimes + " -w " +timeOut;try { //执行命令并获取输出

LOGGER.info("执行命令 " +pingCommand);

Process p=r.exec(pingCommand);if (p == null) {return false;

}

in= new BufferedReader(newInputStreamReader(p.getInputStream()));int connectedCount = 0;

String line= null;while ((line = in.readLine()) != null) {

connectedCount+=getCheckResult(line);

}//如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真

boolean flag = connectedCount ==pingTimes;

LOGGER.info("目标地址 " + ipAddress + " 是否可PING通:" +flag);returnflag;

}catch(Exception ex) {

LOGGER.error(ex);return false;

}finally{try{

in.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}//若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.

private static intgetCheckResult(String line) {

Pattern pattern= Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);

Matcher matcher=pattern.matcher(line);while(matcher.find()) {return 1;

}return 0;

}

@Testpublic void pingTest() throwsException {

String ipAddress= "192.168.1.107";

ping(ipAddress);

ping(ipAddress,4, 5000);

ping02(ipAddress);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值