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
Use Filter in Dplyr Conditional on an If Statement in R
Standard Deviation in R Seems to Be Returning the Wrong Answer - am I Doing Something Wrong
Reverse Datetime (Posixct Data) Axis in Ggplot
Reasons That Ggplot2 Legend Does Not Appear
Converting Unit Abbreviations to Numbers
Create an Id (Row Number) Column
Replace a Value Na with the Value from Another Column in R
Multiple Ggplots of Different Sizes
Knitr: How to Prevent Text Wrapping in Output
How to Change the Figure Caption Format in Bookdown
Function to Split a Matrix into Sub-Matrices in R
Replace Duplicated Elements with Na, Instead of Removing Them
How Does Cut with Breaks Work in R