-3

I am using a raspberry pi to run a BASH program and return a message indicating if the IP address is up or down. Any help with this would be much appreciated.

Thank you

stiemannkj1
  • 3,802
  • 3
  • 21
  • 42
  • What have you tried? What is it you need help with? (StackOverflow won't build a solution for you, but we can help answer any questions you may have.) – Mr. Llama Oct 08 '15 at 19:25
  • 1
    type `ping` then type the ip, what part could you possibly not understand? – redFIVE Oct 08 '15 at 19:25
  • Have you taken a stab at any code yet or is this a "Write a bash program that tells me whether a server is up or down" type of question? Like... where are you struggling with this one so that we can help out. – JNevill Oct 08 '15 at 19:35
  • Possible duplicate of [Checking host availability by using ping in bash scripts](http://stackoverflow.com/questions/18123211/checking-host-availability-by-using-ping-in-bash-scripts) – user000001 Oct 21 '15 at 16:34

1 Answers1

1

Most *nix commands will return a code of '0' for 'good' and non-zero for 'there is a problem' - you might try:

MSG="Ping up"  
IP="a.b.c.d"  
ping ${IP}  
if [[ $? -ne 0 ]] ; MSG="Ping down" ; fi  
echo "Status for ${IP}: ${MSG}"
Dale_Reagan
  • 1,629
  • 12
  • 10
  • The `if [[ $? `... construct is an anti-pattern. You want simply `if ! ping "$IP"; then echo down; fi` – tripleee Nov 02 '15 at 13:46
  • Not sure what you mean by 'anti-pattern' - in BASH, '$?' is a variable containing the status/return code from the last executed command... While using the 'terse' form for 'testing' is fine it's not 'verbose enough' for me - too easy to miss when debugging... and also, typically, not well discussed/explained when used as a 'solution' in an environment for noobs. :) – Dale_Reagan Nov 03 '15 at 14:58
  • Idiomatic shell script almost never needs to examine `$?` directly. The flow control constructs already do it for you, implicitly and elegantly. – tripleee Nov 03 '15 at 15:22