-1

I was wondering if someone could help me write an if then statement for a ping..

Example IP: 192.168.10.1

Here's what I have so far..

lcd_command(LINE_1);
if system(("ping -c1 192.168.10.1")) {
lcd_writechars("Ping Successful");
}
else {
lcd_writechars("Ping Successful");
}

Basically, I want the script to ping every 10 seconds and have "Ping Successful" be displayed, on my LCD, when the host is reachable. When the host is unreachable, I want "Ping Unsuccessful" to be displayed, on my LCD, until the host is reachable again.

If someone could help me out, I would greatly appreciate it!

  • 1
    Your code is almost correct. I think you want to write `"Ping Unsuccessful"` in the `else`-clause though. If this is not your problem, please tell us where you are stuck and be specific. – fuz Nov 16 '15 at 19:15
  • 1
    Your syntax is wrong. `if ()` needs parenthesis and the call to `system` inside that `()`. Please provide a full example and not some randomly cut code snippet. – Jens Gustedt Nov 16 '15 at 19:23

2 Answers2

3

Your braces are not syntactically correct (Bracket need to start after if and not after system). Also please find below the proper usage of return status of system call using the W* macros.

int status = system("ping -c1 192.168.10.1");
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
lcd_writechars("Ping Successful\n");
}
else {
lcd_writechars("Ping failed\n");
}
user1969104
  • 2,340
  • 14
  • 15
  • This works great, thank you so much! I do have a quick question, is there anyway to make this process run in the background? When I run the code, it shows the ping cycle in the terminal. How would I modify the code so that it does not display the ping in the terminal, or is that not possible? –  Nov 20 '15 at 15:27
  • I am not sure what you mean by running in background. Since you are looking for the exit status, `system` call needs to return only after the `ping` completes. If you mean just not displaying output of `ping`, then you can use all the features provided by the shell like redirection. So to not display output, you can use `system("ping -c1 192.168.10.1 > /dev/null")`. To not display any error as well, `system("ping -c1 192.168.10.1 > /dev/null 2>&1")`. – user1969104 Nov 20 '15 at 16:02
  • Adding the /dev/null is exactly what I was looking for, thank you so much for your help! –  Nov 20 '15 at 16:27
2

You would have to capture the ping output and process it somehow, but you're using system().

I'd suggest you learn how to use popen() to execute the process and pipe the output to somewhere in memory so that you can then parse the output and figure out what the result of the ping is.

Right now you're relying on system() to return a value - system() doesn't know or care if your ping was successful or not. system will return -1 if the execution of the process fails or will return the exit status of the process when it completes successfully if that helps.

ETA - also, from reading the code it seems you output the same message regardless of what system() is returning.

Nunchy
  • 948
  • 5
  • 11