0

测试您的互联网是否连接到 wifi 或以太网的最可靠方法是R什么?

我想做一个if声明,例如:

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

system("ipconfig", intern = T)似乎很有用,但我不确定要提取什么,以便每次都能正确识别 wifi/以太网,无论连接设置是什么。

我在一个windows环境中工作。

谢谢

4

1 回答 1

2

我的代码不是最漂亮的,但它会返回一个数据框,您可以在其中简单地根据“状态”和“接口名称”列读取连接状态。主要问题是您最终可能会得到各种以太网/WiFi 配置,因此解析 ipconfigs 输出非常复杂。

我的版本基于简单的 shell 命令netsh interface show interface

这是代码:

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)
于 2021-01-21T19:52:43.600 回答