How to Determine If You Have an Internet Connection in R

How to determine if you have an internet connection in R

Here is an attempt at parsing the output from ipconfig/ifconfig, as suggested by Spacedman.

havingIP <- function() {
if (.Platform$OS.type == "windows") {
ipmessage <- system("ipconfig", intern = TRUE)
} else {
ipmessage <- system("ifconfig", intern = TRUE)
}
validIP <- "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
any(grep(validIP, ipmessage))
}

With a simple TRUE/FALSE output

> havingIP()
[1] TRUE

test if internet is connected over WiFi or ethernet cable in R

My code is not the most beautiful but it will return a data frame where you can simply read the connection status based on the column "Status" and "Interface Name". The main problem is that you might end up with various Ethernet/WiFi configurations and therefore it is quite complicated to parse ipconfigs output.

My version is based on the simple shell command netsh interface show interface

Here is the code:

netsh_lst = system("netsh interface show interface", intern = T)
netsh_df <- NULL

for (i in seq(1,length(netsh_lst))){

current_line <- as.vector(strsplit(netsh_lst[i], '\\s+')[[1]])

if (length(current_line)>4){
current_line <- current_line[1:3]
current_line[4] <- paste(current_line[4:length(current_line)], collapse = ' ')

}
if (length(current_line)>2){

netsh_df = rbind(netsh_df,current_line)

}
}

names <- netsh_df[1,]

colnames(netsh_df) <- names
netsh_df <- netsh_df[-1,]
row.names(netsh_df) <- 1:length(netsh_df[,1])
print(netsh_df)

Command that allows re-checking TRUE/FALSE

It seems like you might be looking for break:

while (TRUE) {
if (havingIP()) {
print("working") # execute what you want here
break # and if we ever reach here, then exit the while loop
} else {
for (i in 1:5) {
Sys.sleep(1)
cat(i)
}
}
}

How can I see if there's an available and active network connection in Python?

Perhaps you could use something like this:

import urllib2

def internet_on():
try:
urllib2.urlopen('http://216.58.192.142', timeout=1)
return True
except urllib2.URLError as err:
return False

Currently, 216.58.192.142 is one of the IP addresses for google.com. Change http://216.58.192.142 to whatever site can be expected to respond quickly.

This fixed IP will not map to google.com forever. So this code is
not robust -- it will need constant maintenance to keep it working.

The reason why the code above uses a fixed IP address instead of fully qualified domain name (FQDN) is because a FQDN would require a DNS lookup. When the machine does not have a working internet connection, the DNS lookup itself may block the call to urllib_request.urlopen for more than a second. Thanks to @rzetterberg for pointing this out.


If the fixed IP address above is not working, you can find a current IP address for google.com (on unix) by running

% dig google.com  +trace 
...
google.com. 300 IN A 216.58.192.142


Related Topics



Leave a reply



Submit