0

i need to write a linux script that pings a host in a timed intervall and doesnt output the standard ping command stuff.

like:

ping -i 10 google.com

Output1: google.com POSITIVE
or
Output2: google.com NEGATIVE

How can i achieve that it doesnt show the Ping Output and transform that into the Output in the examples?

Thought about using the Returncode if 0 then its positive else negative.

Thanks for help !

Kleber
  • 5
  • 1
  • "Thought about using the Returncode if 0 then its positive else negative." Sound like a good plan! What did you try? What didn't work? Please take a look at our [tour] to get a better understanding about how to as a [mre] – 0stone0 Jan 13 '21 at 11:34
  • I think the return code part isnt that of a problem but how do i not put out the normal ping command stuff ? – Kleber Jan 13 '21 at 11:41

1 Answers1

1

At present, your ping command will run infinitely and so you will need to add a count through -c e.g.:

ping -c 2 -i 10 google.com

This will ping twice at 10 second intervals.

With this you can then implement:

ping -q -c 2 -i 10 google.com && echo "google.com POSITIVE" || echo "google.com NEGATIVE"

Anything after && will be executed with a return code of 0 otherwise anything after || will be executed

An alternate if else approach:

if ping -q -c 2 -i 10 google.com
then
  echo "google.com POSITIVE"
else
  echo "google.com NEGATIVE"
fi
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Well thanks thats working almost like i need it to but i wanted to do it partly myself. Well now i safed time Thanks ! – Kleber Jan 13 '21 at 12:03
  • Your oneliner suggestion is will execute the OR clause, if the "POSITIVE" echo fails. In your example it will not, as echo should not fail. But it's never the less a bad pattern to advocate. – Andreas Louv Jan 13 '21 at 12:25
  • Fair point but in this particular instance, it is unlikely the echo statement will fail as we are not expanding any variables. – Raman Sailopal Jan 13 '21 at 12:56