如何在android中ping时获取unix时间戳 [英] How to get unix timestamp while pinging in android

查看:305
本文介绍了如何在android中ping时获取unix时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够在android中执行基本ping,并且能够在android中使用Processes获得结果。我的代码如下



I have been able to perform rudimentary pings in android and have been able to get the result using Processes in android. My code is as follows

try {
    String pingCmd = "ping -s 100 -c 10 " + host;
    String pingResult = "";
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(pingCmd);
    BufferedReader in = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
        text.setText(inputLine + "\n\n");
        pingResult += inputLine;
        text.setText(pingResult);
    }
    in.close();
}//try
catch (IOException e) {
    System.out.println(e);
}





我的尝试:



我正在运行以下命令String pingCmd =ping -s 100 -c 10+ host;。我想得到每个ping的unix时间戳我已经尝试过以下并且没有得到任何结果:





What I have tried:

I am running the following command String pingCmd = "ping -s 100 -c 10 " + host;. I want to get the unix timestamp with each ping I have already tried the following and have not got any results with them:

 String pingCmd = "ping -s 100 -c 10 " + host+ " -D";

String pingCmd = "ping -s 100 -c 10 " + host+ "| while read pong; do echo "$(date): $pong"; done";

String pingCmd = "ping -s 100 -c 10 " + host+ "| perl -nle 'print scalar(localtime), " ", $_'";

String pingCmd = "ping -s 100 -c 10 " + host+ "| xargs -L 1 -I '{}' date '+%+: {}'";



我运行这些时没有任何错误,只是在textview中没有结果。请告诉我如何获得每个ping的unix时间戳



我希望我的输出看起来像这样每个ping



[1461167279.090372]来自ir1.fp.vip.ne1.yahoo.com(98.138.253.109)的108个字节:icmp_seq = 1 ttl = 51 time = 335 ms


I don't get any error when I run these just no result in the textview. Please tell me how to get the unix timestamp with every ping

I expect my output to look something like this on each ping

[1461167279.090372] 108 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=1 ttl=51 time=335 ms

推荐答案

(date):
(date):


pong ; done;

字符串 pingCmd = ping -s 100 -c 10 + host + | perl -nle'print scalar(localtime),
pong"; done"; String pingCmd = "ping -s 100 -c 10 " + host+ "| perl -nle 'print scalar(localtime), " ",


_';

字符串 pingCmd = ping -s 100 -c 10 + host + | xargs -L 1 -I'{}' date'+%+:{}';
_'"; String pingCmd = "ping -s 100 -c 10 " + host+ "| xargs -L 1 -I '{}' date '+%+: {}'";



我运行这些时没有任何错误,只是在textview中没有结果。请告诉我如何获得每个ping的unix时间戳



我希望我的输出看起来像这样每个ping



[1461167279.090372]来自ir1.fp.vip.ne1.yahoo.com(98.138.253.109)的108个字节:icmp_seq = 1 ttl = 51 time = 335 ms


I don't get any error when I run these just no result in the textview. Please tell me how to get the unix timestamp with every ping

I expect my output to look something like this on each ping

[1461167279.090372] 108 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=1 ttl=51 time=335 ms


这篇关于如何在android中ping时获取unix时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆