[英]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
?测试您的互联网是否连接到
R
中的 wifi 或以太网的最可靠方法是什么?
I want to do an if
statement, something like:我想做一个
if
语句,例如:
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. system("ipconfig", intern = T)
似乎很有用,但我不确定要提取什么,以便每次都能正确识别 wifi/以太网,无论连接设置是什么。
I'm working in a windows
environment.我在
windows
环境中工作。
thanks谢谢
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.
主要问题是您最终可能会使用各种以太网/WiFi 配置,因此解析 ipconfigs output 非常复杂。
My version is based on the simple shell command netsh interface show interface
我的版本是基于简单的 shell 命令
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.