2

Is there any way to check Internet Connection on Linux Ubuntu? Because this one is only for Windows-- programmatically check whether my machine has internet access or not

Or Using Poco libraries.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37

1 Answers1

0

You can use route command in Linux to check

FILE *output;

if(!(output = popen("/sbin/route -n | grep -c '^0\\.0\\.0\\.0'","r")))
{
    return 1;
}
unsigned int i;
fscanf(output,"%u",&i);
if(i == 1)
       cerr<<"There is internet connection\n";
else if(i == 0)
       cerr<<"There is no internet connection\n";
pclose(output);

You can find more information about it here.

XZ6H
  • 1,779
  • 19
  • 25
  • 3
    It will only give you the information that a default route is set. but if you can reach internet ip addresses will not be guaranteed. therefor you should open a connection to a well known ip address in the internet like 8.8.8.8 / 8.8.8.4 (google DNS server) – 0x0C4 Jun 05 '17 at 11:37