• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get Unix timestamp while pinging in Android

PingAm

Lurker
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!!!
 
I do love a shell programming question :)
Try this

Code:
ping www.google.com | while read ping; do echo "$(date +%s): $ping" | grep from; done

I get the following output from the above command

Code:
1461842129: Reply from 31.55.166.187: bytes=32 time=18ms TTL=57
1461842130: Reply from 31.55.166.187: bytes=32 time=19ms TTL=57
1461842131: Reply from 31.55.166.187: bytes=32 time=20ms TTL=57
1461842132: Reply from 31.55.166.187: bytes=32 time=19ms TTL=57
 
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?
 
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.
 
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?
 
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.
 
"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.
 
So the command would be something like

Code:
ping www.google.com | while read ping; do echo "$(date +%s): $ping"; done

And the ammended code would be

Code:
while((inputLine =in.readLine())!=null){
  if (inputLine.contains("from")) {
    System.out.println(inputLine);
    text.setText(inputLine +"\n\n");
    pingResult += inputLine;
    text.setText(pingResult);
  }
}
 
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?
 
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?
 
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...
 
Ok, to start with, this is an incorrectly formed command

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

You missed the pipe symbol. It should be

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

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.
 
Back
Top Bottom