1. Download our Official Android App: Forums for Android!

Apps How to get Unix timestamp while pinging in Android

Discussion in 'Android Development' started by PingAm, Apr 28, 2016.

  1. PingAm

    PingAm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Apr 28, 2016

    Apr 28, 2016
    5
    0
    5
    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);
    BufferedReaderin=newBufferedReader(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);
    }

    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 '+%+: {}'";

    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


    Please help me with this need it urgently!!!
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,882
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,882
    11,525
    1,988
    Male
    Software developer
    South West of England
    I do love a shell programming question :)
    Try this

    Code (Text):
    1.  
    2. ping www.google.com | while read ping; do echo "$(date +%s): $ping" | grep from; done
    3.  
    I get the following output from the above command

    Code (Text):
    1.  
    2. 1461842129: Reply from 31.55.166.187: bytes=32 time=18ms TTL=57
    3. 1461842130: Reply from 31.55.166.187: bytes=32 time=19ms TTL=57
    4. 1461842131: Reply from 31.55.166.187: bytes=32 time=20ms TTL=57
    5. 1461842132: Reply from 31.55.166.187: bytes=32 time=19ms TTL=57
    6.  
     
    mikedt likes this.
  3. PingAm

    PingAm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Apr 28, 2016

    Apr 28, 2016
    5
    0
    5
    Sorry for the late reply, just tried it doesn't work from an android device, it works fine on my terminal. Can't figure out the reason why none of the shell commands work on an andoid device. Any suggestions?
     
  4. mikedt

    mikedt 你好
    Rank:
     #4
    Points:
    3,238
    Posts:
    25,122
    Joined:
    Sep 22, 2010

    Sep 22, 2010
    25,122
    14,090
    3,238
    Teachaaa
    Jinan, China
    Probably because Android usually doesn't support a full set of *NIX shell commands. And if you want to do that sort of thing, you have to install Busybox. Which most Android devices don't have, usually because there's no need.
    https://busybox.net/

    I can do the same thing from the Terminal on my Mac no problem of course, OS X, which is a *NIX OS.
     
  5. PingAm

    PingAm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Apr 28, 2016

    Apr 28, 2016
    5
    0
    5
    Yeah I could probably do that, it will work on my phone then, but not on all other android devices that I would be targeting. Is there any other way to obtain a timestamp on an android ping something like date time which I could convert back to unix timestamp?
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,882
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,882
    11,525
    1,988
    Male
    Software developer
    South West of England
    I'm guessing that 'grep' command isn't available on Android?

    If that's the case, then you could you do some post processing on the received data in Java code. All grep does is filter out everything that doesn't have 'from' in the text. You can easily do this in Java.
     
  7. mikedt

    mikedt 你好
    Rank:
     #4
    Points:
    3,238
    Posts:
    25,122
    Joined:
    Sep 22, 2010

    Sep 22, 2010
    25,122
    14,090
    3,238
    Teachaaa
    Jinan, China
    "grep" is a Busybox provided command, not usually in Android. I think Busybox has to be installed in /system, and the device rooted to do that.
     
  8. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,882
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,882
    11,525
    1,988
    Male
    Software developer
    South West of England
    So the command would be something like

    Code (Text):
    1.  
    2. ping www.google.com | while read ping; do echo "$(date +%s): $ping"; done
    3.  
    And the ammended code would be

    Code (Text):
    1.  
    2. while((inputLine =in.readLine())!=null){
    3.   if (inputLine.contains("from")) {
    4.     System.out.println(inputLine);
    5.     text.setText(inputLine +"\n\n");
    6.     pingResult += inputLine;
    7.     text.setText(pingResult);
    8.   }
    9. }
    10.  
     
  9. PingAm

    PingAm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Apr 28, 2016

    Apr 28, 2016
    5
    0
    5
    I tried out your code,

    | while read ping; do echo "$(date +%s): $ping"; done

    This part doesn't seem to work on android so I don't get any value in the input stream so I can't filter out the from part.

    I have checked the error stream and I get this:

    Error Stream: java.lang.ProcessManager$ProcessInputStream@1d27a4efInput Line java.io.BufferedReader@3dfc3bfc

    The ping command works fine for something like "ping -s 100 -c 10 google.com" it gives me the values in the textview, anything that I put after the host doesn't return any value...

    Does your code work on your android device? What output are u getting?
     
  10. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,882
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,882
    11,525
    1,988
    Male
    Software developer
    South West of England
    I just fired up adb and connected to my Nexus 5. The original script works perfectly for me.
    You could try connecting to your device and running it. What output do you get?
     
  11. PingAm

    PingAm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Apr 28, 2016

    Apr 28, 2016
    5
    0
    5
    When I use String pingCmd = "ping -s 100 -c 10 " + host+ "while read ping; do echo "$(date +%s): $ping"; done";

    I get no output just the stuff in the error stream.


    Whereas when I just do

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


    I get something like this

    ping -s 100 -c 10 yahoo.com
    PING yahoo.com (98.139.183.24): 100 data bytes
    108 bytes from 98.139.183.24: icmp_seq=0 ttl=45 time=270.660 ms
    108 bytes from 98.139.183.24: icmp_seq=1 ttl=45 time=318.946 ms

    This is the output from my device textview...
     
  12. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,882
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,882
    11,525
    1,988
    Male
    Software developer
    South West of England
    Ok, to start with, this is an incorrectly formed command

    Code (Text):
    1.  
    2. String pingCmd = "ping -s 100 -c 10 " + host+ "while read ping; do echo "$(date +%s): $ping"; done";
    3.  
    You missed the pipe symbol. It should be

    Code (Text):
    1.  
    2. String pingCmd = "ping -s 100 -c 10 " + host+ " | while read ping; do echo \"$(date +%s): $ping\"; done";
    3.  
    But did you connect to your device using adb shell? Only by running the command directly on the command line will you understand what's going wrong.
     

Share This Page

Loading...