Sorry B50D, i didn't answer you !!

You are right, there is not network connection in the screenshot, but the icon changed the status from disconnected to connected. The following C program should solve this issue:



#include
#include
#include

void ping(){
 system("ping 8.8.8.8");
}

void status(){
 system("scan_connection");
}

int main() {
 pthread_t thd1, thd2;

 pthread_create (&thd1, NULL, (void*)ping, NULL);
 pthread_create (&thd2, NULL, (void*)status, NULL);

 pthread_join(thd1, NULL);
 pthread_join(thd2, NULL);
}



Being "scan_connection" the following script:

#!/bin/sh

sleep 1

var=$(ps -e | grep ping 2>&1)
pkill ping

if test -z "$var"; then
  echo DISCONNECTED
else echo CONNECTED
fi

exit 0



This C program is multithreaded. You can build it by the following way:


$ gcc main.c -o main -lpthread


It runs "ping 8.8.8.8", and the "scan_connection" script at the same time in separete threads. The "scan_connection" will wait 1 second before finding out if the ping process is running or not (after that, ping is killed).

If there is no network connection "ping 8.8.8.8" will return "connect: Network is unreachable", and the ping process will be aborted within this second of waiting. So, the "var" variable will be null in this case.

Here you are the output in both cases:


CASE 1:

root@devuan:/home/aitor# ./main
connect: Network is unreachable
DISCONNECTED


CASE 2:

root@devuan:/home/aitor# ./main
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=54 time=34.7 ms
Terminated
CONNECTED


Cheers, and thanks for reporting the bug :)

  Aitor.





Reply via email to