4

This is kind of a follow up to this question.

My goal in the end is to use my Raspberry Pi 1 Model B to log whenever the Internet connection goes down, and for how long.

I tried doing that with the following command:

ping 8.8.8.8 |  while read line; do echo "$(date): $line"; done | grep --line-buffered time= | tee -a googleping

The above command works on a Ubuntu 15.10 Server and also my MacBook Air with OS X 10.11.2. So I thought I could just use the same on the Pi. But then the first error showed up.

$ ping 8.8.8.8

ping: icmp open socket: Operation not permitted

To get around that I then started ping as super user:

$ sudo ping 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=12.8 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=12.6 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=13.0 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=12.6 ms

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 12.640/12.787/13.002/0.171 ms

Now you would think this solves my problem but no, after this there is another problem I noticed. The pingcommand on the Pi doesn't output request timeouts. So when a package is timed out the Pi just resends it where I would expect a message like that:

Request timeout for icmp_seq 39

But what I get is just nothing, it apparently just resends the package until it gets an answer, but the lost packages show up in the summary at the end:

--- 8.8.8.8 ping statistics ---
168 packets transmitted, 131 received, 22% packet loss, time 167191ms
rtt min/avg/max/mdev = 12.082/13.099/32.888/2.322 ms

The last output before the summary is the following:

64 bytes from 8.8.8.8: icmp_seq=150 ttl=58 time=12.5 ms

Which shows that only 151 different icmp_sequences were sent, and the dropped ones just resend over and over again.

I should also add, that I ping my local router 192.168.2.1 in addition to google 8.8.8.8 to see if it is the connection to the router, or really the connection to google.

The following results in the same output on both systems:

$ ping -V

ping utility, iputils-s20121221

After some looking around I found an option in the man page for ping on the Pi that somewhat does what I want:

$ man ping

[...]
-O     Report  outstanding  ICMP  ECHO  reply  before  sending next packet.
       This is useful together with the timestamp -D to log output to a 
       diagnostic  file  and  search  for missing answers.
[...]

That produces the following output if a package is too slow:

no answer yet for icmp_seq=499

But if my understanding is correct then this is different in the way that the ping command on ubuntu only outputs the message if the answer is not received before the timeout, even if another ping package was send before the answer received. The ping command on my Pi prints the message also out when the answer will be revived after the message is received.

So why is this different on a Pi than on a Ubuntu server? How can I achieve my goal?

Question also posted on superuser.com.

  • Please do not cross post questions, as it leads to duplication of effort... Good question though... – Greenonline Dec 28 '15 at 19:28
  • Have you checked the version of ping on both systems. ping -V. The Pi version may not be the same as the server one, depending on just which distribution you are using on the Pi. – patthoyts Dec 28 '15 at 20:47
  • Yes, both the same Version. Added into my post. Also I will not post cross platform again in the future! – usbpc102 Dec 28 '15 at 21:12
  • Completly rewrote the question to give more information in a better organized way. – usbpc102 Dec 29 '15 at 15:57
3

The reason you need to use sudo with ping is it requires access to a restricted capability. However, linux allows for quite fine grained capability control. The following command permits the ping program to have access to open raw sockets and you can limit access then using unix user groups if you really care.

sudo setcap 'cap_net_raw=+ep' $(which ping)

After using this, ping can be used by any logged in user but doesn't require super-user access. This just lets it open raw sockets.

This may not help you with your main question however.

  • Thanks! But, yes, unfortunately it does not solve my problem. It just removes the need for running ping as superuser. :) – usbpc102 Dec 28 '15 at 17:23
0

Quoting from https://www.raspberrypi.org/forums/viewtopic.php?t=128458&p=859002

That's a known bug in one of the recent releases. You can fix it with sudo chmod u+s /bin/ping. Then ping should work.

chmod changes the permission.

Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.