Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

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

What would be the most robust way to test if your internet is connected to wifi or ethernet in R?

I want to do an if statement, something like:

  if (internet == "wifi") {
    return(x) 
  } else if (internet == "ethernet") {
    return(y)
  } else { #no internet
    return(z)}

system("ipconfig", intern = T) seems to be useful but I'm not sure what to extract so that it correctly identifies wifi/ethernet each time no matter what the connection setup is.

I'm working in a windows environment.

thanks

question from:https://stackoverflow.com/questions/65832716/test-if-internet-is-connected-over-wifi-or-ethernet-cable-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...