Android中实现ping功能的多种方法详解

发布时间:2020-08-29 07:40:39 作者:恬静释然
来源:脚本之家 阅读:554

使用java来实现ping功能。 并写入文件。为了使用java来实现ping的功能,有人推荐使用java的 Runtime.exec()方法来直接调用系统的Ping命令,也有人完成了纯Java实现Ping的程序,使用的是Java的NIO包(native io, 高效IO包)。但是设备检测只是想测试一个远程主机是否可用。所以,可以使用以下三种方式来实现:

1. Jdk1.5的InetAddresss方式

自从Java 1.5,java.net包中就实现了ICMP ping的功能。

使用时应注意,如果远程服务器设置了防火墙或相关的配制,可能会影响到结果。另外,由于发送ICMP请求需要程序对系统有一定的权限,当这个权限无法满足时, isReachable方法将试着连接远程主机的TCP端口 7(Echo)。代码如下:

 public static boolean ping(String ipAddress) throws Exception {
    int timeOut = 3000; // 超时应该在3钞以上
    boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
    return status;
  }

2. 最简单的办法,直接调用CMD

public static void ping1(String ipAddress) throws Exception {
    String line = null;
    try {
      Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
      BufferedReader buf = new BufferedReader(new InputStreamReader(
          pro.getInputStream()));
      while ((line = buf.readLine()) != null)
        System.out.println(line);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }

3.Java调用控制台执行ping命令

具体的思路是这样的:
通过程序调用类似“ping 127.0.0.1 -n 10 -w 4”的命令,这命令会执行ping十次,如果通顺则会输出类似“来自127.0.0.1的回复: 字节=32 时间<1ms TTL=64”的文本(具体数字根据实际情况会有变化),其中中文是根据环境本地化的,有些机器上的中文部分是英文,但不论是中英文环境, 后面的“<1ms TTL=62”字样总是固定的,它表明一次ping的结果是能通的。如果这个字样出现的次数等于10次即测试的次数,则说明127.0.0.1是百分之百能连通的。
技术上:具体调用dos命令用Runtime.getRuntime().exec实现,查看字符串是否符合格式用正则表达式实现。代码如下:

public static boolean ping2(String ipAddress, int pingTimes, int timeOut) { 
    BufferedReader in = null; 
    Runtime r = Runtime.getRuntime(); // 将要执行的ping命令,此命令是windows格式的命令 
    String pingCommand = "ping " + ipAddress + " -n " + pingTimes  + " -w " + timeOut; 
    try {  // 执行命令并获取输出 
      System.out.println(pingCommand);  
      Process p = r.exec(pingCommand);  
      if (p == null) {  
        return false;  
      }
      in = new BufferedReader(new InputStreamReader(p.getInputStream()));  // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数 
      int connectedCount = 0;  
      String line = null;  
      while ((line = in.readLine()) != null) {  
        connectedCount += getCheckResult(line);  
      }  // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真 
      return connectedCount == pingTimes; 
    } catch (Exception ex) {  
      ex.printStackTrace();  // 出现异常则返回假 
      return false; 
    } finally {  
      try {  
        in.close();  
      } catch (IOException e) {  
        e.printStackTrace();  
      } 
    }
  }
  //若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
  private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+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; 
  }

4. 实现程序一开始就ping,运行完之后接受ping,并写入文件

完整代码如下:

import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ping {
  private static final String TAG = "Ping";
  private static Runtime runtime;
  private static Process process;
  private static File pingFile;
 /**
   * Jdk1.5的InetAddresss,代码简单
   * @param ipAddress
   * @throws Exception
   */
  public static boolean ping(String ipAddress) throws Exception {
    int timeOut = 3000; // 超时应该在3钞以上
    boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
    return status;
  }
  /**
   * 使用java调用cmd命令,这种方式最简单,可以把ping的过程显示在本地。ping出相应的格式
   * @param url
   * @throws Exception
   */
  public static void ping1(String url) throws Exception {
    String line = null;
    // 获取主机名
    URL transUrl = null;
    String filePathName = "/sdcard/" + "/ping";
     File commonFilePath = new File(filePathName);
    if (!commonFilePath.exists()) {
      commonFilePath.mkdirs();
      Log.w(TAG, "create path: " + commonFilePath);
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    String date = df.format(new Date());
    String file = "result" + date + ".txt";
    pingFile = new File(commonFilePath,file);
    try {
      transUrl = new URL(url);
      String hostName = transUrl.getHost();
      Log.e(TAG, "hostName: " + hostName);
      runtime = Runtime.getRuntime();
      process = runtime.exec("ping " + hostName);
      BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
      int k = 0;
      while ((line = buf.readLine()) != null) {
        if (line.length() > 0 && line.indexOf("time=") > 0) {
          String context = line.substring(line.indexOf("time="));
          int index = context.indexOf("time=");
          String str = context.substring(index + 5, index + 9);
          Log.e(TAG, "time=: " + str);
          String result =
              new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()) + ", " + hostName + ", " + str + "\r\n";
          Log.e(TAG, "result: " + result);
          write(pingFile, result);
        }
      }
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }
 /**
   * 使用java调用控制台的ping命令,这个比较可靠,还通用,使用起来方便:传入个ip,设置ping的次数和超时,就可以根据返回值来判断是否ping通。
   */
  public static boolean ping2(String ipAddress, int pingTimes, int timeOut) {
    BufferedReader in = null;
    // 将要执行的ping命令,此命令是windows格式的命令
    Runtime r = Runtime.getRuntime();
    String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
    try {
      // 执行命令并获取输出
      System.out.println(pingCommand);
      Process p = r.exec(pingCommand);
      if (p == null) {
        return false;
      }
      // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
      in = new BufferedReader(new InputStreamReader(p.getInputStream()));
      int connectedCount = 0;
      String line = null;
      while ((line = in.readLine()) != null) {
        connectedCount += getCheckResult(line);
      }
      // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
      return connectedCount == pingTimes;
    } catch (Exception ex) {
      ex.printStackTrace(); // 出现异常则返回假
      return false;
    } finally {
      try {
        in.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  /**
   * 停止运行ping
   */
  public static void killPing() {
    if (process != null) {
      process.destroy();
      Log.e(TAG, "process: " + process);
    }
  }
  public static void write(File file, String content) {
    BufferedWriter out = null;
    Log.e(TAG, "file: " + file);
    try {
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
      out.write(content);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  // 若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
  private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+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;
  }
  /*
   * public static void main(String[] args) throws Exception { String ipAddress = "appdlssl.dbankcdn.com"; //
   * System.out.println(ping(ipAddress)); ping02(); // System.out.println(ping(ipAddress, 5, 5000)); }
   */
}

总结

到此这篇关于Android中实现ping功能的多种方法详解的文章就介绍到这了,更多相关android ping 功能内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

推荐阅读:
  1. 多种MySQL复制方法详解
  2. 质数的多种实现方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android ping roi

上一篇:对某网站的一次扫描及渗 透测试

下一篇:Flume NG 学习笔记(八)Interceptors(拦截器)测试

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》