【问题标题】:test if internet is connected over WiFi or ethernet cable in R测试互联网是否通过WiFi或R中的以太网电缆连接
【发布时间】:2021-01-21 17:31:26
【问题描述】:

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

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

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

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

我在windows 环境中工作。

谢谢

【问题讨论】:

  • 您是否试图让这项工作特别适用于一个操作系统?或者您需要它在 Windows、Mac 和 Linux 上工作吗?这似乎很大程度上取决于操作系统,因为这不是 R 本身关心的事情。
  • 抱歉,忘记加windows了,马上更新!
  • 我建议在不同的标签下询问这个问题,或者可能在超级用户而不是堆栈溢出处询问。这个问题实际上与 R 没有任何关系,而是关于各种可能的配置以及它们如何在ipconfig 输出中显示的知识。

标签: r windows cmd command-line ipconfig


【解决方案1】:

我的代码不是最漂亮的,但它会返回一个数据框,您可以在其中简单地根据“状态”和“接口名称”列读取连接状态。主要问题是您最终可能会得到各种以太网/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)

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2021-01-13
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2021-06-21
    相关资源
    最近更新 更多