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